我有一个很大的问题,我正在捕获一个Bitmap图像的视图,但无法访问该视图。意味着我们无法获得它的高度或宽度。请告诉我问题出在哪里?
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="match_parent" android:background="#ffffff">
<FrameLayout android:id="@+id/frameLayout1"
android:layout_width="fill_parent" android:layout_height="40dip"
android:background="@color/Red">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/tv_Previrew_Header"
android:text="Greeting" android:textColor="#ffffff"
android:textStyle="bold" android:textSize="18dip"
android:layout_gravity="center_vertical|center_horizontal"></TextView>
</FrameLayout>
<FrameLayout android:id="@+id/fr"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/btn_email"
android:background="@drawable/email2x" android:layout_gravity="bottom"></Button>
<ImageView android:id="@+id/img_Preview_Image"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_height="270dip" android:layout_width="250dip"></ImageView>
<TextView android:text="TextView" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/tv_Preview_Greeting"
android:layout_gravity="top|left" android:textColor="@color/Red" android:textSize="20dip" android:textStyle="bold" android:layout_marginLeft="35dip" android:layout_marginTop="40dip"></TextView>
<ImageView android:layout_width="wrap_content" android:src="@drawable/halosys_logo_bottom" android:layout_height="wrap_content" android:id="@+id/imageView1" android:layout_gravity="right" android:layout_marginTop="320dip" android:layout_marginRight="40dip"></ImageView>
</FrameLayout>
</LinearLayout>
代码:
FrameLayout view = (FrameLayout)findViewById(R.id.fr);
Bitmap bm = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
bm.compress(CompressFormat.JPEG, 95, new FileOutputStream("/sdcard/image.jpg"));
错误堆栈:
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): FATAL EXCEPTION: main
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.greetingApp.common/com.greetingApp.common.Greeting}: java.lang.IllegalArgumentException: width and height must be > 0
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): at android.os.Looper.loop(Looper.java:123)
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): at java.lang.reflect.Method.invokeNative(Native Method)
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): at java.lang.reflect.Method.invoke(Method.java:521)
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): at dalvik.system.NativeStart.main(Native Method)
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): Caused by: java.lang.IllegalArgumentException: width and height must be > 0
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): at android.graphics.Bitmap.nativeCreate(Native Method)
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): at com.greetingApp.common.Greeting.SaveData(Greeting.java:109)
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): at com.greetingApp.common.Greeting.onCreate(Greeting.java:81)
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-28 17:16:17.955: ERROR/AndroidRuntime(25935): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
答案 0 :(得分:0)
您是否在Activity.onCreate方法中运行代码?如果是,那么此时您的视图的维度尚不清楚。在计算布局大小后,您应该在稍后阶段执行此代码