方向改变导致应用程序转为 FC
我正在开发一个扫描条形码的应用程序,当它成功扫描时,它将显示一个包含已扫描代码的对话框,并且根据条码/二维码中包含的内容,它将显示一个用于打开浏览器的按钮,发送短信等。在显示对话框时,如果屏幕改变方向,则会崩溃。我已经让它在崩溃之前可以改变方向几次,但是当我检查 LogCat 时,它说 NullPointerException 导致了 FC。在我实施@CommonsWare的建议之前,我可以让它在崩溃之前旋转任意次数,但自从我实施了它们以来,FC总是在第二个方向发生变化。当我在调试模式
下启动它时,我可以按照我想要的次数和速度旋转手机,但是一旦我在正常模式下启动它,它总是崩溃。
类字段:
private String currentType;
我实现了 onSaveInstanceState()
:
@Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putString("savedType", currentType);
}
还有 onRestoreInstanceState()
:
@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
currentType = savedInstanceState.getString("savedType");
}
更新
我已按照 @CommonsWare 的建议将上述代码更新为当前的内容。为了完整性,我还编辑了我的帖子。
我也在使用 CM7(Android 版本 - 2.3.7,内核版本 - 2.6.37.6-cyanogenmod-g0799e00 android@portatile #1,Mod 版本 - CyanogenMod-7-11152011-NIGHTLY-N1,内部版本号 - GWK74)。
I'm working on an app that scans barcodes, when it gets a successful scan it will show a Dialog with the code that was scanned and depending on what was contained within the bar/QR code it will show a button to open a browser, send an SMS, etc. While the Dialog is showing, if the screen changes orientation it crashes. I have got it working to where the orientation can change a couple times before it crashes, but when I check LogCat it says that a NullPointerException is causing the FC. Before I had implemented @CommonsWare's suggestions I could get it to rotate an arbitrary amount of times before crashing, but since I've implemented them it FC's on the second orientation change always. When I launch this in debug mode
I can rotate the phone as many times as I want and as quickly as I want but as soon as I launch it in normal mode it always crashes.
Class Field:
private String currentType;
I implemented onSaveInstanceState()
:
@Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putString("savedType", currentType);
}
Also onRestoreInstanceState()
:
@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
currentType = savedInstanceState.getString("savedType");
}
Update
I've updated the above code to what it currently is, following @CommonsWare's suggestions. I've also edited my post for integrity.
I'm also using CM7 (Android Version - 2.3.7, Kernel Version - 2.6.37.6-cyanogenmod-g0799e00 android@portatile #1, Mod Version - CyanogenMod-7-11152011-NIGHTLY-N1, Build Number - GWK74).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无需调用
onRetainNonConfigurationInstance()
。 Android 调用onRetainNonConfigurationInstance()
。您需要使用更好的来源。
要尝试修复您的代码,请执行以下操作:
步骤 #1:从清单中的
中删除android:configChanges
属性步骤 #2:删除
onConfigurationChanged ()
方法步骤#3:要么将
onResume()
逻辑移至onCreate()
中以填充currentType
,要么不移动尝试使用onResume()
之前的currentType
更好的方法是将整个
onRetainNonConfigurationInstance()
/getLastNonConfigurationInstance()
替换为 < code>onSaveInstanceState()/onRestoreInstanceState()
,将您的String
放入Bundle
中。这是一个示例项目,演示了
onSaveInstanceState 的使用()
。 这是一个示例项目,演示了onRetainNonConfigurationInstance 的使用()
。You do not call
onRetainNonConfigurationInstance()
. Android callsonRetainNonConfigurationInstance()
.You need to use better sources.
To attempt to repair your code:
Step #1: Delete the
android:configChanges
attribute from your<activity>
in the manifestStep #2: Delete your
onConfigurationChanged()
methodStep #3: Either move your
onResume()
logic intoonCreate()
to populatecurrentType
, or do not attempt to usecurrentType
beforeonResume()
Even better would be to replace your whole
onRetainNonConfigurationInstance()
/getLastNonConfigurationInstance()
withonSaveInstanceState()
/onRestoreInstanceState()
, putting yourString
in theBundle
.Here is a sample project demonstrating the use of
onSaveInstanceState()
. Here is a sample project demonstrating the use ofonRetainNonConfigurationInstance()
.