gpt4 book ai didi

java - 当设备方向改变时,Qr 扫描应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 19:20:09 29 4
gpt4 key购买 nike

我在 android 中制作了一个基本的二维码扫描仪,但是在扫描二维码时将设备旋转到横向然后再旋转到纵向模式后它崩溃了。还请简要说明导致问题的原因。我需要释放一些资源吗? P.S我是初学者。

package com.example.android.qrking;

import android.app.Activity;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class MainActivity extends Activity implements ZXingScannerView.ResultHandler{
private ZXingScannerView mScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void onClick(View v){
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}

@Override
protected void onPause() {
super.onPause();
mScannerView.stopCamera();
}

@Override
public void handleResult(Result result){
Log.v("handler result", result.getText());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("scan results");
builder.setMessage(result.getText());
AlertDialog alertDialog = builder.create();
alertDialog.show();
//uncomment to scan again
mScannerView.resumeCameraPreview(this);
}
}

Logcat 详细信息

03-09 21:18:24.460 10365-10365/com.example.android.qrking W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x419bac98)
03-09 21:18:24.461 10365-10365/com.example.android.qrking W/dalvikvm: threadid=1: uncaught exception occurred
03-09 21:18:24.461 10365-10365/com.example.android.qrking W/System.err: java.lang.RuntimeException: Unable to pause activity {com.example.android.qrking/com.example.android.qrking.MainActivity}: java.lang.NullPointerException
03-09 21:18:24.464 10365-10365/com.example.android.qrking W/System.err: at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3242)
03-09 21:18:24.464 10365-10365/com.example.android.qrking W/System.err: at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3193)
03-09 21:18:24.464 10365-10365/com.example.android.qrking W/System.err: at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3899)
03-09 21:18:24.464 10365-10365/com.example.android.qrking W/System.err: at android.app.ActivityThread.access$900(ActivityThread.java:151)
03-09 21:18:24.464 10365-10365/com.example.android.qrking W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
03-09 21:18:24.464 10365-10365/com.example.android.qrking W/System.err: at android.os.Handler.dispatchMessage(Handler.java:110)
03-09 21:18:24.465 10365-10365/com.example.android.qrking W/System.err: at android.os.Looper.loop(Looper.java:193)
03-09 21:18:24.465 10365-10365/com.example.android.qrking W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5292)
03-09 21:18:24.465 10365-10365/com.example.android.qrking W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
03-09 21:18:24.465 10365-10365/com.example.android.qrking W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
03-09 21:18:24.465 10365-10365/com.example.android.qrking W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
03-09 21:18:24.465 10365-10365/com.example.android.qrking W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
03-09 21:18:24.465 10365-10365/com.example.android.qrking W/System.err: at dalvik.system.NativeStart.main(Native Method)
03-09 21:18:24.465 10365-10365/com.example.android.qrking W/System.err: Caused by: java.lang.NullPointerException
03-09 21:18:24.466 10365-10365/com.example.android.qrking W/System.err: at com.example.android.qrking.MainActivity.onPause(MainActivity.java:29)
03-09 21:18:24.466 10365-10365/com.example.android.qrking W/System.err: at android.app.Activity.performPause(Activity.java:5368)
03-09 21:18:24.466 10365-10365/com.example.android.qrking W/System.err: at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1239)
03-09 21:18:24.466 10365-10365/com.example.android.qrking W/System.err: at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3224)
03-09 21:18:24.466 10365-10365/com.example.android.qrking W/System.err: ... 12 more
03-09 21:18:24.466 10365-10365/com.example.android.qrking W/dalvikvm: threadid=1: calling UncaughtExceptionHandler
03-09 21:18:24.469 10365-10365/com.example.android.qrking E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.qrking, PID: 10365
java.lang.RuntimeException: Unable to pause activity {com.example.android.qrking/com.example.android.qrking.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3242)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3193)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3899)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.android.qrking.MainActivity.onPause(MainActivity.java:29)
at android.app.Activity.performPause(Activity.java:5368)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1239)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3224)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3193) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3899) 
at android.app.ActivityThread.access$900(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5292) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
at dalvik.system.NativeStart.main(Native Method) 
03-09 21:18:30.246 10365-10365/com.example.android.qrking I/Process: Sending signal. PID: 10365 SIG: 9

最佳答案

默认情况下,当更改方向时,您的 Activity 将重新启动,这可能会导致崩溃。

将此添加到您的 manifest.xml 文件中:

    android:configChanges="keyboardHidden|orientation|screenSize"

停止每次重新启动的 Activity 。

关于java - 当设备方向改变时,Qr 扫描应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42699528/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com