C# Android资源$NotFoundException:资源ID#0x7f030001具有新布局
我正在尝试为我自己的列表使用适配器,但在布局中添加了listview_row.axml(如下所示),并在尝试运行应用程序时进行了调试,从emulator收到消息“不幸的是,Android应用程序已停止”和Visual Studio中未处理的异常:C# Android资源$NotFoundException:资源ID#0x7f030001具有新布局,c#,android,xamarin,android-emulator,C#,Android,Xamarin,Android Emulator,我正在尝试为我自己的列表使用适配器,但在布局中添加了listview_row.axml(如下所示),并在尝试运行应用程序时进行了调试,从emulator收到消息“不幸的是,Android应用程序已停止”和Visual Studio中未处理的异常:System.NullReferenceException:Object reference未设置为对象的实例。和Android资源$NotFoundException:Resource ID#0x7f030001 不确定这里出了什么问题: MyList
System.NullReferenceException:Object reference未设置为对象的实例。
和Android资源$NotFoundException:Resource ID#0x7f030001
不确定这里出了什么问题:
MyListWieAdapter.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace Android_App1
{
class MyListViewAdapter : BaseAdapter<string>
{
private List<string> mItems;
private Context mContext;
public MyListViewAdapter(Context context, List<string> items)
{
mItems = items;
mContext = context;
}
public override int Count
{
get { return mItems.Count; }
}
public override long GetItemId(int position)
{
return position;
}
public override string this[int position]
{
get { return mItems[position]; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if(row == null)
{
row = LayoutInflater.From(mContext).Inflate(Resource.Layout.listview_row, null, false);
}
TextView textName = row.FindViewById<TextView>(Resource.Id.textName);
textName.Text = mItems[position];
return row;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Android.App;
使用Android.Content;
使用Android.OS;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
名称空间Android_App1
{
类MyListViewAdapter:BaseAdapter
{
私人名单;
私有上下文;
公共MyListViewAdapter(上下文、列表项)
{
mItems=项目;
mContext=上下文;
}
公共覆盖整数计数
{
获取{return mItems.Count;}
}
公共覆盖长GetItemId(int位置)
{
返回位置;
}
公共重写字符串此[int位置]
{
获取{return mItems[position];}
}
公共覆盖视图GetView(int位置、视图转换视图、视图组父视图)
{
视图行=转换视图;
if(行==null)
{
row=LayoutInflater.From(mContext).充气(Resource.Layout.listview_row,null,false);
}
TextView textName=row.FindViewById(Resource.Id.textName);
Text name.Text=mItems[position];
返回行;
}
}
}
MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
namespace Android_App1
{
[Activity(Label = "Android_App1", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private List<string> mItems;
private ListView mlistView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
mlistView = FindViewById<ListView>(Resource.Id.listView1);
mItems = new List<string>();
mItems.Add("Name1");
mItems.Add("Name2");
mItems.Add("Name3");
MyListViewAdapter adapter = new MyListViewAdapter(this, mItems);
string indexerTest = adapter[1];
mlistView.Adapter = adapter;
}
}
}
使用系统;
使用Android.App;
使用Android.Content;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Android.OS;
使用System.Collections.Generic;
名称空间Android_App1
{
[活动(Label=“Android_App1”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共课活动:活动
{
私人名单;
私有列表视图;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
mlistView=findviewbyd(Resource.Id.listView1);
mItems=新列表();
添加(“名称1”);
添加(“名称2”);
添加(“名称3”);
MyListViewAdapter=新的MyListViewAdapter(此为mItems);
字符串索引器测试=适配器[1];
mlistView.Adapter=适配器;
}
}
}
并在布局文件夹中:
listview_row.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textName" />
</LinearLayout>
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView1" />
</LinearLayout>
你是否调试了你的代码以找出是什么引发了NRE?@Jon Douglas您好,是的,但这是我得到的全部,给出了一个例外。也许我需要调查一下logcat。由于我是xamarin和android的新手,不确定我要检查什么,以及如何读取设备日志,可能是从xamarin构建或xamarin诊断输出,以确定我已经完全复制了您的代码,它工作正常。请参考@Mike Ma Hello,我的模拟器可能有问题,现在它被检查为Android_Accelerated_x86(Android 6.0-API 23),因为你的代码完全相同,所以我可能应该安装Xamarin Android Player developer.Xamarin.com/releases/Android/Android-Player,但即使这样,错误的内容看起来不一样,比如,如果某个内容为null或刚刚超出设置,则无法获取what@nikorio我没有安装Xamarin Android播放器,请注意Xamarin Android播放器已被弃用。我正在使用android sdk中的google emulator。