2013-05-27 108 views
18

* 我試圖動態地將項目添加到列表視圖。但是,我得到一個運行時異常。請幫助。 全局變量「值」有問題嗎? 我試圖通過打印日誌來調試,我能夠看到存儲在變量*java.lang.RuntimeException:無法啓動活動ComponentInfo:java.lang.NullPointerException:storage == null

public class FriendsActivity extends ListFragment implements TabListener { 
     private Fragment mFragment; 
     private String[] values; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getActivity().setContentView(R.layout.activity_friends); 
    } 

@SuppressWarnings("deprecation") 
public void onActivityCreated(Bundle savedInstanceState) { 
    setHasOptionsMenu(true); 
    super.onActivityCreated(savedInstanceState); 
    String su = User.getLoggedInUsername(); 
    friends.query(friends.class, new StackMobQuery().field(new StackMobQueryField("uname").isEqualTo(su)), new StackMobQueryCallback<friends>(){ 

    @Override 
    public void failure(StackMobException arg0) { 
     // TODO Auto-generated method stub 

    } 

    public void success(List<friends> arg0) { 
     // TODO Auto-generated method stub 
     if(arg0.size()>0){ 
      FriendsActivity.this.values = new String[arg0.size()]; 
      for(int i=0;i<arg0.size();i++){ 
       friends us = arg0.get(i); 
       values[i] = us.getUname(); 
      } 
      for(int i=0;i<values.length;i++) 
       Log.i("Friends",values[i]); 
     } 
      else{ 
       values = new String[]{"xyz","abc","dbz"}; 

      } 
     } 

    }); 
    //String uname=User.getLoggedInUsername(); 
    //Log.i("User Name",uname); 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), 
      android.R.layout.simple_list_item_1, values); 
     setListAdapter(adapter); 
} 

這裏的值是

05-27 16:42:03.667: E/Trace(814): error opening trace file: No such file or directory  (2) 
05-27 16:46:17.718: E/AndroidRuntime(814): FATAL EXCEPTION: main 
05-27 16:46:17.718: E/AndroidRuntime(814): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.cloudchat/com.myapp.cloudchat.HomeScreenActivity}: java.lang.NullPointerException: storage == null 
05-27 16:46:17.718: E/AndroidRuntime(814): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
05-27 16:46:17.718: E/AndroidRuntime(814): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
05-27 16:46:17.718: E/AndroidRuntime(814): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
05-27 16:46:17.718: E/AndroidRuntime(814): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
05-27 16:46:17.718: E/AndroidRuntime(814): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-27 16:46:17.718: E/AndroidRuntime(814): at android.os.Looper.loop(Looper.java:137) 
05-27 16:46:17.718: E/AndroidRuntime(814): at android.app.ActivityThread.main(ActivityThread.java:5041) 
05-27 16:46:17.718: E/AndroidRuntime(814): at java.lang.reflect.Method.invokeNative(Native Method) 
05-27 16:46:17.718: E/AndroidRuntime(814): at java.lang.reflect.Method.invoke(Method.java:511) 
05-27 16:46:17.718: E/AndroidRuntime(814): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
05-27 16:46:17.718: E/AndroidRuntime(814): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
05-27 16:46:17.718: E/AndroidRuntime(814): at dalvik.system.NativeStart.main(Native Method) 
05-27 16:46:17.718: E/AndroidRuntime(814): Caused by: java.lang.NullPointerException: storage == null 
05-27 16:46:17.718: E/AndroidRuntime(814): at java.util.Arrays$ArrayList.<init>(Arrays.java:38) 
05-27 16:46:17.718: E/AndroidRuntime(814): at java.util.Arrays.asList(Arrays.java:154) 
05-27 16:46:17.718: E/AndroidRuntime(814): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128) 
05-27 16:46:17.718: E/AndroidRuntime(814): at com.myapp.cloudchat.FriendsActivity.onActivityCreated(FriendsActivity.java:74) 
05-27 16:46:17.718: E/AndroidRuntime(814): at android.app.Fragment.performActivityCreated(Fragment.java:1703) 
05-27 16:46:17.718: E/AndroidRuntime(814): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903) 
05-27 16:46:17.718: E/AndroidRuntime(814): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057) 
05-27 16:46:17.718: E/AndroidRuntime(814): at android.app.BackStackRecord.run(BackStackRecord.java:682) 
05-27 16:46:17.718: E/AndroidRuntime(814): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435) 
05-27 16:46:17.718: E/AndroidRuntime(814): at android.app.Activity.performStart(Activity.java:5113) 
05-27 16:46:17.718: E/AndroidRuntime(814): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153) 
05-27 16:46:17.718: E/AndroidRuntime(814): ... 11 more 
05-27 16:46:20.428: E/Trace(866): error opening trace file: No such file or directory (2) 

回答

29

這種特殊的例外("storage == null")被拋出時,錯誤列表傳遞給ArrayAdapter(Context, int, T[])的數組參數爲null。 (更具體地講,它是從Arrays.java拋出時,它試圖創建一個新的ArrayList纏着你空數組)。

應該有與可變values本身沒有問題,但你絕對相信values是在你將它傳遞給構造函數ArrayAdapter的時候不是null?只是線下

//Log.i("User Name",uname); 

,只是上面的構造函數調用,添加如下內容:

Log.i("FriendsActivity", "values = <<" + values + ">>"); 

,看看它是否真的是什麼它應該是。

+1

這應該是接受的答案 – Nactus

相關問題