我是安卓开发新手。当我使用下面的代码时,我的应用程序崩溃了,如果我删除它,它会完美运行:
Button fb_button = (Button) findViewById(R.id.facebook_button);
fb_button.setOnClickListener(this);
这是我的代码:
public class PagePrincipale extends Activity
implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_page_principale);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
Typeface DancingScript = Typeface.createFromAsset(getAssets(), "fonts/dancingscriptregular.ttf");
Typeface opensans = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Light.ttf");
/**
* TextViews and stuff
*/
//calling the app activities :
View VR = findViewById(R.id.imageButton1);
VR.setOnClickListener(this);
View Sk = findViewById(R.id.imageButton2);
Sk.setOnClickListener(this);
View Ct = findViewById(R.id.Contact);
Ct.setOnClickListener(this);
Button fb_button = (Button) findViewById(R.id.facebook_button);
fb_button.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.page_principale, menu);
return true;
}
@Override
public void onClick (View arg0){
/**
* Views buttons
*/
if(arg0.getId() == R.id.imageButton1){
//define a new Intent for the second Activity
Intent intent = new Intent(this,Resume.class);
//start the second Activity
this.startActivity(intent);
}
else if (arg0.getId() == R.id.imageButton2){
Intent intent = new Intent(this,Skills.class);
this.startActivity(intent);
}
else if (arg0.getId() == R.id.Contact){
Intent intent = new Intent(this,Contact.class);
this.startActivity(intent);
}
else if (arg0.getId() == R.id.facebook_button){
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com"));
startActivity(browserIntent);
}
}
}
更新:日志猫
11-13 04:54:32.285: E/AndroidRuntime(912): FATAL EXCEPTION: main
11-13 04:54:32.285: E/AndroidRuntime(912): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Hicham.myresume/com.Hicham.myresume.PagePrincipale}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
11-13 04:54:32.285: E/AndroidRuntime(912): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-13 04:54:32.285: E/AndroidRuntime(912): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-13 04:54:32.285: E/AndroidRuntime(912): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-13 04:54:32.285: E/AndroidRuntime(912): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-13 04:54:32.285: E/AndroidRuntime(912): at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 04:54:32.285: E/AndroidRuntime(912): at android.os.Looper.loop(Looper.java:137)
11-13 04:54:32.285: E/AndroidRuntime(912): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-13 04:54:32.285: E/AndroidRuntime(912): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 04:54:32.285: E/AndroidRuntime(912): at java.lang.reflect.Method.invoke(Method.java:525)
11-13 04:54:32.285: E/AndroidRuntime(912): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-13 04:54:32.285: E/AndroidRuntime(912): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-13 04:54:32.285: E/AndroidRuntime(912): at dalvik.system.NativeStart.main(Native Method)
11-13 04:54:32.285: E/AndroidRuntime(912): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
11-13 04:54:32.285: E/AndroidRuntime(912): at com.Hicham.myresume.PagePrincipale.onCreate(PagePrincipale.java:65)
11-13 04:54:32.285: E/AndroidRuntime(912): at android.app.Activity.performCreate(Activity.java:5133)
11-13 04:54:32.285: E/AndroidRuntime(912): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-13 04:54:32.285: E/AndroidRuntime(912): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-13 04:54:32.285: E/AndroidRuntime(912): ... 11 more
11-13 04:54:32.425: W/ActivityManager(286): Force finishing activity com.Hicham.myresume/.PagePrincipale