我寫了下面的代碼onActivityResult方法調用CropImage.class這將給裁剪後的圖像,但我同時從作物的活動返回Add_Customer活動的Android onActivityResult錯誤
調用CropImage活動與經過3中面臨的一個錯誤下面的方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(getApplicationContext(), "Called Succesfully..." + requestCode, Toast.LENGTH_SHORT).show();
if (requestCode == 1)
{
Intent intent = new Intent(Add_Customer.this,CropImage.class);
startActivityForResult(intent, 3);
}
if (requestCode ==3)
{
Toast.makeText(getApplicationContext(),"Photo Added Succesfully...", Toast.LENGTH_SHORT).show();
File f = new File(Environment.DIRECTORY_PICTURES);
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath(),
bitmapOptions);
viewImage.setImageBitmap(bmp);
}
}
CropImage活動的onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop_image);
final CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
final ImageView croppedImageView = (ImageView)findViewById(R.id.croppedImageView);
// Set image for cropping
File f = new File(Environment.getExternalStorageDirectory().toString());
for (File temp : f.listFiles())
{
if (temp.getName().equals("temp.jpg"))
{
f = temp;
break;
}
}
try
{
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
bitmapOptions);
cropImageView.setImageBitmap(bitmap);
//deleting image captured by camera
f.delete();
} catch (Exception e) {
e.printStackTrace();
}
Button cropButton = (Button)findViewById(R.id.crop_button);
cropButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get cropped image, and show result.
croppedImageView.setImageBitmap(cropImageView.getCroppedBitmap());
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
Bitmap crop = cropImageView.getCroppedBitmap();
OutputStream outFile = null;
File file = new File(Environment.DIRECTORY_PICTURES, "temp.jpg");
try {
outFile = new FileOutputStream(file);
crop.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
outFile.flush();
outFile.close();
} catch (Exception e) {
e.printStackTrace();
}
Intent intentMessage = new Intent();
// put the message in Intent
intentMessage.putExtra("MESSAGE", "hello");
intentMessage.putExtra("image", crop);
setResult(3, intentMessage);
// finish The activity
finish();
}
});
}
錯誤:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=3, data=Intent { (has extras) }} to activity {loginscreen.example.com.girviapp/loginscreen.example.com.girviapp.Add_Customer}: java.lang.NullPointerException: Attempt to get length of null array
at android.app.ActivityThread.deliverResults(ActivityThread.java:3539)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582)
at android.app.ActivityThread.access$1300(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at loginscreen.example.com.girviapp.Add_Customer.onActivityResult(Add_Customer.java:196)
at android.app.Activity.dispatchActivityResult(Activity.java:6135)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3535)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582)
at android.app.ActivityThread.access$1300(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
但是我應該在哪裏通過3然後 –
你不需要通過。 Android記住您已通過「startActivityForResult(intent,3)」的請求代碼;「 –
更改後的eRRor: java.lang.RuntimeException:未將結果resultInfo {who = null,request = 3,result = -1,data = Intent {(has extras)}}傳遞給活動 –