2012-07-09 26 views
0

我遇到了setContentView问题,它引发了一个空指针。在OnLongClick监听器中的setContentView

在我的mainFrame中,我得到了一个小型的SurfaceView,它占用了大约50%的屏幕。 我想实现一种方法,如果你长时间点击它,那么SurfaceView就会变成全屏。我这样做是这样的:

final FrameLayout frame = (FrameLayout) findViewById(R.id.frame_layout); 
frame.setOnLongClickListener(new OnLongClickListener() { 
    boolean clicked = false; 
    @Override 
    public boolean onLongClick(View v) { 
     if (clicked){ 
      clicked = false; 
      (MyActivity.this).setContentView(R.layout.main);  
     } else { 
      clicked = true; 
      (MyActivity.this).setContentView(R.layout.fullScreen); 
     } 

     return true; 
    } 
}); 

有人能帮助我如何解决这个问题?

问候

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/RelativeLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="right" 
android:orientation="vertical" 
android:weightSum="1" 
android:background="@drawable/background_new" > 

<TextView 

    android:text="@string/measures" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/text_measures" 
    android:textColor="#ff444444" 
    android:layout_marginTop="30dp" 
    android:layout_marginLeft="10dp" 
    /> 

<ImageButton 
android:contentDescription="@string/desc_edge" 
android:layout_toRightOf="@id/text_measures" 
android:id="@+id/edges_button" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/edges_black" 
android:layout_marginTop="10dp" 
android:layout_marginLeft="10dp" 
android:layout_marginBottom="30dp" 
android:onClick="edge_handler" 
android:background="@null"    
/> 

<FrameLayout 
    android:background="@xml/border" 
    android:id="@+id/camera_preview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="10dp" 
    android:layout_marginBottom="35dp" 
    android:layout_marginRight="200dp" 
    android:layout_marginTop="80dp" 
    android:layout_below="@id/text_measures"   
    > 
    </RelativeLayout> 

我知道它看起来像一个FrameLayout里,但不要上当它是由别人完成的,它实际上是一个SurfaceView。

+0

看到这个https://groups.google.com/forum/?fromgroups#!topic/android-developers/wj4uc0J9yT0 – Sameer 2012-07-09 09:28:02

+0

某种程度上是没有帮助解决我的问题 – 2012-07-09 09:37:00

+0

的setContentView()没有,直到视觉冲击* * onCreate()返回后返回 。如果你调用它两次,网络是用户将只看到 第二。 – SALMAN 2012-07-09 09:41:49

回答

0

只需改变SurfaceView onLong点击框架的布局参数即可。无需再次设置内容视图。

LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 

    surfaceView.setLayoutParams(lp); 
+0

我明白,但我现在得到一个ClassCast异常。我认为它是因为我的FrameLayout已经设置在Fill Parent上,但是因为我在与这些LayoutParams的RelativeLayout中使用它不会工作 – 2012-07-09 09:45:06

+0

发布你的布局文件,所以我可以建议你为什么会出错。 – 2012-07-09 09:50:51

+0

我发布我的xml以上 – 2012-07-09 10:00:41