需要一些幫助來調試我的代碼。我對Android SDK非常陌生,並且正在學習它。從我可以從SO和其他谷歌搜索結果上的幾篇帖子中收集...我制定了這個代碼。Android應用程序關閉按鈕單擊(在自定義註冊頁面上)
public class MainMenu extends Activity {
private int str = 8, dex = 8, inte = 8, luk = 8, stats = 20;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
setContentView(R.layout.activity_main_menu);
}
正如你所看到的,我已經創建了一些我的應用程序將用來存儲數據的私有變量。爲了與這些值交互,我將在每次修改時重繪TextView。 *如果這不是一個理想的策略,請隨時在這裏糾正我。
public void strup(View view) {
if(stats > 0) {
TextView tv = (TextView)findViewById(R.id.textView4);
TextView st = (TextView)findViewById(R.id.textView18);
str++;
stats--;
tv.setText(str);
st.setText(stats);
} else {
Toast.makeText(this, "Out of stats!", Toast.LENGTH_SHORT).show();
}
我使用的按鈕採用以下格式。
Str [X] [+1]
其中Str [X]是TextView,其中X是動態值。 此外,[+1]是一個帶有由XML文件預設的Onclick函數的按鈕。
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Str [" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8" />
<TextView
android:id="@+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="]" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="strup"
android:text="+1" />
現在爲了簡單起見,我已經改變了「機器人:文本=」」「行,以反映它們的實際值,而不是鏈接到@string的/ ......我不認爲這使太大的差別,但我想承認它。
所以我的問題是爲什麼我的應用程序崩潰,當我點擊「+1」按鈕?我所有的應用程序試圖做的是重新繪製一個更高的值(在str下)和一個較低的值(在stats下)的TextView。
感謝您的幫助!
這不會有什麼區別...結帳我的答案。 –