gpt4 book ai didi

android - android中找不到资源异常

转载 作者:太空宇宙 更新时间:2023-11-03 11:51:04 25 4
gpt4 key购买 nike

我正在尝试在我的 android 应用程序中创建一个 ListView 。但是我在运行项目时遇到 Resource Not found Exception。

activity_main.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<include
android:id="@+id/title_bar"
layout="@layout/title_bar" />

<ListView
android:id="@+id/FeedList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title_bar"
></ListView>

</RelativeLayout>

list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">

<ImageButton
android:id="@+id/FeedType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#E0E0E0"
android:padding="9dp"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/FeedTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Feed Title"
android:textSize="18sp"
android:layout_toRightOf="@+id/FeedType"
android:paddingLeft="5dp"
android:textColor="#000000"
/>

<TextView
android:id="@+id/FeedPostedBy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/FeedTitle"
android:text="by FeedOwner"
android:layout_toRightOf="@+id/FeedType"
android:paddingLeft="5dp"
android:textSize="10sp"
/>

<TextView
android:id="@+id/FeedContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/FeedPostedBy"
android:layout_toRightOf="@+id/FeedType"
android:paddingLeft="5dp"
android:paddingTop="8dp"
android:text="The content posted as a feed by the feed owner will appear here"
android:textSize="10sp"/>

</RelativeLayout>

列表适配器类:

package com.tcs.internal.prime;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter{

private Activity listAdapterActivity = null;
private ArrayList<HashMap<String,String>> data;
private static LayoutInflater inflater;

public ListAdapter(Activity ListActivity,ArrayList<HashMap<String, String>> listData)
{
listAdapterActivity = ListActivity;
data = listData;
inflater = (LayoutInflater)listAdapterActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}


@Override
public int getCount() {

return data.size();

}

@Override
public Object getItem(int position) {

return position;
}

@Override
public long getItemId(int position) {

return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View listItem = convertView;

if(listItem == null)
listItem = inflater.inflate(R.id.FeedList, null);

ImageButton feedIcon = (ImageButton) listItem.findViewById(R.id.FeedType);
TextView feedTitle = (TextView) listItem.findViewById(R.id.FeedTitle);
TextView feedOwner = (TextView) listItem.findViewById(R.id.FeedPostedBy);
TextView feedContent = (TextView) listItem.findViewById(R.id.FeedContent);

HashMap<String, String> feedData = new HashMap<String, String>();
feedData = data.get(position);

feedIcon.setImageResource(R.drawable.ic_launcher);
feedTitle.setText(feedData.get("FeedTitle"));
feedOwner.setText(feedData.get("FeedOwner"));
feedContent.setText(feedData.get("FeedContent"));


return listItem;
}

}

主 Activity 类:

package com.tcs.internal.prime;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ArrayList<HashMap<String, String>> feedList = new ArrayList<HashMap<String,String>>();

HashMap<String, String> data = new HashMap<String, String>();

data.put("FeedTitle","Application crashed when launched in Windows 7");
data.put("FeedOwner", "By Venkatramanan");
data.put("FeedContent","Launch the financial aid adminstration in windows 7 environment");

feedList.add(data);

data.clear();

data.put("FeedTitle","Application crashed when launched in Windows 8 ");
data.put("FeedOwner", "By Siva Guru");
data.put("FeedContent","Launch the financial aid adminstration in windows 8 environment");

feedList.add(data);

ListView feedListView = (ListView)findViewById(R.id.FeedList);

ListAdapter listAdapter = new ListAdapter(this, feedList);
feedListView.setAdapter(listAdapter);



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

我收到以下异常:

android.content.res.Resources$NotFoundException: 资源 ID #0x7f070001 类型 #0x12 无效

最佳答案

您只需在 ListView 的适配器的 getview 方法中将自定义布局扩展为

       listItem = inflater.inflate(R.layout.list_item, null);

代替

     listItem = inflater.inflate(R.id.FeedList, null);

关于android - android中找不到资源异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15109872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com