Android应用程序错误:“unfotunatley应用程序已停止”

时间:2013-10-14 00:03:55

标签: java android xml onclicklistener

我是android的初学者,正在编写简单的程序,将设备的响铃状态更改为静音,反之亦然Following is my code and error log似乎错误日志提供了确切的错误消息,但我真的无法得到它..请帮助我用它

错误日志

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>

2 个答案:

答案 0 :(得分:3)

提示是,您没有在按钮参考中使用正确的ID。

因为你是初学者,所以提示会比给你平坦的答案更进一步。

答案 1 :(得分:1)

此行导致问题(我认为)

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

你试图将图标转换为按钮。

你应该把它改成

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