Java 收到此错误:很遗憾,在运行我的应用程序时,应用程序已停止

Java 收到此错误:很遗憾,在运行我的应用程序时,应用程序已停止,java,android,eclipse,emulation,r.java-file,Java,Android,Eclipse,Emulation,R.java File,我在运行应用程序时收到以下错误: 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: mai

我在运行应用程序时收到以下错误:

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
我不知道为什么会发生这种错误?有什么见解吗?谢谢

这是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" />
    <?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>

查看您的
main活动
第23行

原因:java.lang.ClassCastException:android.widget.ImageButton无法强制转换为android.widget.Button

这是您的错误,没有代码我们无法判断到底是什么错误。这可能与
ImageButton
不是
Button

请看参考资料:


您可能有一行文字说明:

Button myButton=(Button)findviewbyd(R.id.my\u image\u按钮)

在xml中,id为“my_image_button”(示例中选择的名称)的元素是ImageButton。不幸的是,您不能将和ImageButton用作标准按钮

然后,应将该行替换为:


ImageButton myImageButton=(ImageButton)findViewById(R.id.my_image_按钮)

您只需更换此

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


android.widget.ImageButton无法强制转换为android.widget.Button
错误非常清楚。您好,欢迎使用StackOverflow。我想提醒您,我们不仅仅是一些I/O机器,您可以将堆栈跟踪抛向它们,期待解决方案。请解释你的问题,显示你的代码,至少说声“嗨”。我很抱歉。我是新来的。我会把密码放进去的。非常感谢。这就是问题所在。它现在工作正常。当我从主菜单按一个按钮进入另一个屏幕,然后按后退按钮时,应用程序正在关闭,而不是返回主菜单,为什么会发生这种情况?请帮忙。非常感谢。
Button myButton = (Button)findViewById(R.id.my_image_button);
ImageButton myImageButton = (ImageButton) findViewById(R.id.my_image_Button);