本文介绍了Android ListView异常问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从字符串数组中填充listView,但是我不断得到以下非常长和复杂的异常报告: 02-16 20:00:24.119:E / AndroidRuntime(17608):FATAL EXCEPTION:main
02-16 20:00:24.119:E / AndroidRuntime(17608):android.content.res.Resources $ NotFoundException:资源ID#0x7f080003类型#0x12无效
02-16 20:00:24.119:E / AndroidRuntime(17608):在android.content.res.Resources.loadXmlResourceParser(Resources.java:1874)
02-16 20:00:24.119:E / AndroidRuntime(17608):android.content.res.Resources.getLayout(Resources.java:731)
02-16 20:00:24.119:E / AndroidRuntime (17608):在android.view.LayoutInflater.inflate(LayoutInflater.java:318)
02-16 20:00:24.119:E / AndroidRuntime(17608):在android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter。 java:332)
02-16 20:00:24.119:E / AndroidRuntime(17608):android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
02-16 20:00: 24.119: E / AndroidRuntime(17608):在android.widget.AbsListView.obtainView(AbsListView.java:1315)
02-16 20:00:24.119:E / AndroidRuntime(17608):在android.widget.ListView.measureHeightOfChildren (ListView.java:1198)
02-16 20:00:24.119:E / AndroidRuntime(17608):在android.widget.ListView.onMeasure(ListView.java:1109)
02-16 20 :00:24.119:E / AndroidRuntime(17608):在android.view.View.measure(View.java:8172)
02-16 20:00:24.119:E / AndroidRuntime(17608):在android。 view.ViewGroup.measureChildWithMargins(ViewGroup.java:3140)
02-16 20:00:24.119:E / AndroidRuntime(17608):在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
02-16 20:00:24.119:E / AndroidRuntime(17608):在android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
02-16 20:00:24.119:E / AndroidRuntime(17608 ):at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
02-16 20:00:24.119:E / AndroidRuntime(17608):在android.view.View.meas ure(View.java:8172)
02-16 20:00:24.119:E / AndroidRuntime(17608):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3140)
02-16 20:00:24.119:E / AndroidRuntime(17608):android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-16 20:00:24.119:E / AndroidRuntime(17608):android .view.View.measure(View.java:8172)
02-16 20:00:24.119:E / AndroidRuntime(17608):在android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
02-16 20:00:24.119:E / AndroidRuntime(17608):android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
02-16 20:00:24.119:E / AndroidRuntime 17608):在android.view.View.measure(View.java:8172)
02-16 20:00:24.119:E / AndroidRuntime(17608):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java :3140)
02-16 20:00:24.119:E / AndroidRuntime(17608):在android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-16 20:00:24.119 :E / AndroidRuntime(17608):在android.vi ew.View.measure(View.java:8172)
02-16 20:00:24.119:E / AndroidRuntime(17608):在android.view.ViewRoot.performTraversals(ViewRoot.java:805)
02-16 20:00:24.119:E / AndroidRuntime(17608):在android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
02-16 20:00:24.119:E / AndroidRuntime(17608 ):在android.os.Handler.dispatchMessage(Handler.java:99)
02-16 20:00:24.119:E / AndroidRuntime(17608):在android.os.Looper.loop(Looper.java: 144)
02-16 20:00:24.119:E / AndroidRuntime(17608):android.app.ActivityThread.main(ActivityThread.java:4937)
02-16 20:00:24.119: E / AndroidRuntime(17608):java.lang.reflect.Method.invokeNative(Native Method)
02-16 20:00:24.119:E / AndroidRuntime(17608):java.lang.reflect.Method。 invoke(Method.java:521)
02-16 20:00:24.119:E / AndroidRuntime(17608):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:858)
02-16 20:00:24.119:E / AndroidRuntime(17608):at com.android.inter nal.os.ZygoteInit.main(ZygoteInit.java:616)
02-16 20:00:24.119:E / AndroidRuntime(17608):在dalvik.system.NativeStart.main(Native Method)
02-16 20:00:25.789:I / Process(17608):发送信号。 PID:17608 SIG:9
以下是我的Activity类:
public class ArticlesActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
// TODO自动生成的方法stub
super.onCreate(savedInstanceState);
setContentView(R.layout.articles);
String [] items = {line#1,line#2,line#3,line#4};
ListView articlesList =(ListView)findViewById(R.id.articlesList);
// ArrayAdapter< String> adapter = new ArrayAdapter< String>(this,R.layout.article_item,items);
// ArrayAdapter< String> adapter = new ArrayAdapter< String>(this,R.layout.article_item,R.id.articlesList,items);
// ArrayAdapter< String> adapter = new ArrayAdapter< String>(this,R.layout.articles,R.id.articleItem,items);
ArrayAdapter< String> adapter = new ArrayAdapter< String>(this,R.id.articleItem,items);
articlesList.setAdapter(adapter);
}
}
这是我的ArticlesList.xml: / p>
<?xml version =1.0encoding =utf-8?>
< LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
android:id =@ + id / articlesContainer
android:layout_width =match_parent
android:layout_height =wrap_content
android:orientation =vertical>
< ListView
android:id =@ + id / articlesList
android:layout_width =match_parent
android:layout_height =wrap_content>
< / ListView>
< / LinearLayout>
这是我的Article_item.xml:
<?xml version =1.0encoding =utf-8?>
< LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =fill_parent
android:layout_height =match_parent
android:gravity =leftright
android:orientation =vertical>
< TextView
android:id =@ + id / articleItem
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =TextView/>
< / LinearLayout>
<! - < TextView xmlns:android =http://schemas.android.com/apk/res/android
android:id =@ + id / articleItem
android:layout_width =fill_parent
android:layout_height =wrap_content/> - >
问题反映了我的情况,我尝试调整我的Article_item.xml只有一个TextView(被注释掉),但它做了没有区别。
解决方案
尝试您的xml Article_item.xml b
<?xml version =1.0encoding =utf-8?>
< TextView xmlns:android =http://schemas.android.com/apk/res/android
android:id =@ + id / TV_articleItem
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =TextView/>
并在您的活动中使用此
String [] items = {line#1,line#2,line#3,line#4};
ListView articlesList =(ListView)findViewById(R.id.articlesList);
ArrayAdapter< String> adapter = new ArrayAdapter< String>(this,R.layout.Article_item,R.id.TV_articleItem,items);
articlesList.setAdapter(adapter);
I am trying populate a listView from a string array but I keep getting the following very long and complex exception report:
02-16 20:00:24.119: E/AndroidRuntime(17608): FATAL EXCEPTION: main
02-16 20:00:24.119: E/AndroidRuntime(17608): android.content.res.Resources$NotFoundException: Resource ID #0x7f080003 type #0x12 is not valid
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1874)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.content.res.Resources.getLayout(Resources.java:731)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.widget.ListView.onMeasure(ListView.java:1109)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.view.View.measure(View.java:8172)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3140)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.view.View.measure(View.java:8172)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3140)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.view.View.measure(View.java:8172)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.view.View.measure(View.java:8172)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3140)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.view.View.measure(View.java:8172)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.view.ViewRoot.performTraversals(ViewRoot.java:805)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.os.Looper.loop(Looper.java:144)
02-16 20:00:24.119: E/AndroidRuntime(17608): at android.app.ActivityThread.main(ActivityThread.java:4937)
02-16 20:00:24.119: E/AndroidRuntime(17608): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 20:00:24.119: E/AndroidRuntime(17608): at java.lang.reflect.Method.invoke(Method.java:521)
02-16 20:00:24.119: E/AndroidRuntime(17608): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-16 20:00:24.119: E/AndroidRuntime(17608): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-16 20:00:24.119: E/AndroidRuntime(17608): at dalvik.system.NativeStart.main(Native Method)
02-16 20:00:25.789: I/Process(17608): Sending signal. PID: 17608 SIG: 9
The following is my Activity class:
public class ArticlesActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.articles);
String[] items = {"line#1", "line#2", "line#3", "line#4"};
ListView articlesList = (ListView)findViewById(R.id.articlesList);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.article_item, items);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.article_item, R.id.articlesList, items);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.articles, R.id.articleItem, items);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.articleItem, items);
articlesList.setAdapter(adapter);
}
}
Here's my ArticlesList.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/articlesContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/articlesList"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
And here's my Article_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="left|right"
android:orientation="vertical" >
<TextView
android:id="@+id/articleItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<!-- <TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/articleItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> -->
This question mirrors my situation and I tried adjusting my Article_item.xml to have just a TextView (which is commented out) but it made no difference.
解决方案
Try your xml Article_item.xml like this
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TV_articleItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
and use this in your activity
String[] items = {"line#1", "line#2", "line#3", "line#4"};
ListView articlesList = (ListView)findViewById(R.id.articlesList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.Article_item, R.id.TV_articleItem, items);
articlesList.setAdapter(adapter);
这篇关于Android ListView异常问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!