使用Intents将数据从一个Activity发送到另一个Activity

时间:2015-11-29 15:01:54

标签: android listview

当我从ListView中选择项目时,我想使用Intents将数据从一个Activity发送到另一个Activity。

我的第一个活动代码:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(WelcomeActivity.this, parent.getItemAtPosition(position)+" is selected "+id, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(WelcomeActivity.this,SecondActivity.class);
        intent.putExtra("passDetails",(String)(parent.getItemAtPosition(position)));
        startActivity(intent);
    }
});

第二个活动代码:

toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
Intent intent = getIntent();
String value = intent.getStringExtra("passDetails");
TextView textView = new TextView(this);
textView.setTextSize(45);
textView.setText(value);
setContentView(textView);

我的logcat错误:

11-29 21:03:11.300: E/AndroidRuntime(11786): FATAL EXCEPTION: main
11-29 21:03:11.300: E/AndroidRuntime(11786): Process: com.avyukta.developer.registration, PID: 11786
11-29 21:03:11.300: E/AndroidRuntime(11786): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.avyukta.developer.registration/com.avyukta.developer.registration.SecondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
11-29 21:03:11.300: E/AndroidRuntime(11786):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3125)
11-29 21:03:11.300: E/AndroidRuntime(11786):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3224)
11-29 21:03:11.300: E/AndroidRuntime(11786):    at android.app.ActivityThread.access$1000(ActivityThread.java:198)
11-29 21:03:11.300: E/AndroidRuntime(11786):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1682)
11-29 21:03:11.300: E/AndroidRuntime(11786):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-29 21:03:11.300: E/AndroidRuntime(11786):    at android.os.Looper.loop(Looper.java:145)
11-29 21:03:11.300: E/AndroidRuntime(11786):    at android.app.ActivityThread.main(ActivityThread.java:6843)
11-29 21:03:11.300: E/AndroidRuntime(11786):    at java.lang.reflect.Method.invoke(Native Method)
11-29 21:03:11.300: E/AndroidRuntime(11786):    at java.lang.reflect.Method.invoke(Method.java:372)
11-29 21:03:11.300: E/AndroidRuntime(11786):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
11-29 21:03:11.300: E/AndroidRuntime(11786):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
11-29 21:03:11.300: E/AndroidRuntime(11786): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
11-29 21:03:11.300: E/AndroidRuntime(11786):    at com.avyukta.developer.registration.SecondActivity.onCreate(SecondActivity.java:25)
11-29 21:03:11.300: E/AndroidRuntime(11786):    at android.app.Activity.performCreate(Activity.java:6500)
11-29 21:03:11.300: E/AndroidRuntime(11786):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
11-29 21:03:11.300: E/AndroidRuntime(11786):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3078)
11-29 21:03:11.300: E/AndroidRuntime(11786):    ... 10 more

1 个答案:

答案 0 :(得分:0)

只需更换这两行

textView.setText(value);
setContentView(textView);

这些行应该是这样的

setContentView(textView);
textView.setText(value);
  

在将textView添加到main之前,您已将文本设置为textView   图