阅读器在扫描时工作正常,但是如果我按回去,应用程序将崩溃。
package com.example.vict.tst;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class MainActivity extends AppCompatActivity {
Button btnScan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnScan = (Button) findViewById(R.id.btnScan);
final Activity activity = this;
btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt("SCANNER!");
integrator.setCameraId(0);
integrator.initiateScan();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
if (result != null)
{
if (result.getContents().equals("HELLO WORLD"))
{
alert(result.getContents());
}else{
alert("CANCELED");
}
}else {
super.onActivityResult(requestCode, resultCode, data);
}
}
private void alert(String msg)
{
Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_LONG).show();
}
}
我已经在finish()
中尝试ELSE
,但似乎无济于事。该应用程序无故崩溃。
如果扫描成功,则应用程序将继续正常运行。
如果是错误消息,表明QR CODE读取了该应用程序仍然可以正常工作(返回活动以再次按下按钮)
但是,如果我按“后退”按钮,应用程序将关闭。
LOGCAT:
06-26 00:48:39.091 26377-26377/com.example.vict.tst E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.victi.testeinuteis, PID: 26377
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=49374, result=0, data=null} to activity {com.example.victi.testeinuteis/com.example.victi.testeinuteis.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:5007)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5050)
at android.app.ActivityThread.access$1600(ActivityThread.java:230)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1876)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7409)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.example.victi.testeinuteis.MainActivity.onActivityResult(MainActivity.java:43)
at android.app.Activity.dispatchActivityResult(Activity.java:7165)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5003)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5050)
at android.app.ActivityThread.access$1600(ActivityThread.java:230)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1876)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7409)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)