0

How can I create 2 surfaceview which splitter up half of the screen on android? e.g each of them takes up 1/2 of the screen.

I get this exception: My app works when I do this:

<?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>

as soon as I switch to this, even without using '@+id/view2', like this:

<?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>

I get a NullPointer exception from this:

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]  
4

1 回答 1

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。

于 2013-02-04T22:16:07.580 回答