gpt4 book ai didi

java - 无法在我的 Android 游戏上加载我的难度级别

转载 作者:行者123 更新时间:2023-12-01 15:30:40 25 4
gpt4 key购买 nike

基本上我必须制作一个具有简单/中等/困难难度的数学游戏。我使用 switch 语句来实现此目的,它调用一个方法来处理每个困难中要做什么。

问题是我无法理解这个带有 switch 语句的方法应该返回什么(如果有的话),以及我应该如何在 onCreate 方法中调用它。我也不知道为什么,但是当我调用 getPuzzle 方法中的 TextView 的 tv.setText 方法时,它给了我一个错误。

public class Game extends Activity {
private static final String TAG = "Brain Training";

public static final String KEY_DIFFICULTY = "com.coursework.braintrain.difficulty";
public static final int DIFFICULTY_EASY = 0;
public static final int DIFFICULTY_MEDIUM = 1;
public static final int DIFFICULTY_HARD = 2;
public static final int DIFFICULTY_GURU = 3;


private int brain;




@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");

int diff = getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_EASY);
setBrain(getPuzzle(diff));
getBrain();


setContentView(R.layout.gamelayout);


final EditText edittext = (EditText) findViewById(R.id.USERentry);


final Button button1 = (Button) findViewById(R.id.keypad_1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// Perform action on clicks
//String buttonText = (String) button.getText();
edittext.setText(edittext.getText() + "1");
//edittext.setText() = edittext.getText() + "1";
}
});
//rest of the bottons...
}


public void Easy12(){
int a = (int) Math.random();
int b = (int) Math.random();
final TextView tv = (TextView) findViewById(R.id.question_label);
String aString = Integer.toString(a);
String bString = Integer.toString(b);
String display = aString + bString;
tv.setText(display);

}

private int getPuzzle(int diff){

//TODO: Continue last game
switch(diff){
case DIFFICULTY_HARD:

break;
case DIFFICULTY_MEDIUM:

break;
case DIFFICULTY_EASY:
Easy12();
break;
default: Easy12();
}
//might require to return an array or something
return diff;


}


public int getBrain() {
return brain;
}


public void setBrain(int brain) {
this.brain = brain;
}


}

如有任何建议,我们将不胜感激。

编辑:这是随附的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/question_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello" />
<EditText
android:id="@+id/USERentry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:textColor="@color/user_txt_color"/>"
<TableLayout
android:id="@+id/keypad"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*">
<TableRow>
<Button android:id="@+id/keypad_1"
android:text="1">
</Button>
<Button android:id="@+id/keypad_2"
android:text="2"
android:onClick="">
</Button>
<Button android:id="@+id/keypad_3"
android:text="3">
</Button>
</TableRow>
<TableRow>
<Button android:id="@+id/keypad_4"
android:text="4">
</Button>
<Button android:id="@+id/keypad_5"
android:text="5">
</Button>
<Button android:id="@+id/keypad_6"
android:text="6">
</Button>
</TableRow>
<TableRow>
<Button android:id="@+id/keypad_7"
android:text="7">
</Button>
<Button android:id="@+id/keypad_8"
android:text="8">
</Button>
<Button android:id="@+id/keypad_9"
android:text="9">
</Button>
</TableRow>
<TableRow>
<Button android:id="@+id/keypad_0"
android:text="0">
</Button>
</TableRow>
<TableRow>
<Button android:id="@+id/keypad_del"
android:text="del">
</Button>
<Button android:id="@+id/keypad_minus"
android:text="-">
</Button>
<Button android:id="@+id/keypad_hash"
android:text="#">
</Button>
</TableRow>

</TableLayout>

</LinearLayout>

最佳答案

您在 Easy12() 方法中遇到空指针异常。正如 Knossos 所建议的,这看起来可能是由于 xml 布局中不存在 question_label 造成的。确保您的 xml 包含如下所示的内容:

<TextView android:id="@+id/question_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello" />

您仍然可以在运行时使用 setText() 方法更改 TextView 的文本,android:text 属性只是一个初始值。

如果您仍然无法使其工作或发现问题,请同时发布您的 xml 布局。如果这是问题所在,您可能需要考虑阅读 http://developer.android.com/guide/topics/ui/declaring-layout.html了解 xml 布局以及如何更多地使用它们。

关于java - 无法在我的 Android 游戏上加载我的难度级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9554517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com