如何创建2个surfaceview,它在Android上分割了一半的屏幕?

时间:2013-02-04 21:56:38

标签: android

如何创建2个surfaceview,它在Android上分割了一半的屏幕? 例如,每个占据屏幕的1/2。

我得到这个例外: 我的应用程序在我这样做时有效:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
    <com.example.android.apis.graphics.Preview android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1" android:id="@+id/view1" />

</LinearLayout>

一切换到这个,即使不使用'@ + id / view2',也是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
    <com.example.android.apis.graphics.Preview android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1" android:id="@+id/view1" />


    <com.example.android.apis.graphics.Preview android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1" android:id="@+id/view2" />

</LinearLayout>

我从中得到一个NullPointer异常:

Choreographer.doCallbacks(int, long) line: 558  
Choreographer.doFrame(long, int) line: 525  
Choreographer$FrameDisplayEventReceiver.run() line: 711 
Handler.handleCallback(Message) line: 615   
Choreographer$FrameHandler(Handler).dispatchMessage(Message) line: 92   
Looper.loop() line: 137 
ActivityThread.main(String[]) line: 4921    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 511  
ZygoteInit$MethodAndArgsCaller.run() line: 1038 
ZygoteInit.main(String[]) line: 805 
NativeStart.main(String[]) line: not available [native method]  

1 个答案:

答案 0 :(得分:0)

<LinearLayout android:layoutHeight="fill_parent"
              android:layoutWidth="fill_parent"
              android:orientation="horizontal" >
    <SurfaceView1 android:layoutWidth="fill_parent"
                  android:layoutHeight="fill_parent"
                  android:layoutWeight="1" />
    <SurfaceView2 android:layoutWidth="fill_parent"
                  android:layoutHeight="fill_parent"
                  android:layoutWeight="1" />
</LinearLayout>

重要的部分是使它们的重量和fill_parent都相同。要解决Android中的奇怪错误,您可能需要指定布局的宽度为0dip。