问题
In my app, I am changing the brightness of an image within an ImageView in Activity B and I'm passing the image as well as the brightness changed to Activity A. But when recieving the result in Activity A, I get a NullPointerException.
Here is part of code in Activity A:
On a button click:
btn_Edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent editIntent = new Intent(HowItWorksActivity.this,
EditPhotoActivity.class);
if (current_working_image_filename != null) {
editIntent.putExtra("current_image_filename",
current_working_image_filename);
startActivityForResult(editIntent, EDITED_PICTURE);
}
}
In onActivityResult:
if(resultCode==RESULT_OK && requestCode == EDITED_PICTURE){
Log.e("EDITED_PIC", "IN onactivityresult");
imageWithBorder = data.getStringExtra("ImageWithBorder");
metrics = getResources().getDisplayMetrics();
postcard_image = decodeSampledBitmapFromFileString(imageWithBorder,
metrics.widthPixels, metrics.heightPixels);
rl.setBackgroundDrawable(drawable);
brightness = data.getIntExtra("Image_Brightness", 0);
Log.e("brightness level", "IN onactivityresult "+brightness);
if(brightness!=0){
postcard_image = EditPhotoActivity.doBrightness(postcard_image,
brightness);
}
putGestureImageOnScreen(postcard_image);
}
In Activity B:
In onclick of a button:
btn_filter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("EditPhoto", "counter value = " + counter);
counter++;
sbarBrightness = (SeekBar)
findViewById(R.id.seekBarForBrightness);
if (counter % 2 == 0) {
sbarBrightness.setVisibility(View.INVISIBLE);
} else {
sbarBrightness.setVisibility(View.VISIBLE);
}
sbarBrightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar,int progress, boolean
fromUser) {
brightness = progress;
putGestureImageOnScreen(doBrightness(imageBitmap, brightness));
}
});
}
});
public static Bitmap doBrightness(Bitmap src, int value) {
int width = src.getWidth();
int height = src.getHeight();
Bitmap bmout = Bitmap.createBitmap(width, height, src.getConfig());
int A, R, G, B;
int pixel;
for (int i = 0; i < width; ++i) {
for (int j = 0; j < height; ++j) {
pixel = src.getPixel(i, j);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
R += value;
if (R > 255) {
R = 255;
} else if (R < 0) {
R = 0;
}
G += value;
if (G > 255) {
G = 255;
} else if (G < 0) {
G = 0;
}
B += value;
if (B > 255) {
B = 255;
} else if (B < 0) {
B = 0;
}
bmout.setPixel(i, j, Color.argb(A, R, G, B));
}
}
return bmout;
}
Here is the logcat output:
02-26 13:57:07.442: E/SEnding brightness(12750): br = 37
02-26 13:57:07.492: E/EDITED_PIC(12750): IN onactivityresult
02-26 13:57:07.492: E/brightness level(12750): IN onactivityresult 37
02-26 13:57:07.502: E/AndroidRuntime(12750): FATAL EXCEPTION: main
02-26 13:57:07.502: E/AndroidRuntime(12750): java.lang.RuntimeException: Failure
delivering result ResultInfo{who=null, request=300, result=-1, data=Intent {
cmp=com.sample.postcare2/.HowItWorksActivity (has extras) }} to activity
{com.sample.postcare2/com.sample.postcare2.HowItWorksActivity}:
java.lang.NullPointerException
02-26 13:57:07.502: E/AndroidRuntime(12750): at
android.app.ActivityThread.deliverResults(ActivityThread.java:3387)
02-26 13:57:07.502: E/AndroidRuntime(12750): at
android.app.ActivityThread.handleSendResult(ActivityThread.java:3437)
02-26 13:57:07.502: E/AndroidRuntime(12750): at
android.app.ActivityThread.access$1100(ActivityThread.java:139)
02-26 13:57:07.502: E/AndroidRuntime(12750): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1291)
02-26 13:57:07.502: E/AndroidRuntime(12750): at
android.os.Handler.dispatchMessage(Handler.java:99)
02-26 13:57:07.502: E/AndroidRuntime(12750): at
android.os.Looper.loop(Looper.java:154)
02-26 13:57:07.502: E/AndroidRuntime(12750): at
android.app.ActivityThread.main(ActivityThread.java:4944)
02-26 13:57:07.502: E/AndroidRuntime(12750): at
java.lang.reflect.Method.invokeNative(Native Method)
02-26 13:57:07.502: E/AndroidRuntime(12750): at
java.lang.reflect.Method.invoke(Method.java:511)
02-26 13:57:07.502: E/AndroidRuntime(12750): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-26 13:57:07.502: E/AndroidRuntime(12750): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-26 13:57:07.502: E/AndroidRuntime(12750): at
dalvik.system.NativeStart.main(Native Method)
02-26 13:57:07.502: E/AndroidRuntime(12750): Caused by: java.lang.NullPointerException
02-26 13:57:07.502: E/AndroidRuntime(12750): at
com.sample.postcare2.EditPhotoActivity.doBrightness(EditPhotoActivity.java:393)
02-26 13:57:07.502: E/AndroidRuntime(12750): at
com.sample.postcare2.HowItWorksActivity.onActivityResult(HowItWorksActivity.java:533)
02-26 13:57:07.502: E/AndroidRuntime(12750): at
android.app.Activity.dispatchActivityResult(Activity.java:4740)
02-26 13:57:07.502: E/AndroidRuntime(12750): at
android.app.ActivityThread.deliverResults(ActivityThread.java:3383)
02-26 13:57:07.502: E/AndroidRuntime(12750): ... 11 more
Please help me to solve the NullpointerException problem.
Thanks.
来源:https://stackoverflow.com/questions/22032914/not-able-to-get-rid-of-nullpointerexception-when-changing-brightness-of-an-image