onclicklistener崩溃了应用程序

时间:2013-11-12 16:10:36

标签: android onclicklistener

我是android dev的新手。当我使用下面的代码时,我的应用程序崩溃,如果我删除它,它的工作正常:

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

更新:logcat

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

2 个答案:

答案 0 :(得分:0)

如果出现以下情况可能会导致崩溃:

facebook_button不是Button的类型

或facebook_button id在布局中不存在。

答案 1 :(得分:0)

您是否尝试调试自己的应用? 您必须检查fb_button,因为它可能为空。

如果是这种情况,则表示(Button) findViewById(R.id.facebook_button);无效。

为什么? 可能是因为id等于facebook_button的窗口小部件不在您的活动加载的视图中。

检查setContentView(R.layout.YOUR_LAYOUT)并确认YOUR_LAYOUT包含您的按钮