尝试在null对象引用上调用虚方法

时间:2018-01-12 07:53:14

标签: java android

我正在尝试制作条形码扫描仪应用,我想将扫描结果添加到某个列表中。在MainActivity中,我有一个按钮,应该将我发送到MyList活动,但随后应用程序崩溃,我不知道如何解决它。

所以这是我的代码:

public class MainActivity extends AbsRuntimePermission implements ZXingScannerView.ResultHandler{

private ZXingScannerView zXingScannerView;
private static final int REQUEST_PERMISSION = 10;
boolean ok = false, chk = false;
private String scanResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    chk = getIntent().getBooleanExtra("check", chk);

    if(chk) {
        chk = false;

        ok = true;
        zXingScannerView = new ZXingScannerView(getApplicationContext());
        setContentView(zXingScannerView);
        zXingScannerView.setResultHandler(this);
        zXingScannerView.startCamera();
    }
}

public void scan(View view){
    requestAppPermissions(new String[]{Manifest.permission.CAMERA}, R.string.msg, REQUEST_PERMISSION);
    ok = true;
    zXingScannerView = new ZXingScannerView(getApplicationContext());
    setContentView(zXingScannerView);
    zXingScannerView.setResultHandler(this);
    zXingScannerView.startCamera();
}

@Override
public void onPermissionGranted(int requestCode) {
    if(ok)
        Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_LONG).show();
}

@Override
public void onResume(){
    super.onResume();

    if(ok)
    {
        if(zXingScannerView == null)
        {
            zXingScannerView = new ZXingScannerView(this);
            setContentView(zXingScannerView);
        }
        zXingScannerView.setResultHandler(this);
        zXingScannerView.startCamera();
    }
}

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

@Override
public void handleResult(final Result result) {
    scanResult = result.getText();
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Result");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            zXingScannerView.resumeCameraPreview(MainActivity.this);
        }
    });
    builder.setNeutralButton("GO", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            try
            {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(scanResult));
                startActivity(intent);
            }catch (Exception ex)
            {
                Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
                intent.putExtra(SearchManager.QUERY, scanResult);
                startActivity(intent);
            }
        }
    });

    builder.setNegativeButton("Add to list", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(MainActivity.this, MyList.class);
            intent.putExtra("CODE", scanResult);
            startActivity(intent);
        }
    });

    builder.setMessage(scanResult);
    AlertDialog alert = builder.create();
    alert.show();
}

public void Lista(View view){
    Intent iNtent = new Intent(MainActivity.this, MyList.class);
    startActivity(iNtent);
}

}` 

这是我的错误报告:

01-12 09:42:26.013 2670-2670/com.example.tchibo.justqr E/AndroidRuntime:        FATAL EXCEPTION: main

Process: com.example.tchibo.justqr, PID: 2670

java.lang.RuntimeException: Unable to pause activity {com.example.tchibo.justqr/com.example.tchibo.justqr.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void me.dm7.barcodescanner.zxing.ZXingScannerView.stopCamera()' on a null object reference                                                                          at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:3976)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3942)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3916)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3890)
at android.app.ActivityThread.-wrap15(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1605)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void me.dm7.barcodescanner.zxing.ZXingScannerView.stopCamera()' on a null object reference

实际上,它说它试图在void me.dm7.barcodescanner.zxing.ZXingScannerView.stopCamera()对象引用上调用虚方法null

(对不起帖子风格,但我不熟悉)

3 个答案:

答案 0 :(得分:1)

你在这个方法中创建方法scan(View view),你初始化你的相机(ZXingScannerView)对象,但不要在任何地方调用。寻找解决方案

onCreate():

中的

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    chk = getIntent().getBooleanExtra("check", chk);
    zXingScannerView = new ZXingScannerView(getApplicationContext());
    if(chk) {
        chk = false;
        ok = true;
        zXingScannerView = new ZXingScannerView(getApplicationContext());
        setContentView(zXingScannerView);
        zXingScannerView.setResultHandler(this);
        zXingScannerView.startCamera();
    }
}
onPause()

中的

@Override
protected void onPause() {
    super.onPause();
    if(zXingScannerView!=null)
    zXingScannerView.stopCamera();
}

答案 1 :(得分:0)

您可以在使用之前检查zXingScannerView。

if (zXingScannerView != null){
    zXingScannerView.stopCamera();
}

答案 2 :(得分:0)

java.lang.RuntimeException: Unable to pause activity 
{com.example.tchibo.justqr/com.example.tchibo.justqr.MainActivity}: 
java.lang.NullPointerException: Attempt to invoke virtual method 'void 
me.dm7.barcodescanner.zxing.ZXingScannerView.stopCamera()' on a null 
object reference    

logcat告诉你问题,这就是这一行:

zXingScannerView.stopCamera();

你的应用正试图阻止不存在的东西!!

简单的解决方案:

if(zXingScannerView!=null){
   zXingScannerView.stopCamera();
}