使用ArrayAdapter时出现空指针异常

时间:2014-04-27 09:40:34

标签: android nullpointerexception adapter android-arrayadapter

我的应用程序因代码中ArrayAdaptor行的空指针异常而停止。 它是ArrayAdaptor的基本实现。它应该根据net上提供的教程工作。数组不为null我能够打印它们。无法弄清楚为什么会出现此错误。任何帮助表示赞赏。 我正在粘贴下面的相关代码。

使用适配器显示数据的类:

@Override
ListView l;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_displayinvestment);
    context = this;
    l = (ListView) findViewById(R.id.listView1);

}

public void displayArr(String[] arr) {

    System.out.println(arr);

    Log.d("MYAPP in displayinvestment", arr[2]);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1, arr);

    l.setAdapter(adapter);
}

}

Displayactivity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DisplayInvestment" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="95dp" >
</ListView>

LogCat输出:

04-27 09:32:34.610: I/System.out(1464): **Array being passed to the display method[Ljava.lang.String;@536ae1ec**

04-27 09:32:34.610: I/System.out(1464): [Ljava.lang.String;@536ae1ec
**04-27 09:32:34.614: D/MYAPP: array in the display method(1464): test3**

04-27 09:32:34.614: D/AndroidRuntime(1464): Shutting down VM
04-27 09:32:34.618: W/dalvikvm(1464): threadid=1: thread exiting with uncaught exception (group=0xa6292288)
04-27 09:32:34.622: E/AndroidRuntime(1464): FATAL EXCEPTION: main
04-27 09:32:34.622: E/AndroidRuntime(1464): java.lang.IllegalStateException: Could not execute method of the activity
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.view.View$1.onClick(View.java:3591)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.view.View.performClick(View.java:4084)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.view.View$PerformClick.run(View.java:16966)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.os.Handler.handleCallback(Handler.java:615)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.os.Looper.loop(Looper.java:137)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.app.ActivityThread.main(ActivityThread.java:4745)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at java.lang.reflect.Method.invokeNative(Native Method)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at java.lang.reflect.Method.invoke(Method.java:511)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at dalvik.system.NativeStart.main(Native Method)
04-27 09:32:34.622: E/AndroidRuntime(1464): Caused by: java.lang.reflect.InvocationTargetException
04-27 09:32:34.622: E/AndroidRuntime(1464):     at java.lang.reflect.Method.invokeNative(Native Method)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at java.lang.reflect.Method.invoke(Method.java:511)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.view.View$1.onClick(View.java:3586)
04-27 09:32:34.622: E/AndroidRuntime(1464):     ... 11 more

**04-27 09:32:34.622: E/AndroidRuntime(1464): Caused by: java.lang.NullPointerException**
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at com.example.portfoliomanagement.DisplayInvestment.displayArr(DisplayInvestment.java:34)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at com.example.portfoliomanagement.InvestmentPage.getdata(InvestmentPage.java:63)
04-27 09:32:34.622: E/AndroidRuntime(1464):     ... 14 more

2 个答案:

答案 0 :(得分:1)

context由于某种原因是null

最可能的原因是您使用new实例化了您无法执行的活动 - 对于初学者,不会调用onCreate()生命周期回调。

要实例化活动,请使用Intent

答案 1 :(得分:0)

04-27 09:32:34.622: E/AndroidRuntime(1464): Caused by: java.lang.NullPointerException
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)

ArrayAdapter.java 的第310行如下:

mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

这意味着context为空。