配置更改时的Intent Extras null [英] Intent extras null on configuration change

查看:153
本文介绍了配置更改时的Intent Extras null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个显示在SurfaceView上的布局,并且可以使用Bundle extras = getIntent().getExtras()来获得setDataSource.

I created a layout that displays on a SurfaceView and I can get the setDataSource by using Bundle extras = getIntent().getExtras().

一切正常,直到我尝试从land\layout.xml设置景观布局.

Everything works fine until I try to set the landscape layout from land\layout.xml.

我的日志猫是

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference
at asia.sumikawa.cybereyeview.liveActivity.onCreate(liveActivity.java:65)
at android.app.Activity.performCreate(Activity.java:6092)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4216) 
at android.app.ActivityThread.access$900(ActivityThread.java:178) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1476) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5637) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)  

我的Java编码

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate");
setContentView(R.layout.activity_live_alternate);
//loadLibrary();
final String newString;
if (savedInstanceState == null) {
  Bundle extras = getIntent().getExtras();
  if(extras == null) {
    newString = null;
  } else {
    newString = extras.getString("urlAddress");
  }
} else {
  newString = (String) savedInstanceState.getSerializable("urlAddress");
}
urlLink = "rtsp://" + newString.trim().substring(2);
urlString = newString;

空指针异常在行上

urlLink = "rtsp://" + newString.trim().substring(2);

从中获取值

Bundle extras = getIntent().getExtras();

PS ,我不想使用android:configChanges="orientation",因为我试图使布局具有不同的高度/宽度值

PS I would prefer not using android:configChanges="orientation" as I'm trying to make the layout have different height/width value

编辑

添加这些代码后,要感谢 cricket_007

After adding these code thanks to cricket_007

if (newString != null){
  urlLink = "rtsp://" + newString.trim().substring(2);
}else{
  Log.i(LOG,"Error");
}

我得到了这个错误

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'char java.lang.String.charAt(int)' on a null object reference
at asia.sumikawa.cybereyeview.liveActivity.playAll(liveActivity.java:307)
at asia.sumikawa.cybereyeview.liveActivity.onCreate(liveActivity.java:77)

指向这些代码行

void playAll(){
if(urlString.charAt(0) == '1'){
  videoPlay();
}else if(urlString.charAt(0) == '2'){
  videoPlay();videoPlay2();
}else if(urlString.charAt(0) == '3'){
  videoPlay();videoPlay2();
  videoPlay3();
}else if(urlString.charAt(0) == '4'){
  videoPlay();videoPlay2();
  videoPlay3();videoPlay4();
}}

只要有需要,这些就是我用来传递上一类的String的代码

Just in case this is needed,these are the codes I use to pass the String from the previous class

Intent i = new Intent(addressActivity.this, liveActivity.class);
String strName = content.toString();
i.putExtra("urlAddress", strName);
startActivity(i);

推荐答案

从中获取值

Bundle extras = getIntent().getExtras();

并非总是如此-如果savedInstanceState不为null,则newStringsavedInstanceState.getSerializable("urlAddress");的值,可能会返回null.

Not always - if savedInstanceState is not null, then newString is the value of savedInstanceState.getSerializable("urlAddress");, which could possibly return null.

或者,getIntent().getExtras()为空,因此您点击

Alternatively, getIntent().getExtras() is null, therefore you hit

if (extras == null) {
    newString = null;
}

这肯定会导致错误.

在任何一种情况下,您都可以使用此方法来捕获错误

In either case, you can catch the error by using this

if (newString != null) {
    urlString = "rtsp://" + newString.trim().substring(2);
    // Code that requires urlString
    playAll();
} else {
    // Show an error
}

然后,要解决该问题,您可能必须实现onSaveInstanceState才能将url字符串放入该savedInstanceState捆绑包中.但是,可能应该使用putStringgetString,而不是put/get-Serializable.这样就避免了强制转换.

And, then to address the problem, you might have to implement onSaveInstanceState to put the url string into that savedInstanceState Bundle. But, you should be using putString and getString, probably, instead of put / get - Serializable. That way you avoid the cast.

为了找到变量从何处获取空值,只需进行适当的日志记录和调试即可.

In order to find where the variable is getting null, it's just a matter of logging and debugging appropriately.

可以在 处理运行时更改

这篇关于配置更改时的Intent Extras null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆