Android:Array Adapter 和 ListView,抛出一些奇怪的参考错误?
我正在尝试创建一个自定义消息应用程序。只是一些基本的东西,但我遇到了一些我从未听说过的错误。我花了两天时间才修复它,所以我想我会打扰你们。这是代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView messages = (ListView) findViewById(R.id.messages);
msgList = getSMS();
for(int i = 0; i<msgList.size(); i++) {
System.out.println(msgList.get(i));
}
adapter = new ArrayAdapter<String>(MainActivity.this, 0, R.layout.list_item, msgList);
**messages.setAdapter(adapter);**
messages.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int pos,long id)
{
SmsManager m = SmsManager.getDefault();
String destinationNumber=(String) ((TextView) view).getText();
m.sendTextMessage(destinationNumber, null, destinationNumber, null, null);
}
});
}
public List<String> getSMS() {
List<String> messages = new ArrayList<String>();
Uri uri = Uri.parse("content://sms/inbox");
Cursor c = null;
try{
c = getApplicationContext().getContentResolver().query(uri, null, null ,null,null);
}catch(Exception e){
e.printStackTrace();
}
try{
for (boolean hasData = c.moveToFirst(); hasData; hasData = c.moveToNext()) {
final String address = c.getString(c.getColumnIndex("address"));
final String body = c.getString(c.getColumnIndexOrThrow("body"));
messages.add("Number: " + address + ". Message: " + body);
}
}catch(Exception e){
e.printStackTrace();
}
c.close();
return messages;
}
这是错误:
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): FATAL EXCEPTION: main
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): android.content.res.Resources$NotFoundException: Resource ID #0x0
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.content.res.Resources.getValue(Resources.java:892)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1869)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.content.res.Resources.getLayout(Resources.java:731)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.ListView.onMeasure(ListView.java:1109)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.View.measure(View.java:8171)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.View.measure(View.java:8171)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.View.measure(View.java:8171)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.View.measure(View.java:8171)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.View.measure(View.java:8171)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.ViewRoot.performTraversals(ViewRoot.java:805)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.ViewRoot.handleMessage(ViewRoot.java:1731)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.os.Looper.loop(Looper.java:123)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at java.lang.reflect.Method.invoke(Method.java:521)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at dalvik.system.NativeStart.main(Native Method)
我知道在设置适配器时发生错误,但我检查了消息是否正常检索,并且我似乎找不到发生错误的位置。 非常感谢有关错误的任何想法!
编辑:是的,抱歉,这是 list_item.xml。如果有帮助的话,我有一个类似的程序在 2.1 手机上运行,但在 2.2 手机上抛出此错误。不过,还没有在 2.1 上尝试过这个应用程序。
<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dp"
android:textSize="22sp" >
</TextView>
I am attempting to create a custom messaging app. Just something basic but I am getting some error that I have never heard of. Tried for 2 days to fix it so I thought I would bother you guys with it. This is the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView messages = (ListView) findViewById(R.id.messages);
msgList = getSMS();
for(int i = 0; i<msgList.size(); i++) {
System.out.println(msgList.get(i));
}
adapter = new ArrayAdapter<String>(MainActivity.this, 0, R.layout.list_item, msgList);
**messages.setAdapter(adapter);**
messages.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int pos,long id)
{
SmsManager m = SmsManager.getDefault();
String destinationNumber=(String) ((TextView) view).getText();
m.sendTextMessage(destinationNumber, null, destinationNumber, null, null);
}
});
}
public List<String> getSMS() {
List<String> messages = new ArrayList<String>();
Uri uri = Uri.parse("content://sms/inbox");
Cursor c = null;
try{
c = getApplicationContext().getContentResolver().query(uri, null, null ,null,null);
}catch(Exception e){
e.printStackTrace();
}
try{
for (boolean hasData = c.moveToFirst(); hasData; hasData = c.moveToNext()) {
final String address = c.getString(c.getColumnIndex("address"));
final String body = c.getString(c.getColumnIndexOrThrow("body"));
messages.add("Number: " + address + ". Message: " + body);
}
}catch(Exception e){
e.printStackTrace();
}
c.close();
return messages;
}
And here is the error:
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): FATAL EXCEPTION: main
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): android.content.res.Resources$NotFoundException: Resource ID #0x0
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.content.res.Resources.getValue(Resources.java:892)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1869)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.content.res.Resources.getLayout(Resources.java:731)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.ListView.onMeasure(ListView.java:1109)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.View.measure(View.java:8171)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.View.measure(View.java:8171)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.View.measure(View.java:8171)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.View.measure(View.java:8171)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.View.measure(View.java:8171)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.ViewRoot.performTraversals(ViewRoot.java:805)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.view.ViewRoot.handleMessage(ViewRoot.java:1731)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.os.Looper.loop(Looper.java:123)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at java.lang.reflect.Method.invoke(Method.java:521)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): at dalvik.system.NativeStart.main(Native Method)
I know the error is occurring when I set the adapter, but I have checked the messages are being retrieved fine and I cannot seem to find where the error is occurring.
Any ideas on the error(s) are greatly appreciated!
EDIT: Yep, Sorry here is the list_item.xml. If it helps, I have had a similar program working on a 2.1 phone, but throws this error on 2.2. Haven't tried this app on 2.1, however.
<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dp"
android:textSize="22sp" >
</TextView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将 ArrayAdapter 构造函数行更改为:
在原始行中,您告诉 ArrayAdapter 膨胀 ID 为 0(不存在)的布局,然后找到 ID 为 R.id 的 TextView。列表项。您的 TextView 没有设置 ID,因此您需要使用仅接受 TextView 资源 ID 的构造函数。您将用于更复杂的 ListView 项目的另一个构造函数,其中可能有多个视图。
或者,您可以为 TextView 提供一个 ID,例如“@+id/text”,然后使用:
Try changing your ArrayAdapter constructor line to:
In your original line, you're telling the ArrayAdapter to inflate a layout with an ID of 0 (which doesn't exist), and then find a TextView within with an ID of R.id.list_item. Your TextView doesn't have an ID set, so you need to use the constructor that takes in only the TextView resource ID. The other constructor you would use for more complex ListView items where you may have multiple views within.
Alternately, you could give your TextView an ID, such as "@+id/text", then use: