2013-02-12 2 views
0

2 일 동안 나는 setVisibility(View.INVISIBLE) 기능을 작동 시키려고 노력했습니다. 하지만 에뮬레이터에서 내 앱을 테스트하면 항상 충돌합니다. 뭐가 문제 야? (그리고 또한 λ : onClickListener에서 뷰 arg0v 차이 뭐죠) 여기서안드로이드에서 setVisibility (View.INVISIBLE)은 작동하지 않습니다

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



     } 
    }); 
    } 

} 

및이 로그 캣 데이터이다 : 여기

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> 
+3

'Logcat'에서 예외 데이터를 추가하십시오. – Shaiful

+0

xml 레이아웃을 공유 할 수 있습니까? – fiddler

답변

0

UI 스레드가 아닌 UI를 업데이트하기 때문에 앱이 다운 될 수도 있습니다. 당신은 타이머를 사용하고 별도의 스레드에서 실행됩니다. 포스트에 initCountdown.setText 포장보십시오 : 당신은 버튼으로 시작 버튼을 사용하여 XML.Please에서 하여 ImageButton 그들에게 동일하게 정의

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

고맙습니다. :) –

+0

당신은 환영합니다 :) 이것을 확인하십시오 http://developer.android.com/guide/components/processes-and-threads.html 제가 아는 바로는 일반적인 실수입니다. – Olsavage

0

시작 버튼은 레이아웃에 ImageButton으로 정의됩니다. 다음과 같이 코드를 변경해야합니다.

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

고마워요.하지만 문제가 해결되지 않았습니다 ... –

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

이 버튼 따라서는 ClassCastException 아니다 사용하여 검색하는 것으로 보인다. 레이아웃 xml을 확인하고 올바른 캐스트를 사용하십시오.

희망이 도움이됩니다.