2013-02-27 136 views
3

我想在我的Android應用程序中創建一個列表視圖。但是在運行項目時,我收到資源未找到異常。資源未找到異常在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型#0×12是無效

+1

哪些資源具有0x7f070001 ID?你可以在生成的R.java中檢查它。 – m0skit0 2013-02-27 10:37:17

+1

發佈完整的錯誤日誌消息。 – 2013-02-27 10:39:07

+0

ListView有那個ID – venkat 2013-02-27 10:39:15

回答

10

所有您需要充氣,你的ListView的適配器的getview自定義佈局方法

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

,而不是

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

謝謝。有效。 – venkat 2013-02-27 10:44:04