简体   繁体   中英

Receive this error: unfortunately the app has stopped, when running my app

I receive the following error when running my application:

02-09 18:01:19.582: D/AndroidRuntime(1306): Shutting down VM
02-09 18:01:19.582: W/dalvikvm(1306): threadid=1: thread exiting with uncaught exception (group=0xa4d86b20)
02-09 18:01:19.582: E/AndroidRuntime(1306): FATAL EXCEPTION: main
02-09 18:01:19.582: E/AndroidRuntime(1306): Process: com.shashank.sharjahinternationalairport, PID: 1306
02-09 18:01:19.582: E/AndroidRuntime(1306): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shashank.sharjahinternationalairport/com.shashank.sharjahinternationalairport.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
02-09 18:01:19.582: E/AndroidRuntime(1306):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-09 18:01:19.582: E/AndroidRuntime(1306):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-09 18:01:19.582: E/AndroidRuntime(1306):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-09 18:01:19.582: E/AndroidRuntime(1306):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-09 18:01:19.582: E/AndroidRuntime(1306):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-09 18:01:19.582: E/AndroidRuntime(1306):     at android.os.Looper.loop(Looper.java:136)
02-09 18:01:19.582: E/AndroidRuntime(1306):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-09 18:01:19.582: E/AndroidRuntime(1306):     at java.lang.reflect.Method.invokeNative(Native Method)
02-09 18:01:19.582: E/AndroidRuntime(1306):     at java.lang.reflect.Method.invoke(Method.java:515)
02-09 18:01:19.582: E/AndroidRuntime(1306):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-09 18:01:19.582: E/AndroidRuntime(1306):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-09 18:01:19.582: E/AndroidRuntime(1306):     at dalvik.system.NativeStart.main(Native Method)
02-09 18:01:19.582: E/AndroidRuntime(1306): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
02-09 18:01:19.582: E/AndroidRuntime(1306):     at com.shashank.sharjahinternationalairport.MainActivity.onCreate(MainActivity.java:23)
02-09 18:01:19.582: E/AndroidRuntime(1306):     at android.app.Activity.performCreate(Activity.java:5231)
02-09 18:01:19.582: E/AndroidRuntime(1306):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-09 18:01:19.582: E/AndroidRuntime(1306):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-09 18:01:19.582: E/AndroidRuntime(1306):     ... 11 more

I'm not sure why this error is happening? Any insights? Thanks.

this is the main.xml code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

                <ImageButton
                    android:id="@+id/visitorInfo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignTop="@+id/saaDca"
                    android:layout_marginTop="48dp"
                    android:src="@drawable/ic_launcher" />

                <ImageButton
                    android:id="@+id/flightInfo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/visitorInfo"
                    android:layout_alignTop="@+id/cargo"
                    android:src="@drawable/ic_launcher" />

                <ImageButton
                    android:id="@+id/cargo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginRight="24dp"
                    android:layout_marginTop="46dp"
                    android:src="@drawable/ic_launcher" />

                <ImageButton
                    android:id="@+id/saaDca"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/flightInfo"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="37dp"
                    android:layout_toRightOf="@+id/visitorInfo"
                    android:src="@drawable/ic_launcher" />

                <ImageButton
                    android:id="@+id/airportGuide"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/cargo"
                    android:layout_below="@+id/saaDca"
                    android:src="@drawable/ic_launcher" />

this is the airport_guide.xml code:

    <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" >

     <ImageButton
     android:id="@+id/aboutAirport"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:src="@drawable/ic_launcher" />
     <ImageButton
     android:id="@+id/transferAndTransit"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:src="@drawable/ic_launcher" />
    <ImageButton
     android:id="@+id/arrivals"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:src="@drawable/ic_launcher" />
    <ImageButton
     android:id="@+id/fs"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:src="@drawable/ic_launcher" />
    <ImageButton
     android:id="@+id/departure"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:src="@drawable/ic_launcher" />
    <ImageButton
     android:id="@+id/virtualTours"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:src="@drawable/ic_launcher" />
    </LinearLayout>

this is the java code :

 package com.shashank.sharjahinternationalairport;

   import android.app.Activity;
   import android.os.Bundle;
   import android.view.Menu;
   import android.view.View;
   import android.view.View.OnClickListener;
   import android.widget.Button;

    public class MainActivity extends Activity {
Button flightInfoButton;
Button airportGuideButton;
Button visitorInfoButton;
Button saaDcaButton;
Button cargoButton;


    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    flightInfoButton = (Button) findViewById(R.id.flightInfo);
    airportGuideButton = (Button) findViewById(R.id.airportGuide);
    visitorInfoButton = (Button) findViewById(R.id.visitorInfo);
    saaDcaButton = (Button) findViewById(R.id.saaDca);        
    cargoButton = (Button) findViewById(R.id.cargo);

    airportGuideButton.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View V){

        setContentView(R.layout.airport_guide);

    }
});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

     }

You just need to replace this

Button myButton = (Button)findViewById(R.id.my_image_button);

With

ImageButton myImageButton = (ImageButton) findViewById(R.id.my_image_Button);

Look at your MainActivity line 23

Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button

That is your error, without code we can not tell what exactly is wrong. IT may have to do with the fact that ImageButton is not a child of Button

Take a look at the reference:

http://developer.android.com/reference/android/widget/ImageButton.html

You probably have a line stating :

Button myButton = (Button)findViewById(R.id.my_image_button);

In the xml the element with id "my_image_button" (name chosen for the example) is an ImageButton. Unfortunately, you cannot use and ImageButton as a standard Button.

You should then replace the line by :

ImageButton myImageButton = (ImageButton) findViewById(R.id.my_image_Button);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM