我需要在新活动中点击放大图像我从服务器下载这些图像,然后以编程方式将它们添加到主视图中,从而明确地设置它们的ID。但我的问题是它只是选择最后一个图像的id,因此给出了异常。我在它获取图像的活动中使用以下代码。 :
private OnClickListener clickImageListener = new OnClickListener() {
public void onClick(View v) {
Intent fullScreenIntent = new Intent(v.getContext(),FullImageActivity.class);
fullScreenIntent.putExtra(ProfilePageNormalUser.class.getName(),imageId) ;
ProfilePageNormalUser.this.startActivity(fullScreenIntent);
System.out.println("going to fullimageactivity");
}
};
以及要放大图像的以下代码。
Intent intent = getIntent();
int imageId = (Integer) intent.getExtras().get(ProfilePageNormalUser.class.getName());
imageView.setLayoutParams( new ViewGroup.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));
InputStream is = this.getResources().openRawResource(imageId);
Bitmap originalBitmap = BitmapFactory.decodeStream(is);
ImageView myimage = (ImageView) findViewById(R.id.imageviewEnlarged);
myimage.setImageBitmap(originalBitmap);
myimage.setScaleType(ScaleType.FIT_XY);
但它给了我以下例外:
编辑:它也在堆栈跟踪中给出了以下异常:
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emx.OnDaMove/com.emx.OnDaMove.FullImageActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x4
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): at android.os.Looper.loop(Looper.java:130)
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): at java.lang.reflect.Method.invokeNative(Native Method)
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): at java.lang.reflect.Method.invoke(Method.java:507)
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): at dalvik.system.NativeStart.main(Native Method)
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x4
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): at android.content.res.Resources.getValue(Resources.java:892)
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): at android.content.res.Resources.openRawResource(Resources.java:817)
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): at android.content.res.Resources.openRawResource(Resources.java:799)
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): at com.emx.OnDaMove.FullImageActivity.onCreate(FullImageActivity.java:25)
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): ... 11 more
答案 0 :(得分:0)
我只能猜测而不会看到堆栈跟踪
findViewById(R.id.imageviewEnlarged);// maybe here?...
我是Android的新手,但例外情况是告诉你ViewGroup$LayoutParams
不会去你所在的位置,或者你试图把那些不会去那里的东西
答案 1 :(得分:0)
我认为你的问题在于ResourceNotFoundException。这是堆栈跟踪中的行:
11-01 10:25:51.323: ERROR/AndroidRuntime(5001): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x4
确保您在此行中获取正确的图片ID:
int imageId = (Integer) intent.getExtras().get(ProfilePageNormalUser.class.getName());