0
我有一個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)
你有一個NullPointerException。日誌會告訴你你應該執行哪個語句。 – greenapps