在 myListView.setAdapter(aa); 的第 42 行获取 nullpointerexception;该代码应该可以工作,但无法真正看到阻止适配器填充的内容。如果我注释掉 arrayadapter 部分,它可以工作,但不能使用它,所以任何帮助将不胜感激。
我尝试将布局更改为仅一个 ListView,并让该类扩展 ListActivity,但这也不起作用。
package ie.ucc.bis.is4432calculator;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class secondActivity extends Activity {
protected double latestResult = 0.0;
protected TextView Results;
public String ResultString;
ArrayList<String> ListItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
Results= (TextView)findViewById(R.id.textView1);
Intent i = this.getIntent(); /*delete "this."*/
latestResult = i.getDoubleExtra("latestResult", 0.0);
Results.setText(String.valueOf(latestResult));
ListItems = i.getStringArrayListExtra("ListItems");
Results.setText(getResultString());
ListView myListView = (ListView)findViewById(R.id.listView1);
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,ListItems);
myListView.setAdapter(aa);
}
public void setResultString(String ResultString) {
this.ResultString = ResultString;
}
public String getResultString(){
return ResultString;
}
}
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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
/>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
不太确定出了什么问题,我假设 ArrayAdapter 不会填充,但不能真正理解为什么该代码在不同的项目中有效,但不是在这里。Logcat 结果发布在下面。
03-15 16:11:12.104: E/AndroidRuntime(844): FATAL EXCEPTION: main
03-15 16:11:12.104: E/AndroidRuntime(844): java.lang.RuntimeException: Unable to start activity ComponentInfo{ie.ucc.bis.is4432calculator/ie.ucc.bis.is4432calculator.secondActivity}: java.lang.NullPointerException
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.os.Handler.dispatchMessage(Handler.java:99)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.os.Looper.loop(Looper.java:123)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-15 16:11:12.104: E/AndroidRuntime(844): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 16:11:12.104: E/AndroidRuntime(844): at java.lang.reflect.Method.invoke(Method.java:507)
03-15 16:11:12.104: E/AndroidRuntime(844): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-15 16:11:12.104: E/AndroidRuntime(844): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-15 16:11:12.104: E/AndroidRuntime(844): at dalvik.system.NativeStart.main(Native Method)
03-15 16:11:12.104: E/AndroidRuntime(844): Caused by: java.lang.NullPointerException
03-15 16:11:12.104: E/AndroidRuntime(844): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:291)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.widget.ListView.setAdapter(ListView.java:454)
03-15 16:11:12.104: E/AndroidRuntime(844): at ie.ucc.bis.is4432calculator.secondActivity.onCreate(secondActivity.java:42)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-15 16:11:12.104: E/AndroidRuntime(844): ... 11 more
谢谢你的帮助