android中找不到资源异常

2024-05-04

我正在尝试在我的 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>

列表项.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;
}

}

主要活动类:

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


您只需在列表视图适配器 getview 方法中膨胀自定义布局即可

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

代替

     listItem = inflater.inflate(R.id.FeedList, null);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android中找不到资源异常 的相关文章

  • 如何在android网络库(ION)中使用自签名SSL?

    使用此网络库 https github com koush ion https github com koush ion 由于当前状态是开发 我想使用自签名 SSL 证书 图书馆论坛有一些讨论 https github com koush
  • Android上如何模拟后台Activity因内存不足而被系统杀死的过程?

    我正在处理 内存不足 不再有后台进程 问题 当这种情况发生时 我的活动处于后台并被杀死 我正在尝试保存并加载实例状态来解决它 但因为它并不是每次都会发生 在这种情况下我应该如何测试我的活动 Thanks 您可以通过 adb 强制进程终止 g
  • 让协程等待之前的调用

    我还没有完全掌握 Kotlin 协程 基本上我希望协程在执行之前等待任何先前的调用完成 下面的代码似乎可以工作 但它正在做我认为它正在做的事情吗 private var saveJob Job null fun save saveJob s
  • 需要使用手机后退按钮返回 Web 视图的帮助

    这是我的代码 package com testappmobile import android app Activity import android os Bundle import android view KeyEvent impor
  • 如何在 android 中启动“添加联系人”活动

    您能告诉我如何在 Android 中启动 添加联系人 活动吗 谢谢 API 级别 5 及以上解决方案 Add listener so your activity gets called back upon completion of act
  • 如何在 Android TextView 中使用土耳其语字符,如“ş ç ı ö”?

    我想在 android TextView 中写入 ile 但它没有正确绘制 怎样才能使用这样的字符呢 例如 我将文本视图设置为 ile 它显示为 ile 我怎样才能解决这个问题 尝试以下方法 看看是否有帮助 source http grou
  • Android:我可以创建一个不是矩形的视图/画布吗?圆形的?

    我有一个圆形视图 悬停在主要内容上方 gt 从屏幕出来的 z 轴方向 当有人点击屏幕时 我希望选择主要内容或悬停在上方的视图 当它覆盖主视图时 到目前为止效果很好 我在透明画布上有一个圆形物品 这意味着您可以看到该圆圈之外的背景的所有内容
  • Android PhoneGap 插件,UI 选项卡栏,调整 WebView 大小

    我正在创建一个美味的 PhoneGap 插件 希望一旦它能被打开 准备好了 插件基本完成了 我只需要一个漂亮的用户界面 相互作用 简而言之 我想创建一个 本机 android 工具栏组件 如果您实现 PhoneGap UIControls
  • 位图内存不足错误

    我对这个错误有疑问 我从 URL 制作网站图标解析器 我这样做是这样的 public class GrabIconsFromWebPage public static String replaceUrl String url StringB
  • Google 地图删除标记路线上下文菜单

    我使用 Android Studio 的 Google 地图模板启动了一个新项目 并在地图上添加了一个标记 LatLng location new LatLng lat lng Marker marker mMap addMarker ne
  • Android 版 jTwitter 授权错误

    我在我的 Android 应用程序中使用 jTwitter 库 直到前天一切都运转良好 但今天遇到异常 服务提供商响应错误 301 请帮助我 这是堆栈跟踪 02 21 21 07 27 258 E AndroidRuntime 4013 F
  • 在选项卡上保存数据

    我有 3 个选项卡 每个选项卡都有一个单独的活动 我想在用户单击任一选项卡上的 保存 时保存数据 有几个选项可供选择 共享首选项 全局变量或将对象保存在上下文中 编辑 我必须保存图像和文本字段 Android 共享首选项 https sta
  • 使用 Android Studio 进行调试永远停留在“等待调试器”状态

    UPDATE The supposed重复是一个关于陷入 等待调试器 执行时Run 而这个问题就陷入了 等待调试器 执行时Debug 产生问题的步骤不同 解决方案也不同 每当我尝试使用Android Studio的调试功能时 运行状态总是停
  • 即使 Android M 上的移动数据已打开(有连接),也可以通过 WiFi(无连接)发送请求

    我必须在没有互联网连接的情况下将 UDP 数据包发送到 WiFi 模块 配有自己的 AP 但是当我将手机连接到 AP 时 Android 会在移动数据接口上重定向我的数据包 因为它有互联网连接 我使用下面的代码来完成我的工作 但它似乎不适用
  • Glass 语音命令给定列表中最接近的匹配项

    使用 Glass 您可以通过 确定 Glass 菜单启动应用程序 它似乎会选择最接近的匹配项 除非命令相距数英里 并且您可以明显看到命令列表 无论如何 是否可以从应用程序内或从语音提示 在初始应用程序触发后 给出类似的列表并返回最接近的匹配
  • 如何在Android中创建一个简洁的两栏输入表单?

    我想创建一个整洁的两列输入表单 如下所示 到目前为止我的 xml 布局代码
  • 在 Android 手机中通过耳机插孔发送数据

    我目前正在处理一个新项目 我必须通过具有特定电压的耳机插孔发送数据 然后我可以在该电压上工作 所以这里我需要根据我的数据来编程具体电压 我是否可以在android中访问耳机的输出电压 然后创建一个应用程序来控制该电压 这是一篇讨论此问题的
  • 使用 DataBindingComponent 的 Inflate 方法

    当 Glide 成功渲染图像后 我在更新文本视图时看到此错误 致命异常 java lang IllegalStateException 必需 CustomBinding 类中的 DataBindingComponent 为 null 绑定适
  • 使用Intent拨打电话需要权限吗?

    在我的一个应用程序中 我使用以下代码来拨打电话 Intent intent new Intent Intent ACTION CALL Uri parse startActivity intent 文档说我确实需要以下清单许可才能这样做
  • 如何正确编写AttributeSet的XML?

    我想创建一个面板适用于 Android 平台的其他小部件 http code google com p android misc widgets 在运行时 XmlPullParser parser getResources getXml R

随机推荐