2013-10-05 89 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