Intent extras null on configuration change

亡梦爱人 提交于 2019-12-12 21:16:54

问题


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

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

My logcat is

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)  

My java coding

@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;

The null pointer exception is on line

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

which gets the value from

Bundle extras = getIntent().getExtras();

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

EDIT

After adding these code thanks to cricket_007

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

I got this error instead

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)

which point to these lines of codes

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();
}}

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);

回答1:


which gets the value from

Bundle extras = getIntent().getExtras();

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

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

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

Which will definitely cause an error.

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
}

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.

Approaches to saving your data between orientation changes can be found at Handling Runtime Changes




回答2:


So I decided to make it manually

I use

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();

    setContentView(R.layout.activity_live_alternate_land);

} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();

    setContentView(R.layout.activity_live_alternate);
}

with this setting on AndroidManifest.xml

android:configChanges="orientation"

This will make the height/width value different without destroying the activity



来源:https://stackoverflow.com/questions/38757374/intent-extras-null-on-configuration-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!