我是新手,还有一个完整的菜鸟。我的应用程序在启动时崩溃,但我看不出代码有什么问题。我认为它与图像按钮有关。非常感谢帮助: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
答案 0 :(得分:1)
问题是因为您在布局中定义了ImageButton,但是您使用的是按钮。
试试这个
ImageButton btn=(ImageButton)findViewById(R.id.imageButton2);
删除此行
findViewById(R.id.imageButton2);
答案 1 :(得分:1)
替换此行
Button btn=(Button)findViewById(R.id.imageButton2);
通过这个
ImageButton btn=(ImageButton)findViewById(R.id.imageButton2);
然后试试。
答案 2 :(得分:0)
你能显示日志吗?在日志中,您可以看到受影响的行号。为什么在setContentView(yourXML)之后
findViewById(R.id.imageButton2);
?删除它。