所以我有以下代码:
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
我真的不知道它可能来自哪里.. 有什么帮助吗?
答案 0 :(得分:0)
此行中可能friendslist
为空:
aAdpt = new MyFriendAdapter(friendslist, this);
答案 1 :(得分:0)
如果您查看堆栈跟踪,那么您将获得com.example.myfirstapp.MyFriendAdapter.getCount
com.example.myfirstapp.MyFriendAdapter.getCount
...所以你的清单是 null 。
答案 2 :(得分:-1)
您的Listview为空。 首先检查你的布局文件,
ListView lv =(ListView)findViewById(R.id.listView); lv在这里为空。
检查你的导入语句R.It指向错误的R.java文件。