Android中的setVisibility(View.INVISIBLE)不起作用

时间:2013-02-12 14:33:00

标签: android view onclick visible invisible

两天来,我一直试图让setVisibility(View.INVISIBLE)功能正常工作。但是,如果我在模拟器中测试我的应用程序,它总是会崩溃。怎么了? (还有:arg0中视图vonClickListener之间的区别是什么?)

public class SpielActivity extends Activity {


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

    final Button startbutton = (Button)findViewById(R.id.anfangsbutton);
    final TextView initCountdown = (TextView)findViewById(R.id.initcountdown);

    startbutton.setVisibility(View.VISIBLE);

   startbutton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) { 
            startbutton.setVisibility(View.INVISIBLE);
             new CountDownTimer(3500, 1000) {

                 public void onTick(long millisUntilFinished) {
                     initCountdown.setText("" + millisUntilFinished / 1000);
                 }

                 public void onFinish() {
                     initCountdown.setText("");
                 }
              }.start();



        }
    });
    }

}

这是LogCat数据:

02-12 14:18:30.147: W/dalvikvm(332): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-12 14:18:30.147: E/AndroidRuntime(332): FATAL EXCEPTION: main
02-12 14:18:30.147: E/AndroidRuntime(332): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.JodomStudios.cloudgame/com.JodomStudios.cloudgame.SpielActivity}: java.lang.ClassCastException: android.widget.ImageButton
02-12 14:18:30.147: E/AndroidRuntime(332):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-12 14:18:30.147: E/AndroidRuntime(332):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-12 14:18:30.147: E/AndroidRuntime(332):  at android.app.ActivityThread.access(ActivityThread.java:117)
02-12 14:18:30.147: E/AndroidRuntime(332):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-12 14:18:30.147: E/AndroidRuntime(332):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 14:18:30.147: E/AndroidRuntime(332):  at android.os.Looper.loop(Looper.java:123)
02-12 14:18:30.147: E/AndroidRuntime(332):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-12 14:18:30.147: E/AndroidRuntime(332):  at java.lang.reflect.Method.invokeNative(Native Method)
02-12 14:18:30.147: E/AndroidRuntime(332):  at java.lang.reflect.Method.invoke(Method.java:507)
02-12 14:18:30.147: E/AndroidRuntime(332):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-12 14:18:30.147: E/AndroidRuntime(332):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-12 14:18:30.147: E/AndroidRuntime(332):  at dalvik.system.NativeStart.main(Native Method)
02-12 14:18:30.147: E/AndroidRuntime(332): Caused by: java.lang.ClassCastException: android.widget.ImageButton
02-12 14:18:30.147: E/AndroidRuntime(332):  at com.JodomStudios.cloudgame.SpielActivity.onCreate(SpielActivity.java:19)
02-12 14:18:30.147: E/AndroidRuntime(332):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-12 14:18:30.147: E/AndroidRuntime(332):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-12 14:18:30.147: E/AndroidRuntime(332):  ... 11 more

这是xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearlayoutspiel"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/backgroundleiter"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/zeitzaehler"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/countdown"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/menubutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="@string/menu" />

    <Button
        android:id="@+id/contbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="@string/cont" />

    <ImageButton
        android:id="@+id/pausebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="#00000000"
        android:src="@drawable/closebutton" />

    <TextView
        android:id="@+id/initcountdown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/initcountdown"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="40sp" />

    <ImageButton
        android:id="@+id/retrybutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/pausebutton"
        android:background="#00000000"
        android:src="@drawable/zeichnung" />

    <Button
        android:id="@+id/anfangsbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/menubutton"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="18dp"
        android:text="@string/startgame" />

</RelativeLayout>

4 个答案:

答案 0 :(得分:1)

您正在使用开始按钮作为按钮,并在XML中定义了A ImageButton 。请将它们设为相同。

答案 1 :(得分:1)

来自日志:

02-12 14:18:30.147: E/AndroidRuntime(332): Caused by: java.lang.ClassCastException: android.widget.ImageButton

您正在使用

检索按钮
final Button startbutton = (Button)findViewById(R.id.anfangsbutton);

不是Button,因此也是ClassCastException。请验证您的布局xml并使用正确的演员表。

希望这会有所帮助。

答案 2 :(得分:0)

您的开始按钮在您的布局中定义为ImageButton。您需要像这样更改代码:

final ImageButton startbutton = (ImageButton)findViewById(R.id.anfangsbutton);

答案 3 :(得分:0)

我认为您的应用也可能崩溃,因为您不是从UI线程更新UI。你使用计时器,它在单独的线程中运行。 尝试将initCountdown.setText包装到post:

initCountdown.post(new Runnable() {
public void run() {
initCountdown.setText("" + millisUntilFinished / 1000);
}
});