通过intent发送字符串数组时出现Nullpointer异常

时间:2014-05-14 09:13:42

标签: java android arrays android-intent nullpointerexception

因此,在我的Android应用程序中,用户登录主要活动,然后他们继续主页活动并执行应用程序主要功能。此活动创建一个字符串列表,当用户单击注销按钮时,该列表将通过intent发送。在通过intent发送之前,我将列表转换为数组。然后在我的主要活动上有另一个按钮"检索数据"它启动了另一个活动,但它只是一个ListView。所以我想再次输入字符串数组来检索数据,这样用户就可以点击butoon并查看列表数据。 这是我从第一个发送字符串数组的地方:

btnLogout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) 
            {
                String[] resultsArray = new String[userResults.size()];
                Intent intentLogout=new Intent(getApplicationContext(),MainActivity.class);
                intentLogout.putExtra("Results", resultsArray);
                startActivity(intentLogout);
            }
            });

以下是我收到的信息并在我的主要活动中再次发送:

Bundle extras = getIntent().getExtras();
         if(extras!=null)
         {
            results = extras.getStringArray("Results");
         }
         else
         {
             Toast.makeText(getApplicationContext(), "No data to retrieve yet", Toast.LENGTH_LONG).show();
         }

 btnRetrieveData.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) 
            {
                Intent intentRD=new Intent(getApplicationContext(),RetrieveDataActivity.class);
                intentRD.putExtra("UserResults", results);
                startActivity(intentRD);
            }
            });

这里是我的retrieveDataActivity,我之前没有使用过Listviews,所以我不确定我是否犯了任何明显/愚蠢的错误:

public class RetrieveDataActivity extends Activity 
{
    String [] values;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listviewexampleactivity);

        // Get ListView object
        listView = (ListView) findViewById(R.id.list);

        // Defined Array values to show in ListView
        Bundle extras = getIntent().getExtras();
         if(extras!=null)
         {
            values = extras.getStringArray("UserResults");
         }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, values);


        // Assign adapter to ListView
        listView.setAdapter(adapter); 

        // ListView Item Click Listener
        listView.setOnItemClickListener(new OnItemClickListener() {

              @Override
              public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
              {

                   // ListView Clicked item index
                   int itemPosition = position;

                   // ListView Clicked item value
                   String  itemValue = (String) listView.getItemAtPosition(position);

                    // Show Alert 
                    Toast.makeText(getApplicationContext(),"Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG) .show();

              }

         }); 
    }

}

最后是logcat:

05-14 04:59:36.396: E/AndroidRuntime(28254): FATAL EXCEPTION: main
05-14 04:59:36.396: E/AndroidRuntime(28254): Process: com.gmail.Sheridjohn.letterchecker, PID: 28254
05-14 04:59:36.396: E/AndroidRuntime(28254): java.lang.NullPointerException
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.widget.AbsListView.obtainView(AbsListView.java:2263)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.widget.ListView.onMeasure(ListView.java:1175)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.view.View.measure(View.java:16497)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.view.View.measure(View.java:16497)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.view.View.measure(View.java:16497)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.view.View.measure(View.java:16497)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.view.Choreographer.doFrame(Choreographer.java:544)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.os.Handler.handleCallback(Handler.java:733)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.os.Handler.dispatchMessage(Handler.java:95)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.os.Looper.loop(Looper.java:136)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at android.app.ActivityThread.main(ActivityThread.java:5017)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at java.lang.reflect.Method.invokeNative(Native Method)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at java.lang.reflect.Method.invoke(Method.java:515)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-14 04:59:36.396: E/AndroidRuntime(28254):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

好的,问题是从意图

获取数组列表的位置

尝试

 Intent intent = new Intent(this, MainActivity.class);
 values=extras.getStringArrayExtra("UserResults");

intent.putExtra("Arrays",values);

而不是

values = extras.getStringArray("UserResults");