我有一个我希望从各种活动中启动的ProductActivity.class。我可以从onActivityResult内部的ReaderActivity类成功启动它,但不能从其他类的onCreate方法启动它。我错过了什么?
public class ReaderActivity extends AppCompatActivity {
private Button scan_btn, verifyButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
scan_btn = (Button) findViewById(R.id.scan_btn);
verifyButton = (Button) findViewById(R.id.buttonVerify);
final Activity activity = this;
scan_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((myApplication) activity.getApplication()).setWhichButton("scan");
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setCameraId(0);
integrator.initiateScan();
}
});
verifyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((myApplication) activity.getApplication()).setWhichButton("verify");
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setCameraId(0);
integrator.initiateScan();
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
String val = ((myApplication) this.getApplication()).getWhichButton();
if (result !=null) {
//....more code...
if (condition1) {
Intent intentVerifyFail = new Intent(getBaseContext(), VerifyFailActivity.class);
startActivity(intentVerifyFail);
}
if (condition2){
Intent intentVerifySuccess = new Intent(getBaseContext(), VerifySuccessActivity.class);
intentVerifySuccess.putExtra("ID", results);
startActivity(intentVerifySuccess);
}
}
if(condition3){
Intent intentProduct = new Intent(getBaseContext(), ProductActivity.class);
intentProduct.putExtra("ID", finalResults);
intentProduct.putExtra("SIGNER", accountOut);
intentProduct.putExtra("TIME", sTime);
startActivity(intentProduct);
}
}
}
如果我尝试启动ProductActivity.class,它现在来自VerifiySuccessActivity和VerifyFailActivity,它崩溃了
public class VerifySuccessActivity extends AppCompatActivity {
private Button OKButton;
private EditText productIDVerified;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verify_success);
OKButton = (Button) findViewById(R.id.buttonOKVerifySuccess);
productIDVerified = (EditText) findViewById(R.id.textSKUVerified);
String productSKU= getIntent().getExtras().getString("ID");
productIDVerified.setText(productSKU, TextView.BufferType.EDITABLE);
OKButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getBaseContext(), ProductActivity.class);
startActivity(intent);
}
});
}
}
以下错误代码:
FATAL EXCEPTION: main
Process: com.example.newpc.qrcode, PID: 24682
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newpc.qrcode/com.example.newpc.qrcode.ProductActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5631)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at com.example.newpc.qrcode.ProductActivity.onCreate(ProductActivity.java:21)
at android.app.Activity.performCreate(Activity.java:6092)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5631)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
答案 0 :(得分:1)
此行导致崩溃
String productSKU= getIntent().getExtras().getString("ID");
您应该检查您获得的额外内容是否为空
String productSKU= getIntent().getExtras().getString("ID");
Bundle extras = getIntent().getExtras();
if (extras != null) {
productSku = extras.getString("ID");
}