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);
Not always - if
savedInstanceState
is not null, thennewString
is the value ofsavedInstanceState.getSerializable("urlAddress");
, which could possibly return null.Alternatively,
getIntent().getExtras()
is null, therefore you hitWhich will definitely cause an error.
In either case, you can catch the error by using this
And, then to address the problem, you might have to implement
onSaveInstanceState
to put the url string into thatsavedInstanceState
Bundle. But, you should be usingputString
andgetString
, probably, instead ofput
/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
So I decided to make it manually
I use
with this setting on
AndroidManifest.xml
android:configChanges="orientation"
This will make the height/width value different without destroying the activity