简体   繁体   English

收到此错误:运行我的应用程序时,不幸的是该应用程序已停止

[英]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: 这是main.xml代码:

<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: 这是airport_guide.xml代码:

    <?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 : 这是java代码:

 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 看一下您的MainActivity第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 IT可能与ImageButton不是Button的子代有关

Take a look at the reference: 看一下参考:

http://developer.android.com/reference/android/widget/ImageButton.html 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. 在xml中,标识为“ my_image_button”(为示例选择的名称)的元素是ImageButton。 Unfortunately, you cannot use and ImageButton as a standard Button. 不幸的是,您不能将ImageButton和StandardButton一起使用。

You should then replace the line by : 然后,您应将行替换为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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