2012-11-15 37 views
0

我想在android上捕捉一个布局文件图像并保存到SD卡。 在这里,如果我使用这段代码,我想捕获布局RelativeLayout,我的应用程序运行非常好。如何在android上保存布局?

<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 
+0

你能发布错误的堆栈跟踪吗? –

+0

清理项目并重建。有时R文件不会更新,因此它告诉您投下错误的小部件。而我没有看到你的'FrameLayout'有id。 –

回答

0

在代码中的某个点(我敢肯定,在这里

日志错误:Test.java: 43),您正在将一个FrameLayout错误地转换为Button。

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

0

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