0

我想在 android 上捕获一个布局文件图像并保存到 sd 卡。在这里,如果我使用此代码,我想要捕获布局 RelativeLayout,我的应用程序运行得很好。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="*" >
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <Button
            android:id="@+id/ChoosePictureButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Sign" />
        <Button
            android:id="@+id/Clear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Clear" />
        <Button
            android:id="@+id/SavePictureButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Save" />
    </TableRow>
</TableLayout>
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="870dp"
    android:layout_alignParentLeft="true"
    android:layout_centerInParent="true" >
    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:src="@drawable/sample" />
</FrameLayout>
<com.example.testsign.SignatureView
    android:id="@+id/sign"
    android:layout_width="match_parent"
    android:layout_height="170dp"
    android:layout_alignParentBottom="true"
    android:visibility="invisible" >
</com.example.testsign.SignatureView>

但是当我想捕获 Framelayout 时,我的应用程序出错了。我不明白为什么?你能帮帮我吗!

源代码:

public class Test extends Activity implements OnClickListener {

private SignatureView sign;
private Button choosePicture;
private Button savePicture;
private Button clear;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    int currentOrientation = getResources().getConfiguration().orientation;
    if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    }
    else {
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    }
    choosePicture = (Button) this.findViewById(R.id.ChoosePictureButton);
    savePicture = (Button) this.findViewById(R.id.SavePictureButton);
    savePicture.setOnClickListener(this);
    choosePicture.setOnClickListener(this);
    clear=(Button)findViewById(R.id.Clear);
    clear.setOnClickListener(this);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_test, menu);
    return true;
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

    if(arg0==choosePicture)
    {
        sign = (SignatureView) findViewById(R.id.sign);
        sign.setBackgroundColor(0xFFFFFFFF);
        sign.setVisibility(View.VISIBLE);

    }else if(arg0==clear)
    {
        //sign.setVisibility(View.INVISIBLE);
        sign.clearSignature();
    }else if(arg0==savePicture)
    {   
        Toast.makeText(this, "Saved!!!Thank you!!!", Toast.LENGTH_LONG).show();
        captureScreen();

    }
}
private void captureScreen() {
      RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main);
      Bitmap b = Bitmap.createBitmap(mainLayout.getWidth(), mainLayout
            .getHeight(), Bitmap.Config.ARGB_8888);
      Canvas c = new Canvas(b);
      mainLayout.draw(c);
      ContentValues contentValues = new ContentValues(3);
      contentValues.put(Media.DISPLAY_NAME, "Draw On Me");
      Uri imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, contentValues);
      try {
          OutputStream imageFileOS = getContentResolver().openOutputStream(imageFileUri);
          b.compress(CompressFormat.JPEG, 90, imageFileOS);
          Toast t = Toast.makeText(this, "Thank you! Image Saved!", Toast.LENGTH_LONG);
          t.show();
        } catch (Exception e) {
          Log.v("EXCEPTION", e.getMessage());
        }

}

}

当我想要捕获帧布局时记录错误

> 11-15 09:00:30.492: W/dalvikvm(4364): threadid=1: thread exiting with uncaught exception (group=0x40bda1f8)

11-15 09:00:30.492: E/AndroidRuntime(4364): FATAL EXCEPTION: main

11-15 09:00:30.492: E/AndroidRuntime(4364): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testsign/com.example.testsign.Test}: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.widget.Button

11-15 09:00:30.492: E/AndroidRuntime(4364): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)

11-15 09:00:30.492: E/AndroidRuntime(4364): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)

11-15 09:00:30.492: E/AndroidRuntime(4364): at android.app.ActivityThread.access$600(ActivityThread.java:128)

11-15 09:00:30.492: E/AndroidRuntime(4364): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)

11-15 09:00:30.492: E/AndroidRuntime(4364): at android.os.Handler.dispatchMessage(Handler.java:99)

11-15 09:00:30.492: E/AndroidRuntime(4364): at android.os.Looper.loop(Looper.java:137)

11-15 09:00:30.492: E/AndroidRuntime(4364): at android.app.ActivityThread.main(ActivityThread.java:4514)
11-15 09:00:30.492: E/AndroidRuntime(4364): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 09:00:30.492: E/AndroidRuntime(4364): at java.lang.reflect.Method.invoke(Method.java:511)

11-15 09:00:30.492: E/AndroidRuntime(4364): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)

11-15 09:00:30.492: E/AndroidRuntime(4364): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)

11-15 09:00:30.492: E/AndroidRuntime(4364): at dalvik.system.NativeStart.main(Native Method)

11-15 09:00:30.492: E/AndroidRuntime(4364): Caused by: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.widget.Button

11-15 09:00:30.492: E/AndroidRuntime(4364): at com.example.testsign.Test.onCreate(Test.java:43)

11-15 09:00:30.492: E/AndroidRuntime(4364): at android.app.Activity.performCreate(Activity.java:4465)

11-15 09:00:30.492: E/AndroidRuntime(4364): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)

11-15 09:00:30.492: E/AndroidRuntime(4364): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)

11-15 09:00:30.492: E/AndroidRuntime(4364): ... 11 more
4

2 回答 2

0

如果您使用 eclipse 清理您的项目。当 xml 编辑器出现问题时,就会出现转换问题。花了我很长时间才弄清楚为什么会发生铸造错误!

于 2013-05-24T02:43:25.573 回答
0

在您的代码中的某个时刻(我在这里很确定:Test.java:43),您将 FrameLayout 错误地转换为 Button。

仔细检查您正在使用的 id(在您的布局中,当您 findViewById 时在您的 Activiy 中)。

于 2012-11-15T02:22:27.537 回答