我从另一个类里面的类调用startActivityForResult()时遇到问题(Null Pointer Exception),这里是代码:
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();
}
}
所以请告诉我,如果我做错了什么!!
Logcat
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)
答案 0 :(得分:5)
您将OCRActivity
视为普通的Java类,而不是另一个Activity。因此,当您致电startActivityForResult()
时,您会收到一个NPE,因为尚未调用活动的onCreate()
,这意味着其当前实例无法调用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()
这样的函数(以及任何其他不需要在活动本身中的函数,并且可以通过接收数据作为参数并将其返回)到单独的类中进行调用(调用它实用程序或其他)。
答案 1 :(得分: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();
} }