简体   繁体   English

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

[英]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.当我从 ListView 中选择项目时,我想使用 Intents 将数据从一个活动发送到另一个活动。

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 :我的 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

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在将 textView 添加到主视图之前,您将文本设置为 textView

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用意图将数据从一个活动传输到另一个活动 - Transfer data from one Activity to Another Activity Using Intents 意图信息不会从一项活动发送到另一项活动 - Information in intents not send from one activity to another 如何使用 Intent 将对象从一个 Android Activity 发送到另一个? - How to send an object from one Android Activity to another using Intents? 如何将数据从一个活动传递到另一个活动而不使用意图? - How to Pass data from One activity to another without using intents? 通过自定义适配器的意图通过android中的项目将数据从一个活动发送到另一个活动/片段 - send data from one activity to another activity / fragment via Intents through Custom Adapter for List items in android 如何使用意图将用户输入数据从一个活动发送到另一个活动 - How to use intents to send a user input data from one activity to another 如何使用意图将包含可绘制对象的对象从一个Android活动发送到另一个? - How to send an object containing drawable objects from one android activity to another using intents? Android-我尝试使用意图将数据从一个活动发送到另一个活动 - Android-I tried sending data from one activity to another using intents 从列表视图将数据作为意图发送到另一个活动 - sending data as intents to another activity from listview 将listView数据从一个活动发送到另一个活动 - Send listView data from one activity to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM