如何在Android上保存布局?

时间:2012-11-15 01:43:07

标签: android android-layout

我想在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());
        }

}

}

当我想要捕获framelayout

记录错误

> 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

2 个答案:

答案 0 :(得分:0)

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

仔细检查您正在使用的ID(在您的布局中,以及当您找到ViewById时在Activiy中)。

答案 1 :(得分:0)

如果你使用eclipse清理你的项目。当xml编辑器搞砸时,会出现转换问题。让我永远弄清楚为什么会发生铸造错误!