【问题标题】:ArrayAdapter causing NullPointerArrayAdapter 导致 NullPointer
【发布时间】:2014-01-10 22:11:43
【问题描述】:

所以我有以下代码:

public class TestActivity extends SherlockFragmentActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);

    ...
    ListView lv = (ListView) findViewById(R.id.listView);
    aAdpt = new MyFriendAdapter(friendslist, this);
    lv.setAdapter(aAdpt);


    }
...
}

这是测试布局:

...
<ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/selection_profile_pic" />
...

还有我的自定义适配器:

public class MyFriendAdapter extends ArrayAdapter<Friends>{

    ...

    public MyFriendAdapter(List<Friends> friendList, Context ctx) {
        super(ctx, R.layout.row_friend, friendList);
        this.friendList = friendList;
        this.context = ctx;
    }

    public int getCount() {
        return friendList.size();
    }

    public Friends getItem(int position) {
        return friendList.get(position);
    }

    public long getItemId(int position) {
         return friendList.get(position).hashCode();
    }



    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ....
    return v;
    }
}

根据我的 logcat,我认为异常来自 getCount,但我不知道为什么......

01-10 22:32:08.037: E/AndroidRuntime(25486): FATAL EXCEPTION: main
01-10 22:32:08.037: E/AndroidRuntime(25486): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.TestActivity}: java.lang.NullPointerException
01-10 22:32:08.037: E/AndroidRuntime(25486):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-10 22:32:08.037: E/AndroidRuntime(25486):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-10 22:32:08.037: E/AndroidRuntime(25486):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-10 22:32:08.037: E/AndroidRuntime(25486):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-10 22:32:08.037: E/AndroidRuntime(25486):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 22:32:08.037: E/AndroidRuntime(25486):    at android.os.Looper.loop(Looper.java:137)
01-10 22:32:08.037: E/AndroidRuntime(25486):    at android.app.ActivityThread.main(ActivityThread.java:4424)
01-10 22:32:08.037: E/AndroidRuntime(25486):    at java.lang.reflect.Method.invokeNative(Native Method)
01-10 22:32:08.037: E/AndroidRuntime(25486):    at java.lang.reflect.Method.invoke(Method.java:511)
01-10 22:32:08.037: E/AndroidRuntime(25486):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
01-10 22:32:08.037: E/AndroidRuntime(25486):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
01-10 22:32:08.037: E/AndroidRuntime(25486):    at dalvik.system.NativeStart.main(Native Method)
01-10 22:32:08.037: E/AndroidRuntime(25486): Caused by: java.lang.NullPointerException
01-10 22:32:08.037: E/AndroidRuntime(25486):    at com.example.myfirstapp.MyFriendAdapter.getCount(MyFriendAdapter.java:29)
01-10 22:32:08.037: E/AndroidRuntime(25486):    at android.widget.ListView.setAdapter(ListView.java:460)
01-10 22:32:08.037: E/AndroidRuntime(25486):    at com.example.myfirstapp.TestActivity.onCreate(TestActivity.java:68)
01-10 22:32:08.037: E/AndroidRuntime(25486):    at android.app.Activity.performCreate(Activity.java:4465)
01-10 22:32:08.037: E/AndroidRuntime(25486):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-10 22:32:08.037: E/AndroidRuntime(25486):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-10 22:32:08.037: E/AndroidRuntime(25486):    ... 11 more

我真的不知道它可能来自哪里.. 有什么帮助吗?

【问题讨论】:

    标签: android arraylist nullpointerexception


    【解决方案1】:

    可能friendslist 在这一行中为空:

    aAdpt = new MyFriendAdapter(friendslist, this);
    

    【讨论】:

    • 嗯,谢谢 :'( 找了一段时间,我忘了取消注释某些行 :'(
    【解决方案2】:

    如果您查看堆栈跟踪,您将获得 com.example.myfirstapp.MyFriendAdapter.getCount

    com.example.myfirstapp.MyFriendAdapter.getCount
    

    ...所以你的列表是 null

    【讨论】:

      【解决方案3】:

      您的 Listview 为空。 首先检查你的布局文件,

      ListView lv = (ListView) findViewById(R.id.listView); lv 在这里为空。

      检查你的 R 的导入语句。它指向了错误的 R.java 文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多