我搜索了其他主题并得出结论,我的listview是空指针异常的原因,但我无论如何都无法修复它。
这是我的XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/instantme_check_list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2.88" >
</ListView>
将剩下的部分排除在外,以减少杂乱,我的代码是
public class instantme_check extends Activity {
InputStream is = null;
TextView name;
TextView content;
EditText entername;
EditText entercontent;
private ListView list;
...
name = (TextView) findViewById(R.id.instantme_check_name);
content = (TextView) findViewById(R.id.instantme_check_message);
entername = (EditText) findViewById(R.id.instantme_check_entername);
entercontent = (EditText) findViewById(R.id.instantme_check_entermessage);
list = (ListView) findViewById(R.id.instantme_check_list);
...
listAdapter4 = new displayadapter4();
list.setAdapter(listAdapter4);
至于显示适配器类(我已经在代码的其他两个部分使用它,它工作,所以我认为它很好)
public class displayadapter4 extends ArrayAdapter<String> {
displayadapter4()
{
super(instantme_check.this, R.layout.row, R.id.label, course);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = super.getView(position, convertView, parent);
TextView size1 = (TextView) row.findViewById(R.id.size);
size1.setText("Subject: " + subject[position]);
TextView size2 = (TextView) row.findViewById(R.id.size2);
size2.setText("Message: " + message[position]);
TextView size3 = (TextView) row.findViewById(R.id.size3);
size3.setText("From: " + user[position]);
return(row);
}
}
它一直给出nullpointer异常,虽然我放入arrayadapter的数据我确定没问题,因为我使用了Log.i来验证它。
至于我的错误日志
04-07 00:33:10.714: W/dalvikvm(273): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-07 00:33:10.734: E/AndroidRuntime(273): FATAL EXCEPTION: main
04-07 00:33:10.734: E/AndroidRuntime(273): java.lang.NullPointerException
04-07 00:33:10.734: E/AndroidRuntime(273): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
04-07 00:33:10.734: E/AndroidRuntime(273): at com.Assist.assistme.instantme_check$displayadapter4.getView(instantme_check.java:172)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.widget.ListView.makeAndAddView(ListView.java:1727)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.widget.ListView.fillDown(ListView.java:652)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.widget.ListView.fillFromTop(ListView.java:709)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.widget.ListView.layoutChildren(ListView.java:1580)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.view.View.layout(View.java:7035)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.view.View.layout(View.java:7035)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.view.View.layout(View.java:7035)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.view.View.layout(View.java:7035)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.view.View.layout(View.java:7035)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.os.Looper.loop(Looper.java:123)
04-07 00:33:10.734: E/AndroidRuntime(273): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-07 00:33:10.734: E/AndroidRuntime(273): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 00:33:10.734: E/AndroidRuntime(273): at java.lang.reflect.Method.invoke(Method.java:521)
04-07 00:33:10.734: E/AndroidRuntime(273): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-07 00:33:10.734: E/AndroidRuntime(273): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-07 00:33:10.734: E/AndroidRuntime(273): at dalvik.system.NativeStart.main(Native Method)
04-07 00:33:12.884: D/dalvikvm(289): GC_EXTERNAL_ALLOC freed 806 objects / 57952 bytes in 66ms
对于这篇真正冗长的帖子感到抱歉。提前谢谢。
编辑: row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textSize="20sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="10sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/size2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:textSize="10sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/size3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="10sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
答案 0 :(得分:0)
根据您的LogCat:
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
问题是你传递给ArrayAdapter的超级构造函数的View id,这里:
super(instantme_check.this, R.layout.row, R.id.label, course);
您的布局中不存在,您可以通读source code来查看导致NullPointerException的确切原因和原因。
解决方案很简单:使用不同的ID或将此TextView添加到您的布局中。