Android:从onCreate启动活动失败,但从onActivityResult启动活动有效

Android:从onCreate启动活动失败,但从onActivityResult启动活动有效,android,Android,我有一个ProductActivity.class,我想从各种活动中启动它。我可以从下面的ReaderActivity类从ActivityResult内部成功启动它,但不能从其他类的onCreate方法启动它。我错过了什么 public class ReaderActivity extends AppCompatActivity { private Button scan_btn, verifyButton; @Override protected void onCreate

我有一个ProductActivity.class,我想从各种活动中启动它。我可以从下面的ReaderActivity类从ActivityResult内部成功启动它,但不能从其他类的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);
}
}
}
现在,从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) 

这条线引起了车祸

String productSKU= getIntent().getExtras().getString("ID");
你应该检查你得到的额外数据是否为空

String productSKU= getIntent().getExtras().getString("ID");
Bundle extras = getIntent().getExtras();
if (extras != null) {
    productSku = extras.getString("ID");
}

您有一个NullPointerException。日志会告诉您应该执行哪条语句。