2013-10-14 175 views
0

我是Android的初學者和編碼簡單的程序來更改設備的振鈴狀態爲沉默,反之亦然Following is my code and error log它似乎錯誤日誌給出了確切的錯誤消息,但我真的無法得到它..Please幫我一下吧Android應用程序錯誤:「unfotunatley應用程序已停止」

錯誤日誌

10-14 00:01:34.964: E/AndroidRuntime(771): FATAL EXCEPTION: main 
10-14 00:01:34.964: E/AndroidRuntime(771): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.silentmodetoggle/com.example.silentmodetoggle.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button 
10-14 00:01:34.964: E/AndroidRuntime(771): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
10-14 00:01:34.964: E/AndroidRuntime(771): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
10-14 00:01:34.964: E/AndroidRuntime(771): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
10-14 00:01:34.964: E/AndroidRuntime(771): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
10-14 00:01:34.964: E/AndroidRuntime(771): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-14 00:01:34.964: E/AndroidRuntime(771): at android.os.Looper.loop(Looper.java:137) 
10-14 00:01:34.964: E/AndroidRuntime(771): at android.app.ActivityThread.main(ActivityThread.java:5041) 
10-14 00:01:34.964: E/AndroidRuntime(771): at java.lang.reflect.Method.invokeNative(Native Method) 
10-14 00:01:34.964: E/AndroidRuntime(771): at java.lang.reflect.Method.invoke(Method.java:511) 
10-14 00:01:34.964: E/AndroidRuntime(771): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
10-14 00:01:34.964: E/AndroidRuntime(771): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
10-14 00:01:34.964: E/AndroidRuntime(771): at dalvik.system.NativeStart.main(Native Method) 
10-14 00:01:34.964: E/AndroidRuntime(771): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button 
10-14 00:01:34.964: E/AndroidRuntime(771): at com.example.silentmodetoggle.MainActivity.setButtonCliclListener(MainActivity.java:34) 
10-14 00:01:34.964: E/AndroidRuntime(771): at com.example.silentmodetoggle.MainActivity.onCreate(MainActivity.java:26) 
10-14 00:01:34.964: E/AndroidRuntime(771): at android.app.Activity.performCreate(Activity.java:5104) 
10-14 00:01:34.964: E/AndroidRuntime(771): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
10-14 00:01:34.964: E/AndroidRuntime(771): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
10-14 00:01:34.964: E/AndroidRuntime(771): ... 11 more 

Java文件

public class MainActivity extends Activity 
    { 
     private AudioManager audio; 
     private boolean issilent ; 
     ImageView img; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      setButtonCliclListener(); 
      audio= (AudioManager) getSystemService(AUDIO_SERVICE); 
      checkifsilent(); 
      setButtonCliclListener(); 

     } 
     private void setButtonCliclListener() 
     { 
      Button toggle= (Button) findViewById(R.id.on_icon); 
      toggle.setOnClickListener(new OnClickListener() 
      { 
       @Override 
       public void onClick(View v) 
       { 
        if(issilent) 
        { 
         audio.setRingerMode(AudioManager.RINGER_MODE_SILENT); 
         issilent=true; 
        } 
        else 
        { 
         audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL); 
         issilent= false; 
        } 
       } 
      }); 
      toggleui(); 

     } 


     public void checkifsilent() 
     { 
      int ringermode= audio.getRingerMode(); 
      if(ringermode== AudioManager.RINGER_MODE_SILENT) 
      { 
       issilent=true; 
      } 
      else 
      {issilent=false; } 
     } 

     private void toggleui() 
     { 
      img= (ImageView) findViewById(R.id.on_icon); 
      Drawable newimg; 
      if(issilent) 
      { 
       newimg=getResources().getDrawable(R.drawable.off); 
      } 
      else 
      { 
       newimg= getResources().getDrawable(R.drawable.on); 
      } 

      img.setImageDrawable(newimg); 
     } 

     @Override 
     public void onResume() 
     { 
      super.onResume(); 
      checkifsilent(); 
      toggleui(); 
     } 

    } 

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 

    <ImageView 
     android:id="@+id/on_icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:src="@drawable/on" /> 

    <Button 
     android:id="@+id/Toggle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:text="Toggle Mode" /> 

</LinearLayout> 

回答

3

的提示是,你不使用你的按鈕引用正確的ID。

因爲你是一個初學者的提示將遠遠比給你一個平坦的答案。

+0

哦謝謝U,知道了這些愚蠢的錯誤:) –

1

此行引起的問題(我認爲)

Button toggle= (Button) findViewById(R.id.on_icon); 

ü[R嘗試投放的圖標按鈕。

ü應該將其更改爲

Button toggle= (Button) findViewById(R.id.Toggle); 
+0

感謝ü它的工作 –

相關問題