嘿我想将位图传递给另一个活动。为了避免“Failed binder transaction”异常,我压缩了位图。但是现在我得到了一个nullpointer异常,虽然我可以在imageview中显示图像,因此它包含数据。为了测试,我只传了一个字符串,工作正常。 谢谢你的回答。
MainActivity.java
//Here I want to pass the bitmap
topBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
camera.release();
bottomBitmap=MainActivity.scaleDownBitmap(bottomBitmap, 100,getApplicationContext());
Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
intent.putExtra("bottomBitmap", bottomBitmap);
startActivity(intent);
//Method to compress bitmap
public static Bitmap scaleDownBitmap(Bitmap photo, int newHeight, Context context) {
final float densityMultiplier = context.getResources().getDisplayMetrics().density;
int h= (int) (newHeight*densityMultiplier);
int w= (int) (h * photo.getWidth()/((double) photo.getHeight()));
photo=Bitmap.createScaledBitmap(photo, w, h, true);
return photo;
}
DetailActivity.java
private Bitmap bottomBitmap;
private ImageView bottomImage;
//Receive Data
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_activity);
Intent intent = getIntent();
this.bottomBitmap = (Bitmap) intent.getParcelableExtra("bottomBitmap");
this.bottomImage.setImageBitmap(this.bottomBitmap);
}
日志:
04-28 21:56:13.317: E/AndroidRuntime(19456): FATAL EXCEPTION: main
04-28 21:56:13.317: E/AndroidRuntime(19456): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.campic/com.example.campic.DetailActivity}: java.lang.NullPointerException
04-28 21:56:13.317: E/AndroidRuntime(19456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2517)
04-28 21:56:13.317: E/AndroidRuntime(19456): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
04-28 21:56:13.317: E/AndroidRuntime(19456): at android.app.ActivityThread.access$600(ActivityThread.java:162)
04-28 21:56:13.317: E/AndroidRuntime(19456): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1413)
04-28 21:56:13.317: E/AndroidRuntime(19456): at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 21:56:13.317: E/AndroidRuntime(19456): at android.os.Looper.loop(Looper.java:158)
04-28 21:56:13.317: E/AndroidRuntime(19456): at android.app.ActivityThread.main(ActivityThread.java:5789)
04-28 21:56:13.317: E/AndroidRuntime(19456): at java.lang.reflect.Method.invokeNative(Native Method)
04-28 21:56:13.317: E/AndroidRuntime(19456): at java.lang.reflect.Method.invoke(Method.java:525)
04-28 21:56:13.317: E/AndroidRuntime(19456): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
04-28 21:56:13.317: E/AndroidRuntime(19456): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
04-28 21:56:13.317: E/AndroidRuntime(19456): at dalvik.system.NativeStart.main(Native Method)
04-28 21:56:13.317: E/AndroidRuntime(19456): Caused by: java.lang.NullPointerException
04-28 21:56:13.317: E/AndroidRuntime(19456): at com.example.campic.DetailActivity.onCreate(DetailActivity.java:22)
04-28 21:56:13.317: E/AndroidRuntime(19456): at android.app.Activity.performCreate(Activity.java:5195)
04-28 21:56:13.317: E/AndroidRuntime(19456): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
04-28 21:56:13.317: E/AndroidRuntime(19456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2473)
04-28 21:56:13.317: E/AndroidRuntime(19456): ... 11 more
答案 0 :(得分:1)
在DetailActivity
:
private ImageView bottomImage;
...
this.bottomImage.setImageBitmap(this.bottomBitmap);
bottomImage
永远不会被初始化。您可能需要先使用findViewById
初始化变量,然后再调用setImageBitmap
。