我的问题是,在我在模拟器上启动我的应用程序后,它写了#34;不幸的是,appname已停止"。 Eclipse并没有表明存在任何错误。我知道我可以用LogCat以某种方式修复它们,但不幸的是我对eclipse完全不熟悉,我不确定如何做到这一点。所以请告诉我,我的错误是什么,以及如何解决它们。
logcat的
03-15 07:18:55.620: D/AndroidRuntime(795): Shutting down VM
03-15 07:18:55.620: W/dalvikvm(795): threadid=1: thread exiting with uncaught exception (group=0xb3aa4ba8)
03-15 07:18:55.700: E/AndroidRuntime(795): FATAL EXCEPTION: main
03-15 07:18:55.700: E/AndroidRuntime(795): Process: com.gosha.bux, PID: 795
03-15 07:18:55.700: E/AndroidRuntime(795): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gosha.bux/com.gosha.bux.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
03-15 07:18:55.700: E/AndroidRuntime(795): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-15 07:18:55.700: E/AndroidRuntime(795): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-15 07:18:55.700: E/AndroidRuntime(795): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-15 07:18:55.700: E/AndroidRuntime(795): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-15 07:18:55.700: E/AndroidRuntime(795): at android.os.Handler.dispatchMessage(Handler.java:102)
03-15 07:18:55.700: E/AndroidRuntime(795): at android.os.Looper.loop(Looper.java:136)
03-15 07:18:55.700: E/AndroidRuntime(795): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-15 07:18:55.700: E/AndroidRuntime(795): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 07:18:55.700: E/AndroidRuntime(795): at java.lang.reflect.Method.invoke(Method.java:515)
03-15 07:18:55.700: E/AndroidRuntime(795): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-15 07:18:55.700: E/AndroidRuntime(795): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-15 07:18:55.700: E/AndroidRuntime(795): at dalvik.system.NativeStart.main(Native Method)
03-15 07:18:55.700: E/AndroidRuntime(795): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
03-15 07:18:55.700: E/AndroidRuntime(795): at com.gosha.bux.MainActivity.onCreate(MainActivity.java:21)
03-15 07:18:55.700: E/AndroidRuntime(795): at android.app.Activity.performCreate(Activity.java:5231)
03-15 07:18:55.700: E/AndroidRuntime(795): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-15 07:18:55.700: E/AndroidRuntime(795): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-15 07:18:55.700: E/AndroidRuntime(795): ... 11 more
03-15 07:18:55.700: E/AndroidRuntime(795): FATAL EXCEPTION: main
03-15 07:18:55.700: E/AndroidRuntime(795): Process: com.gosha.bux, PID: 795
03-15 07:18:55.700: E/AndroidRuntime(795): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gosha.bux/com.gosha.bux.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
03-15 07:18:55.700: E/AndroidRuntime(795): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-15 07:18:55.700: E/AndroidRuntime(795): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-15 07:18:55.700: E/AndroidRuntime(795): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-15 07:18:55.700: E/AndroidRuntime(795): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-15 07:18:55.700: E/AndroidRuntime(795): at android.os.Handler.dispatchMessage(Handler.java:102)
03-15 07:18:55.700: E/AndroidRuntime(795): at android.os.Looper.loop(Looper.java:136)
03-15 07:18:55.700: E/AndroidRuntime(795): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-15 07:18:55.700: E/AndroidRuntime(795): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 07:18:55.700: E/AndroidRuntime(795): at java.lang.reflect.Method.invoke(Method.java:515)
03-15 07:18:55.700: E/AndroidRuntime(795): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-15 07:18:55.700: E/AndroidRuntime(795): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-15 07:18:55.700: E/AndroidRuntime(795): at dalvik.system.NativeStart.main(Native Method)
03-15 07:18:55.700: E/AndroidRuntime(795): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
03-15 07:18:55.700: E/AndroidRuntime(795): at com.gosha.bux.MainActivity.onCreate(MainActivity.java:21)
03-15 07:18:55.700: E/AndroidRuntime(795): at android.app.Activity.performCreate(Activity.java:5231)
03-15 07:18:55.700: E/AndroidRuntime(795): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-15 07:18:55.700: E/AndroidRuntime(795): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-15 07:18:55.700: E/AndroidRuntime(795): ... 11 more
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gosha.bux"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:debuggable="true"
>
<activity
android:name="com.gosha.bux.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
主要
package com.gosha.bux;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button first;
TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
first = (Button)findViewById(R.id.button1);
t= (TextView)findViewById(R.id.textView1);
counter = 0;
first.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//TODO Auto-generated method stub
counter = counter+1;
t.setText("heeey"+counter);
}
}) ;
}}
答案 0 :(得分:2)
ComponentInfo{com.gosha.bux/com.gosha.bux.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
解决方案1: 用ImageButton替换Button
ImageButton first = (ImageButton)findViewById(R.id.button1);
解决方案2: 在您的布局中,使用Button更改ImageButton。
答案 1 :(得分:1)
如果您想要ImageButton
,请更改 -
ImageButton first;
first = (ImageButton) findViewById(R.id.button1);
如果你想要简单的按钮然后改变 -
ImageButton
进入xml文件中的Button
。
答案 2 :(得分:0)
尝试:
ImageButton first = (ImageButton)findViewById(R.id.button1);
如果这不起作用,请尝试刷新项目。
答案 3 :(得分:0)
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gosha.bux/com.gosha.bux.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
建议您将ImageButton
投射到Button
。您的R.id.button1
类型ImageButton
是否有可能?
您必须使用
ImageButton first = (ImageButton)findViewById(R.id.button1);
答案 4 :(得分:0)
更改此代码
Button first;
setContentView(R.layout.activity_main);
first = (Button)findViewById(R.id.button1);
到
ImageButton first;
setContentView(R.layout.activity_main);
first = (ImageButton)findViewById(R.id.button1);