2013-10-05 90 views
0

嗨,我想問問爲什麼不是我的按鈕不工作點擊我剛剛複製我的其他佈局按鈕代碼的作品,並嘗試創建它在我自己的,但仍然無法正常工作?簡單按鈕事件不起作用?

這裏是我的代碼

的.java 包com.thesis.logipic;

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.widget.Button; 


public class Menu extends Activity 
{ 

Button beginner; 

@Override 
protected void onCreate(Bundle MenuButtons) { 
    super.onCreate(MenuButtons); 
} 

public void onClick(View v) 
{ 
    Button clickedButton = (Button) v; 
    switch (clickedButton.getId()) 
    { 

    case R.id.btnBeginner: 
     setContentView(R.layout.gameplay); 
     break; 

    case R.id.btnLearner: 
     setContentView(R.layout.menu); 
     break; 
    } 
} 

} 

的.xml

<ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent"> 

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:background="@drawable/categories" > 

<Button 
    android:id="@+id/btnLearner" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/btnBeginner" 
    android:layout_centerHorizontal="true" 
    android:background="@drawable/learner_menu" /> 

<Button 
    android:id="@+id/btnBeginner" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/btnLearner" 
    android:layout_alignParentTop="true" 
    android:background="@drawable/beginner_menu" /> 

</RelativeLayout> 

</ScrollView> 

</LinearLayout> 

和清單

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 

    <activity 
     android:name=".Splash" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name=".ThesisActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="com.thesis.logipic.THESISACTIVITY" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name=".logipic.Menu" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="com.thesis.logipic.MENU" /> 
      <category android:name="android.intent.category.ALTERNATIVE" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name=".Gameplay" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="com.thesis.logipic.GAMEPLAY" /> 
      <category android:name="android.intent.category.ALTERNATIVE" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name=".Beginner" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="com.thesis.logipic.BEGINNER" /> 
      <category android:name="android.intent.category.ALTERNATIVE" /> 
     </intent-filter> 
    </activity> 

</application> 

</manifest> 
+0

你真的[R試圖做?你從未設置內容視圖。嘗試從互聯網上的一些android基本教程。 –

回答

0

試試這個:

@Override 
protected void onCreate(Bundle MenuButtons) { 
    super(MenuButtons); 
    ((Button) findViewById(R.id.btnBeginner)).setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      setContentView(R.layout.gameplay); 
     } 
    }); 
    ((Button) findViewById(R.id.btnLearner)).setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      setContentView(R.layout.menu); 
     } 
    }); 
} 
0

您必須修改自己的活動:

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class Menu extends Activity implements OnClickListener { 

    private Button beginner, learner; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); // Your layout 

     beginner = (Button) findViewById(R.id.btnBeginner); 
     beginner.setOnClickListener(this); 

     learner = (Button) findViewById(R.id.btnLearner); 
     learner.setOnClickListener(this); 

    } 

    @Override 
    public void onClick(View v) { 

     switch (v.getId()) { 

     case R.id.btnBeginner: 
      setContentView(R.layout.gameplay); 
      break; 

     case R.id.btnLearner: 
      setContentView(R.layout.menu); 
      break; 
     } 
    } 

} 
+0

它有一個錯誤,當我點擊它之前,它不做任何事情哈哈 – John

0

將contentview設置到您的onCreate方法中。

setContentView(R.layout.menu); 

然後將下面的標籤添加到xml Button元素中。

android:onClick="onClick" 

編輯

爲了您的方便。它是您的修改代碼。

@Override 
    protected void onCreate(Bundle MenuButtons) { 
    super.onCreate(MenuButtons); 
    setContentView(R.layout.menu); 
    } 

和XML

<ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent"> 

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:background="@drawable/categories" > 

<Button 
    android:id="@+id/btnLearner" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/btnBeginner" 
    android:layout_centerHorizontal="true" 
    android:background="@drawable/learner_menu" 
    android:onClick="onClick" 
    /> 

<Button 
    android:id="@+id/btnBeginner" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/btnLearner" 
    android:layout_alignParentTop="true" 
    android:background="@drawable/beginner_menu" 
    android:onClick="onClick" 
    /> 


</RelativeLayout> 

</ScrollView> 
+0

謝謝,順便說一句我做了代碼,但它強制關閉,當我點擊按鈕,但比沒有響應好haha – John

+0

請粘貼您的完整的錯誤日誌。 – Adnan

+0

它顯示「android已停止工作,意外強制關閉」,但當我試圖把它放在第三個佈局,將顯示它工作正常 – John