android中没有找到资源异常

时间:2013-02-27 10:35:32

标签: android android-listview

我正在尝试在我的Android应用程序中创建一个列表视图。但是我在运行项目时遇到资源未找到异常。

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>

ListAdapter类:

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;
}

}

MainActivity类:

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无效

1 个答案:

答案 0 :(得分:10)

您必须在listview适配器getview方法中将自定义布局充气为

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

而不是

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