18

*我正在尝试将项目动态添加到列表视图中。但是,我遇到了运行时异常。请帮忙。全局变量“值”有问题吗?我尝试通过打印日志进行调试,并且能够看到存储在变量 * 中的值

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)
4

1 回答 1

29

"storage == null"当传递给的数组参数ArrayAdapter(Context, int, T[])为空时,将引发此特定异常 ( )。(更具体地说,它是从 Arrays.java 中抛出的,当它试图创建一个新的ArrayList包装在你的空数组周围时。)

变量本身应该没有问题values,但是您绝对确定在将其传递给构造函数values时它不为空吗?ArrayAdapter就在这条线的下方

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

在构造函数调用之上,添加如下内容:

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

看看它是否真的是它应该是的。

于 2013-07-26T16:59:27.987 回答