0

在单击按钮的以下代码中,我想隐藏相对布局rl1并显示rl2,但是单击按钮后我的应用程序崩溃了。我做错了什么

 <?xml version="1.0" encoding="utf-8"?>

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
<RelativeLayout 
android:id="@+id/rl1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/welcome_1">

    <ImageView
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_alignParentRight="true"

        android:paddingTop="360dp"
        android:layout_marginRight="4dp"
        android:src="@drawable/button1" />

     </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl2"
         android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="invisible"
     android:background="@drawable/menu_2"
        ></RelativeLayout>
     </RelativeLayout>

Java 代码

public class mockupsActivity extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


 // ImageButton img1 = (ImageButton)findViewById(R.id.button1);
    ImageView img1 =(ImageView)findViewById(R.id.button1);
    img1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
           // your code here
   //       Toast.makeText(TouchpointmockupsActivity.this, "test", Toast.LENGTH_SHORT).show();
            LinearLayout rl1 = (LinearLayout) findViewById(R.id.rl1);
            rl1.setVisibility(View.INVISIBLE);
            LinearLayout rl2 = (LinearLayout) findViewById(R.id.rl2);
            rl2.setVisibility(View.VISIBLE);
        }
    });





}
}
4

1 回答 1

5

在您的代码中更改LinearLayout 为。RelativeLayout你可能会得到一个 classcastException。

编辑:更改 OnClick 方法如下

        RelativeLayout rl1 = (RelativeLayout) findViewById(R.id.rl1);
        rl1.setVisibility(View.INVISIBLE);
        RelativeLayout rl2 = (RelativeLayout) findViewById(R.id.rl2);
        rl2.setVisibility(View.VISIBLE);
于 2012-08-25T20:49:27.087 回答