方向改变导致应用程序转为 FC

发布于 2024-12-27 18:29:03 字数 1104 浏览 2 评论 0原文

我正在开发一个扫描条形码的应用程序,当它成功扫描时,它将显示一个包含已扫描代码的对话框,并且根据条码/二维码中包含的内容,它将显示一个用于打开浏览器的按钮,发送短信等。在显示对话框时,如果屏幕改变方向,则会崩溃。我已经让它在崩溃之前可以改变方向几次,但是当我检查 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最近可好 2025-01-03 18:29:03

在 onPause() 中,我使用了 this.onRetainNonConfigurationInstance() 来尝试解决这个问题,但它似乎没有产生任何影响。

您无需调用onRetainNonConfigurationInstance()。 Android 调用 onRetainNonConfigurationInstance()

根据我读过的所有有关处理方向更改的帖子,我想这在我看来应该可行。

您需要使用更好的来源。

要尝试修复您的代码,请执行以下操作:

步骤 #1:从清单中的 中删除 android:configChanges 属性

步骤 #2:删除 onConfigurationChanged () 方法

步骤#3:要么将 onResume() 逻辑移至 onCreate() 中以填充 currentType,要么不移动尝试使用onResume() 之前的 currentType

更好的方法是将整个 onRetainNonConfigurationInstance()/getLastNonConfigurationInstance() 替换为 < code>onSaveInstanceState()/onRestoreInstanceState(),将您的 String 放入 Bundle 中。

这是一个示例项目,演示了 onSaveInstanceState 的使用()这是一个示例项目,演示了 onRetainNonConfigurationInstance 的使用()

In onPause() I have used this.onRetainNonConfigurationInstance() to try to remedy that but it doesn't appear to have made a difference.

You do not call onRetainNonConfigurationInstance(). Android calls onRetainNonConfigurationInstance().

I guess it also seems to me that this should work, according to all the posts I've read about handling orientation changes.

You need to use better sources.

To attempt to repair your code:

Step #1: Delete the android:configChanges attribute from your <activity> in the manifest

Step #2: Delete your onConfigurationChanged() method

Step #3: Either move your onResume() logic into onCreate() to populate currentType, or do not attempt to use currentType before onResume()

Even better would be to replace your whole onRetainNonConfigurationInstance()/getLastNonConfigurationInstance() with onSaveInstanceState()/onRestoreInstanceState(), putting your String in the Bundle.

Here is a sample project demonstrating the use of onSaveInstanceState(). Here is a sample project demonstrating the use of onRetainNonConfigurationInstance().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文