android中资源未找到异常 [英] Resource Not found exception in android

查看:89
本文介绍了android中资源未找到异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的android应用程序中创建一个listview.但是我在运行项目时遇到资源未找到异常.

i am trying to create a listview in my android application. But i am getting Resource Not found Exception while running the project.

activity_main.xml:

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:

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类:

ListAdapter Class :

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类:

MainActivity class :

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

android.content.res.Resources$NotFoundException: Resource ID #0x7f070001 type #0x12 is not valid

推荐答案

所有您需要在listview适配器的getview方法中将自定义布局添加为

All you have to inflate the custom layout in your listview's adapter's getview method as

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

代替

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

这篇关于android中资源未找到异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆