Domanda

Need some help debugging my code. I am very new to the Android SDK and am working on learning it. From what I can glean from several posts on SO and other google search results... I formulated this code.

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

As you can see, I have created a few private variables my app is going to use to store data. In order to interact with these values, I will redraw the TextView each time they are modified. *Feel free to correct me here if this is not an ideal strategy.

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

I use a button with the following format.

    Str [X] [+1] 

Where Str [X] is TextView with X a dynamic value. Also where [+1] is a button with an Onclick function preset by the XML file.

    <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" />

Now for simplicity, I have altered the "android:text="" " lines to reflect their actual values instead of linking to @string/... I don't think this makes of much difference but I wanted to acknowledge it.

So my question is why does my Application crash when I click the "+1" button? All my app is trying to do is to redraw the TextView with a higher value (under str) and a lower value (under stats).

Thanks for any and all help!

È stato utile?

Soluzione

You are passing integer value in settext. Either cast integer to string or you can try changing settext like this:

   tv.setText(""+str);
    st.setText(""+stats);

Altri suggerimenti

You tried to create this object:

 TextView st = (TextView)findViewById(R.id.textView18);

where is textView18 in the xml?

u can caste using toString method like this:

tv.setText(str.toString()); st.setText(stats.toString());

Try this change the position of your setContentView(R.layout.activity_main_menu);

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);
    setContentView(R.layout.activity_main_menu);
    getActionBar().setDisplayShowHomeEnabled(false);
    getActionBar().setDisplayShowTitleEnabled(false);

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top