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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10913165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:04:23  来源:igfitidea点击:

Passing long with an intent, always getting error

javaandroidandroid-intentlong-integer

提问by David Corsalini

I need to pass a long (_id) with an intent from Activity A to Activity B, using this code:

我需要使用以下代码将带有意图的 long (_id) 从活动 A 传递到活动 B:

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

and getting it in Activity B with this method (called in onCreate()):

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

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

resid in ActB is a long. I get this error:

驻留在ActB是很长的。我收到此错误:

    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.access0(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)

Any idea how to solve this? (it seems really easy, but i just can't get out of it)

知道如何解决这个问题吗?(看起来很容易,但我就是无法摆脱它)

回答by sinek

According to the log the problem seems to be in following:

根据日志,问题似乎如下:

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

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

so change it to:

所以将其更改为:

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

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

BTW: the getLongExtra implementation looks like this:

顺便说一句:getLongExtra 实现如下所示:

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

So that's why see that it fails at line:

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

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

回答by Kazekage Gaara

Change:

改变:

vip0.putExtra("resid", 1L);

A long value is always declared with a Lsuffix.

长值总是用L后缀声明。

回答by FabianCook

Try using Long.valueOf(Integer int) instead of (long) 1

尝试使用 Long.valueOf(Integer int) 而不是 (long) 1

This is because it is being seen as an int still.

这是因为它被视为 int 仍然。