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

时间:2014-02-09 18:27:53

标签: 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: 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" />

这是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>

这是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;
    }

     }

3 个答案:

答案 0 :(得分:1)

您只需要替换此

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

使用

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

答案 1 :(得分:0)

查看您的MainActivity第23行

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

这是你的错误,没有代码,我们无法分清究竟是什么错。 IT可能与ImageButton不是Button

的孩子这一事实有关

看一下参考文献:

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

答案 2 :(得分:0)

你可能有一句话说明:

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

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

然后您应该通过以下方式替换该行:

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