0

我是新来的,完全是菜鸟。我的应用程序在启动时崩溃,但我看不出代码有什么问题。我认为这与图像按钮有关。非常感谢您的帮助:D。

package com.gamix.gtavcheats;


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

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.imageButton2);
    Button btn=(Button)findViewById(R.id.imageButton2);

    btn.setOnClickListener(new View.OnClickListener()
   {
   public void onClick(View v) 
   {
       Intent myIntent = new Intent(MainActivity.this, Xbox360.class);
       MainActivity.this.startActivity(myIntent);       
   }  
});
}

@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;
}


}

日志:

10-26 09:12:26.016: E/AndroidRuntime(1291): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gamix.gtavcheats/com.gamix.gtavcheats.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
10-26 09:12:26.016: E/AndroidRuntime(1291):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-26 09:12:26.016: E/AndroidRuntime(1291):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-26 09:12:26.016: E/AndroidRuntime(1291):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-26 09:12:26.016: E/AndroidRuntime(1291):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-26 09:12:26.016: E/AndroidRuntime(1291):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 09:12:26.016: E/AndroidRuntime(1291):     at android.os.Looper.loop(Looper.java:137)
10-26 09:12:26.016: E/AndroidRuntime(1291):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-26 09:12:26.016: E/AndroidRuntime(1291):     at java.lang.reflect.Method.invokeNative(Native Method)
10-26 09:12:26.016: E/AndroidRuntime(1291):     at java.lang.reflect.Method.invoke(Method.java:525)
10-26 09:12:26.016: E/AndroidRuntime(1291):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-26 09:12:26.016: E/AndroidRuntime(1291):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-26 09:12:26.016: E/AndroidRuntime(1291):     at dalvik.system.NativeStart.main(Native Method)
10-26 09:12:26.016: E/AndroidRuntime(1291): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
10-26 09:12:26.016: E/AndroidRuntime(1291):     at com.gamix.gtavcheats.MainActivity.onCreate(MainActivity.java:17)
10-26 09:12:26.016: E/AndroidRuntime(1291):     at android.app.Activity.performCreate(Activity.java:5133)
10-26 09:12:26.016: E/AndroidRuntime(1291):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-26 09:12:26.016: E/AndroidRuntime(1291):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-26 09:12:26.016: E/AndroidRuntime(1291):     ... 11 more
4

3 回答 3

1

问题是因为您在布局中定义了 ImageButton 但您使用的是按钮。

像这样试试

ImageButton btn=(ImageButton)findViewById(R.id.imageButton2);

删除此行

findViewById(R.id.imageButton2);
于 2013-10-26T13:10:37.917 回答
1

替换此行

Button btn=(Button)findViewById(R.id.imageButton2);

通过这个

ImageButton btn=(ImageButton)findViewById(R.id.imageButton2);

并尝试。

于 2013-10-26T13:20:00.507 回答
0

你能显示日志吗?在日志中,您可以看到受影响的行号。为什么在 setContentView(yourXML) 之后 findViewById(R.id.imageButton2);?去掉它。

于 2013-10-26T13:10:35.847 回答