Android打开对话框在onActivityResult之后崩溃应用程序

时间:2014-08-08 16:32:08

标签: android

我有一个扫描条形码然后打开对话框的应用程序。使用zxing库扫描条形码后,应用程序崩溃。为什么会这样?这是代码。

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings){
            return true;
        }else if (id == R.id.action_scan){

            IntentIntegrator scanIntegrator = new IntentIntegrator(this);
            scanIntegrator.initiateScan();


            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {


        IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        if (scanningResult != null) {
            NewPackage dialog = new NewPackage(scanningResult.getContents());
            dialog.show(getSupportFragmentManager(), "new_package_dialog");
            }
        else{

            }
        }

stacktrace:

  

08-08 12:40:32.727:E / AndroidRuntime(22924):   java.lang.RuntimeException:传递结果失败   ResultInfo {who = null,request = 49374,result = -1,data = Intent {   act = com.google.zxing.client.android.SCAN flg = 0x80000(有额外内容)}}   活动{com.example.shiptrack / com.example.shiptrack.Packages}:   java.lang.IllegalStateException:之后无法执行此操作   onSaveInstanceState 08-08 12:40:32.727:E / AndroidRuntime(22924):at   android.app.ActivityThread.deliverResults(ActivityThread.java:3351)   08-08 12:40:32.727:E / AndroidRuntime(22924):at   android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)   08-08 12:40:32.727:E / AndroidRuntime(22924):at   android.app.ActivityThread.access $ 1300(ActivityThread.java:135)08-08   12:40:32.727:E / AndroidRuntime(22924):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1244)   08-08 12:40:32.727:E / AndroidRuntime(22924):at   android.os.Handler.dispatchMessage(Handler.java:102)08-08   12:40:32.727:E / AndroidRuntime(22924):at   android.os.Looper.loop(Looper.java:136)08-08 12:40:32.727:   E / AndroidRuntime(22924):at   android.app.ActivityThread.main(ActivityThread.java:5001)08-08   12:40:32.727:E / AndroidRuntime(22924):at   java.lang.reflect.Method.invokeNative(Native Method)08-08   12:40:32.727:E / AndroidRuntime(22924):at   java.lang.reflect.Method.invoke(Method.java:515)08-08 12:40:32.727:   E / AndroidRuntime(22924):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)   08-08 12:40:32.727:E / AndroidRuntime(22924):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)08-08   12:40:32.727:E / AndroidRuntime(22924):at   dalvik.system.NativeStart.main(Native Method)08-08 12:40:32.727:   E / AndroidRuntime(22924):引起:java.lang.IllegalStateException:   在onSaveInstanceState 08-08之后无法执行此操作   12:40:32.727:E / AndroidRuntime(22924):at   android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1360)   08-08 12:40:32.727:E / AndroidRuntime(22924):at   android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1378)   08-08 12:40:32.727:E / AndroidRuntime(22924):at   android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)   08-08 12:40:32.727:E / AndroidRuntime(22924):at   android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)   08-08 12:40:32.727:E / AndroidRuntime(22924):at   android.support.v4.app.DialogFragment.show(DialogFragment.java:138)   08-08 12:40:32.727:E / AndroidRuntime(22924):at   com.example.shiptrack.Packages.showDialog(Packages.java:73)08-08   12:40:32.727:E / AndroidRuntime(22924):at   com.example.shiptrack.Packages.onActivityResult(Packages.java:63)   08-08 12:40:32.727:E / AndroidRuntime(22924):at   android.app.Activity.dispatchActivityResult(Activity.java:5423)08-08   12:40:32.727:E / AndroidRuntime(22924):at   android.app.ActivityThread.deliverResults(ActivityThread.java:3347)   08-08 12:40:32.727:E / AndroidRuntime(22924):... 11更多

2 个答案:

答案 0 :(得分:0)

扫描条形码时是否有可能改变方向? 看起来启动器活动在获得结果之前被破坏并重新创建。 也许处理调整活动中的方向和屏幕大小的配置更改应该可以防止这个问题。

尝试在清单的活动代码中添加 android:configChanges =“orientation | screensize | keyboardHidden”

答案 1 :(得分:0)

在Android N(2016年8月发布的7.0,约占Android市场份额的25%)上,该错误仍然存​​在。

问题在于,活动无法在处理活动之前显示对话框 onActivityResult。不必调用Activity.runOnUIThread(),因为onActivityResult中的代码已经在主UI线程中运行,相反,您只需要在请求对话框之前调用父(重写)方法onActivityResultsuper.onActivityResult(requestCode, resultCode, intent);

因此您的onActivityResult变为:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // First call onActivityResult to be able to show dialogs and manage fragments.
    // If the call is not done, showing dialog will crash the app!
    // (Again, thank Google for the beautiful design)
    super.onActivityResult(requestCode, resultCode, data);

    // Do your stuff
    IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, 
        resultCode, intent);
    if (scanningResult != null) {
        NewPackage dialog = new NewPackage(scanningResult.getContents());
        dialog.show(getSupportFragmentManager(), "new_package_dialog");
    }
}