Android:启动Activity从onCreate失败,但从onActivityResult工作
问题描述:
我有一个ProductActivity.class,我想从各种活动启动。我可以从以下的ReaderActivity类中成功地从onActivityResult启动它,但无法从其他类的onCreate方法启动它。我错过了什么?Android:启动Activity从onCreate失败,但从onActivityResult工作
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);
}
}
}
无论从VerifiySuccessActivity和VerifyFailActivity
现在,如果我尝试推出ProductActivity.class,它崩溃以下
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)
答
此行导致崩溃
3210你应该检查你得到的附加项是否为空
String productSKU= getIntent().getExtras().getString("ID");
Bundle extras = getIntent().getExtras();
if (extras != null) {
productSku = extras.getString("ID");
}
你有一个NullPointerException。日志会告诉你你应该执行哪个语句。 – greenapps