简体   繁体   中英

Send data from one Activity to another Activity using Intents

I want to send data from one Activity to another Activity using Intents when I select item from a ListView.

My first Activity code :

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

Second Activity Code :

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

My logcat error :

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

Just replace these two lines

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

These lines should be like this

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

You were setting text to textView before adding textView to your main view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM