2014-01-10 75 views
0

所以我有以下代碼:ArrayAdapter導致空指針

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

大概friendslist是在這一行空:

aAdpt = new MyFriendAdapter(friendslist, this); 
+0

非常感謝:'(一直在尋找一些時間,我忘記取消註釋某行:'( –

0

,如果你看看你所得到的com.example.myfirstapp.MyFriendAdapter.getCount的堆棧跟蹤

com.example.myfirstapp.MyFriendAdapter.getCount 

。 ..so你的名單是null

-1

您列表視圖爲空。 首先檢查你的佈局文件,

ListView lv =(ListView)findViewById(R.id.listView); lv在這裏爲null。

檢查您的導入語句R.它指向錯誤的R.java文件。