gpt4 book ai didi

java - 第一个 Intent 是有效,第二个 Intent 是让我崩溃

转载 作者:行者123 更新时间:2023-12-01 15:19:08 27 4
gpt4 key购买 nike

接收方有两个 Intent ,它们从同一 Activity 调用并将数据发送到不同的 Activity 。第二个 Intent 不起作用。第一个是这样的。我不明白为什么。所有 Activity 和 list 似乎都是正确的。

//second intent on senders side    
public void onItemSelected(AdapterView<?> arg0, View users, int i,
long l) {
FILENAME = (adapter.getItem(i)).toString();

Bundle viewBag2 = new Bundle();
viewBag2.putString("profile_name", FILENAME);
Intent b = new Intent(OptionsMenu.this, CoreActivity.class);
b.putExtras(viewBag2);
startActivity(b);
}


//second intent on receiver side

private void Data_transfer() {
Bundle gotbasket2 = getIntent().getExtras();
profileName = gotbasket2.getString("profile_name");
}

//first (working intent) on senders side
public void onClick(View v) {
Bundle viewBag = new Bundle();
viewBag.putString("spinner_result", s);
a.putExtras(viewBag);
}



//first (working intent) on receiver side
private void Data_transfer() {
// TODO Auto-generated method stub
Bundle gotbasket = getIntent().getExtras();
x = gotbasket.getString("spinner_result");
}

下面是附加的 logcat。

 06-26 20:22:09.787: D/AndroidRuntime(1802): Shutting down VM
06-26 20:22:09.787: W/dalvikvm(1802): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-26 20:22:09.847: E/AndroidRuntime(1802): FATAL EXCEPTION: main
06-26 20:22:09.847: E/AndroidRuntime(1802): java.lang.RuntimeException: Unable to start activity ComponentInfo{mioc.diver/mioc.diver.CoreActivity}: java.lang.NullPointerException
06-26 20:22:09.847: E/AndroidRuntime(1802): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-26 20:22:09.847: E/AndroidRuntime(1802): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-26 20:22:09.847: E/AndroidRuntime(1802): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-26 20:22:09.847: E/AndroidRuntime(1802): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-26 20:22:09.847: E/AndroidRuntime(1802): at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 20:22:09.847: E/AndroidRuntime(1802): at android.os.Looper.loop(Looper.java:123)
06-26 20:22:09.847: E/AndroidRuntime(1802): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-26 20:22:09.847: E/AndroidRuntime(1802): at java.lang.reflect.Method.invokeNative(Native Method)
06-26 20:22:09.847: E/AndroidRuntime(1802): at java.lang.reflect.Method.invoke(Method.java:507)
06-26 20:22:09.847: E/AndroidRuntime(1802): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-26 20:22:09.847: E/AndroidRuntime(1802): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-26 20:22:09.847: E/AndroidRuntime(1802): at dalvik.system.NativeStart.main(Native Method)
06-26 20:22:09.847: E/AndroidRuntime(1802): Caused by: java.lang.NullPointerException
06-26 20:22:09.847: E/AndroidRuntime(1802): at mioc.diver.CoreActivity.Data_transfer(CoreActivity.java:189)
06-26 20:22:09.847: E/AndroidRuntime(1802): at mioc.diver.CoreActivity.onCreate(CoreActivity.java:88)
06-26 20:22:09.847: E/AndroidRuntime(1802): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-26 20:22:09.847: E/AndroidRuntime(1802): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-26 20:22:09.847: E/AndroidRuntime(1802): ... 11 more

最佳答案

首先,您不需要每次想要在 Intent 中添加额外内容时都创建一个新的 bundle 。你可以这样做:

Intent b = new Intent(OptionsMenu.this, CoreActivity.class);
b.putExtra(key, value);

其次,方法名称不应以大写字母开头,如 Google Java Style Guide 中所述。 .

最后,您似乎在 Data_transfer() 中收到 NullPointerException,这意味着您的 Activity 的第 189 行(可能是 FILENAME)有一个 null 变量。

关于java - 第一个 Intent 是有效,第二个 Intent 是让我崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11215016/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com