user2088260
user2088260

Reputation:

Android application error : "unfotunatley Application has stopped"

I am Beginner to android and was coding simple program to change Ringing status of devices to silent and vice versa Following is my code and error log it seems error log is giving exact error message but i am really unable to get it ..Please help me with it

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 File

    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>

Upvotes: 0

Views: 106

Answers (2)

Tom
Tom

Reputation: 1273

This line is causing the problem (i think)

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

u r trying to cast an icon to a button.

u should change it to

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

Upvotes: 0

jtt
jtt

Reputation: 13541

The hint is, you're not using the right id in your button reference.

Being that you're a beginner a hint will go a lot farther than giving you the flat out answer.

Upvotes: 2

Related Questions