1

从另一个类中的类调用 startActivityForResult() 时遇到问题(空指针异常),代码如下:

public class OCRActivity extends Activity {

public OCRActivity(String operator)
{
    this.operator = operator;
}


public void startCameraActivity() {

    final Intent capture_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(capture_intent, 1); // I get the Null pointer Exception here
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {

        if(requestCode == 1){
            //get the Uri for the captured image
            picUri = data.getData();
            //carry out the crop operation
            performCrop();
        }
}

这是我打电话的地方startCameraActivity()

public class WayToFillActivity extends Activity implements OnClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.way_to_fill);

    CAMERA_BTN = (Button) findViewById(R.id.camera_btn);
    CAMERA_BTN.setOnClickListener(this);
    Intent operator_intent =  getIntent();
    OPERATOR = operator_intent.getStringExtra("operator");
}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.camera_btn)
    {
        OCRActivity ocr = new OCRActivity(OPERATOR);
        ocr.startCameraActivity();

    }

}

所以如果我做错了什么请告诉我!

日志猫

01-13 16:22:26.583: E/AndroidRuntime(32425): java.lang.NullPointerException
01-13 16:22:26.583: E/AndroidRuntime(32425):    at     android.app.Activity.startActivityForResult(Activity.java:3190)
01-13 16:22:26.583: E/AndroidRuntime(32425):    at com.almannaa.EasyRecharge.OCRActivity.startCameraActivity(OCRActivity.java:176)
4

2 回答 2

5

您将OCRActivity其视为一个普通的 Java 类,而不是另一个 Activity。因此,当您调用时,startActivityForResult()您会得到一个 NPE,因为 ActivityonCreate()尚未被调用,这意味着它的当前实例对调用无效startActivityForResult()

相反,请尝试使用:

public class WayToFillActivity extends Activity implements OnClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.way_to_fill);

    CAMERA_BTN = (Button) findViewById(R.id.camera_btn);
    CAMERA_BTN.setOnClickListener(this);
    Intent operator_intent =  getIntent();
    OPERATOR = operator_intent.getStringExtra("operator");
}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.camera_btn)
    {
        OCRActivity ocr = new OCRActivity(OPERATOR);
        ocr.startCameraActivity();

    }

}

public void startCameraActivity() {

    final Intent capture_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(capture_intent, 1); // I get the Null pointer Exception here
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {

        if(requestCode == 1){
            //get the Uri for the captured image
            picUri = data.getData();
            //carry out the crop operation
            performCrop();
        }
}
}

您可能应该将函数performCrop()(以及实际上不需要在活动本身中的任何其他函数,并且可以通过接收数据作为参数并返回它来工作)移动到一个单独的类中(称为实用程序或其他)。

于 2013-01-13T17:27:50.513 回答
0

也许您应该检查数据是否不为空。

@Override        
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1 && data != null) {

        if(resultCode == RESULT_OK){
            //get the Uri for the captured image
            picUri = data.getData();
            //carry out the crop operation
            performCrop();
        } }
于 2013-01-13T17:31:46.840 回答