所以我有一個imageview1,當它被點擊一個新的活動時,在下一個活動的相同圖像顯示在新的imageview2上時開始。被點擊的imageview1被我的mainactivity上的listview訪問。列表視圖由一個數組填充,每個列表視圖項都有一個相應的圖像。從imageview傳遞圖像到第二個活動時出錯
所以,當一個列表視圖項被點擊時,它開始一個新的活動imageview1駐留。該imageview使用引用可繪製圖像的xml typedarray進行填充。
我無法將圖像從我的第一個imageview傳遞到下一個新的活動。我得到一個空指針異常,我已經在下面列出了。我對編程非常陌生,我不確定如何解決這個問題。任何幫助將非常感激。
我的繼承人routeDetails類,其中包含第一ImageView的
public class RouteDetails extends AppCompatActivity {
ImageView routeImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_details);
//TextView for route details
final TextView routeDetailsView = (TextView) findViewById(R.id.routeDetailsView);
routeDetailsView.setText(getIntent().getExtras().getString("route"));
//ImageView for route details
routeImage = (ImageView) findViewById(R.id.routeImage);
routeImage.setImageResource(getIntent().getIntExtra("imageResourceId", 0));
//passing image to fullscreenimage
routeImage.setDrawingCacheEnabled(true);
routeImage.buildDrawingCache();
final Bitmap bitmap = routeImage.getDrawingCache();
////////// Trying to pass image to new activity
routeImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
Intent i = new Intent(RouteDetails.this, FullScreenImage.class);
//Bitmap _bitmap; // your bitmap
ByteArrayOutputStream _bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
i.putExtra("byteArray", _bs.toByteArray());
startActivity(i);
}
});
////////////
}
和我的繼承人fullscreenimage類即時通訊試圖將圖像傳遞到
public class FullScreenImage extends AppCompatActivity {
TouchImageView routeImageFull;
@Override
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.full_screen_image);
routeImageFull = (TouchImageView) findViewById(R.id.fullScreenImageView);
if(getIntent().hasExtra("byteArray")) {
TouchImageView routeImageFull= new TouchImageView(this);
Bitmap bitmap = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
routeImageFull.setImageBitmap(bitmap);
}
}
}
和繼承人的錯誤即時得到
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zach.listview, PID: 3429
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
at com.example.zach.listview.RouteDetails$1.onClick(RouteDetails.java:47)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
的【什麼是一個NullPointerException,如何解決呢?(可能的複製http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do- i-fix-it) –
請閱讀上面的鏈接以獲得有關如何計算錯誤的建議。你應該從RouteDetails.java的第47行開始。 –
'routeImage.getDrawingCache()'返回了null,顯然是 –