Question

For two days I've been trying to get the setVisibility(View.INVISIBLE) function to work. But if I test my app in emulator, it always crashes. What's wrong? (and also: whats the difference between the view arg0 and v in the onClickListener?)

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();



        }
    });
    }

}

and here is the LogCat data:

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

and here is the xml layout

<?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>
Was it helpful?

Solution 3

I think your app also may crash because you update UI not from UI thread. You use timer and it runs in separate thread. Try to wrap initCountdown.setText into post:

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

OTHER TIPS

You are using start button as a Button and defined A ImageButton in your XML.Please make them same.

From the logs :

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

It appears that the button you are retrieving using

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

is not a Button and hence a ClassCastException. Please verify your layout xml and use the correct cast.

Hope this helps.

Your start button is defined as an ImageButton in your layout. You need to change your code like this:

final ImageButton startbutton = (ImageButton)findViewById(R.id.anfangsbutton);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top