我创建了一个显示在SurfaceView
上的布局,我可以使用setDataSource
获取Bundle extras = getIntent().getExtras()
。
在我尝试从land\layout.xml
设置横向布局之前,一切正常。
我的logcat是
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"
,因为我试图让布局具有不同的高度/宽度值
修改
感谢 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
的代码
Intent i = new Intent(addressActivity.this, liveActivity.class);
String strName = content.toString();
i.putExtra("urlAddress", strName);
startActivity(i);
答案 0 :(得分:1)
从
获取值Bundle extras = getIntent().getExtras();
并非总是如果savedInstanceState
不为null,则newString
是savedInstanceState.getSerializable("urlAddress");
的值,可能返回null。
或者,getIntent().getExtras()
为空,因此您点击了
if (extras == null) {
newString = null;
}
肯定会导致错误。
在任何一种情况下,您都可以使用此
来捕获错误if (newString != null) {
urlString = "rtsp://" + newString.trim().substring(2);
// Code that requires urlString
playAll();
} else {
// Show an error
}
然后,为了解决这个问题,您可能必须实现onSaveInstanceState
将url字符串放入savedInstanceState
Bundle中。但是,您应该使用putString
和getString
代替put
/ get
- Serializable
。这样你就可以避免施法。
为了找到变量变为null的位置,只需要适当地记录和调试。
找到在方向更改之间保存数据的方法答案 1 :(得分:-1)
所以我决定手动制作
我用
@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);
}
在AndroidManifest.xml
android:configChanges="orientation"
这将使高度/宽度值不同而不会破坏活动