2

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);
Fay Zan
  • 165
  • 2
  • 17
  • Please try this [solution](http://stackoverflow.com/questions/4096169/onsaveinstancestate-and-onrestoreinstancestate) – LvN Aug 04 '16 at 04:17

2 Answers2

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

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • thanks for the reply,I tried changing `newString = null;` to `newString = "";` I still get the same error.I have updated my question – Fay Zan Aug 04 '16 at 03:21
  • @FayZan See my update. Take note of the comment that says `// Code that requires urlString` – OneCricketeer Aug 04 '16 at 03:48
  • Thanks,I didn't understand what that meant until you added `playAll()`.Will update my progress with you – Fay Zan Aug 04 '16 at 07:52
-1

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

Fay Zan
  • 165
  • 2
  • 17
  • The XML content view automatically switches. How does that fix the NullPointerException in the Bundle? – OneCricketeer Aug 04 '16 at 11:40
  • @cricket_007 I input this command `android:configChanges="orientation|screenSize|keyboardHidden"` into my AndroidManifest.xml so it wouldn't crash upon orientation.Plus when using `android:configChanges="orientation"` my activity won't be destroyed when it goes landscape which may explains why my `Bundle extras = getIntent().getExtras();` is null in the first place.If I remember correctly Bundle extra is temporary,so upon destroy it becomes empty – Fay Zan Aug 05 '16 at 01:33
  • @cricket_007 I'm not really 100% sure that's the reason though since I'm having similar problem with another code which I posted a question here http://stackoverflow.com/questions/2605470/displaying-the-time-in-am-pm-format-in-android – Fay Zan Aug 05 '16 at 01:37
  • That link isn't to a question that you posted – OneCricketeer Aug 05 '16 at 02:03
  • @cricket_007 Oopps,I'm sorry about that http://stackoverflow.com/questions/38763116/dynamic-time-display-crash-on-orientation/38779986#38779986.I already got it fixed though if you have a better way I'd glad to try it – Fay Zan Aug 05 '16 at 02:05