gpt4 book ai didi

java - 有意传递很长时间,总是出错

转载 作者:太空宇宙 更新时间:2023-11-03 11:52:00 25 4
gpt4 key购买 nike

我需要使用此代码将带有 Intent 的长 (_id) 从 Activity A 传递到 Activity B:

Intent vip0= new Intent(this, PageSinglePlayerGuess.class);
vip0.putExtra("resid", (long) 1);
startActivity(vip0);

并使用此方法(在 onCreate() 中调用)在 Activity B 中获取它:

public long getResId() {
Intent i= getIntent();
resid= i.getLongExtra("resid", 1);
Log.d("D", "Risorsa: " + resid);
return resid;
}

驻留在ActB 中的是一个long。我收到此错误:

    06-06 11:10:47.138: W/Bundle(538): Key resid expected Long but value was a java.lang.Integer.  The default value 1 was returned.
06-06 11:10:47.158: W/Bundle(538): Attempt to cast generated internal exception:
06-06 11:10:47.158: W/Bundle(538): java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
06-06 11:10:47.158: W/Bundle(538): at android.os.Bundle.getLong(Bundle.java:966)
06-06 11:10:47.158: W/Bundle(538): at android.content.Intent.getLongExtra(Intent.java:3874)
06-06 11:10:47.158: W/Bundle(538): at com.gmail.corsalini.celebrityquiz.PageSinglePlayerGuess.getResId(PageSinglePlayerGuess.java:60)
06-06 11:10:47.158: W/Bundle(538): at com.gmail.corsalini.celebrityquiz.PageSinglePlayerGuess.onCreate(PageSinglePlayerGuess.java:46)
06-06 11:10:47.158: W/Bundle(538): at android.app.Activity.performCreate(Activity.java:4465)
06-06 11:10:47.158: W/Bundle(538): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-06 11:10:47.158: W/Bundle(538): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-06 11:10:47.158: W/Bundle(538): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-06 11:10:47.158: W/Bundle(538): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-06 11:10:47.158: W/Bundle(538): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-06 11:10:47.158: W/Bundle(538): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 11:10:47.158: W/Bundle(538): at android.os.Looper.loop(Looper.java:137)
06-06 11:10:47.158: W/Bundle(538): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-06 11:10:47.158: W/Bundle(538): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 11:10:47.158: W/Bundle(538): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 11:10:47.158: W/Bundle(538): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-06 11:10:47.158: W/Bundle(538): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-06 11:10:47.158: W/Bundle(538): at dalvik.system.NativeStart.main(Native Method)

知道如何解决这个问题吗? (看起来真的很容易,但我就是出不来)

最佳答案

根据日志,问题似乎出在以下地方:

resid= i.getLongExtra("resid", 1);

所以改成:

resid= i.getLongExtra("resid", 1L);

顺便说一句:getLongExtra 实现看起来像这样:

public long getLongExtra(String name, long defaultValue) {
return mExtras == null ? defaultValue : mExtras.getLong(name, defaultValue);
}

这就是为什么看到它在行失败的原因:

at android.os.Bundle.getLong(Bundle.java:966)

关于java - 有意传递很长时间,总是出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10913165/

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