尝试在空对象引用上调用虚拟方法“java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28239763/
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-11-02 13:15:16  来源:igfitidea点击:

Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference

javaandroidnullpointerexception

提问by robWDC

I need to pass a long[] from an Activity to an IntentService. Created a Bundle, stored the array in the Bundle and then added the Bundle to the Intent. In the IntentService, when i try to extract the Bundle, i get: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference.As you can see, i've tried this with both a long[] and String[] and both fault in the same way.

我需要将 long[] 从 Activity 传递到 IntentService。创建了一个 Bundle,将数组存储在 Bundle 中,然后将 Bundle 添加到 Intent。在 IntentService 中,当我尝试提取 Bundle 时,我得到: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference.如您所见,我已尝试使用 long[] 和 String[] 进行此操作,并且两者都以相同的方式出错。

In the Activity

在活动中

String[] sAr = {"some", "times"};
// launch simulator threads using test parameters
Intent inStartSim = new Intent(this, SimService.class);
//inStartSim.setAction("TrigParm");
inStartSim.setAction("genData");
Bundle simBundle = new Bundle();
//simBundle.putByteArray(arSimConfig);
//simBundle.putLongArray("simConfigAr",arSim1Config);
simBundle.putStringArray("simConfigAr",sAr);
inStartSim.putExtras(simBundle);
startService(inStartSim);

In the IntentService

在 IntentService

Bundle simBundle = intent.getBundleExtra("simBundle");
long[] simConfigParm;
String[] sAr;
//simConfigParm = simBundle.getLongArray("simConfigAr");
sAr = simBundle.getStringArray("simConfigAr");

When drilling into the Bundle object after the exception, i did notice that the identifiers are different:

在异常之后钻入 Bundle 对象时,我确实注意到标识符不同:

Bundle[{simConfigAr=[J@5668b8a}] – in Simulator
Bundle[{simConfigAr=[J@f0d80de}] – in SimService

I know that there are many other ways to move data between Activities and Services, but this seemed to be the easiest. http://developer.android.com/guide/faq/framework.html#3- a good starting point for other techniques.

我知道还有许多其他方法可以在活动和服务之间移动数据,但这似乎是最简单的。 http://developer.android.com/guide/faq/framework.html#3- 其他技术的良好起点。

回答by Rohit5k2

You need to get the value from the bundle you are putting into

您需要从要放入的捆绑包中获取价值

Solution 1

方案一

Change this in you intent service

在你的意图服务中改变这一点

Bundle simBundle = intent.getBundleExtra("simBundle");

to

Bundle simBundle = intent.getExtras();

Solution 2This is preferred if you will send extra values from other activity too to the same service

解决方案 2如果您也将来自其他活动的额外值发送到同一服务,则这是首选

Change this in your Activity

在您的活动中更改此设置

inStartSim.putExtras(simBundle);

to

inStartSim.putExtra("simBundle", simBundle)