java.lang.RuntimeException: Unable to start activi

2019-01-23 13:23发布

问题:

*I am trying to add items to the list view dynamically. But, I am getting a run time exception.Please help. Is there a problem with the global variable "values"? I tried to debug by printing the logs and I am able to see the values stored in the variable *

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);
}

Here's the error list

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)

回答1:

This particular exception ("storage == null") is thrown when the array parameter passed to ArrayAdapter(Context, int, T[]) is null. (To be more specific, it's thrown from Arrays.java when it tries to create a new ArrayList wrapped around your null array.)

There should be no problem with the variable values itself, but are you absolutely sure that values is not null at the point you're passing it into the ArrayAdapter constructor? Just below the line

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

and just above the constructor call, add something like this:

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

and see if it really is what it's supposed to be.