Leth
Leth

Reputation: 1059

Rolling a dice based on number of sides

EDIT: See included error log.

I am trying to create a simple app to roll different sided dice in Android Studio. This is my code so far: MainActivity.java

package com.example.thomb.tutorialspoint;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

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

    Button button = (Button) findViewById(R.id.buttonRoll);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int roll = RollDice(sidesChosen);
            TextView tv1 = (TextView)findViewById(R.id.textView);
            tv1.setText(roll);
            setContentView(tv1);
        }
    });
}

public int sidesChosen;
public int RollDice(int sides) {
    Random r = new Random();
    return r.nextInt(sides)+1;
} //method
public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radioButtonD4:
            if (checked)
                sidesChosen = 4;
                break;
        case R.id.radioButtonD6:
            if (checked)
                sidesChosen = 6;
                break;
        case R.id.radioButtonD8:
            if (checked)
                sidesChosen = 8;
                break;
        case R.id.radioButtonD10:
            if (checked)
                sidesChosen = 10;
                break;
        case R.id.radioButtonD12:
            if (checked)
                sidesChosen = 12;
                break;
        case R.id.radioButtonD20:
            if (checked)
                sidesChosen = 20;
                break;
    } //switch
} //method
} //class

This is how the layout looks like: https://i.sstatic.net/adpP1.png

The app crashes when i click the roll button, but I've no idea why. The ID's are all correct and the radio buttons works as expected. I am using API level 25. I am fairly new to Java, but I am quite familiar with C#, so the problem may lie in the code syntax, although Android Studio reports no errors. Let me know if you need to see the XML for the layout as well.

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.thomb.tutorialspoint, PID: 5029
              java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                  at android.view.ViewGroup.addViewInner(ViewGroup.java:4310)
                  at android.view.ViewGroup.addView(ViewGroup.java:4146)
                  at android.view.ViewGroup.addView(ViewGroup.java:4087)
                  at android.view.ViewGroup.addView(ViewGroup.java:4060)
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:279)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:145)
                  at com.example.thomb.tutorialspoint.MainActivity$1.onClick(MainActivity.java:25)
                  at android.view.View.performClick(View.java:5280)
                  at android.view.View$PerformClick.run(View.java:21239)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:234)
                  at android.app.ActivityThread.main(ActivityThread.java:5526)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Upvotes: 0

Views: 550

Answers (2)

Mehmet K
Mehmet K

Reputation: 2814

setText() is a overloaded method with two types: one that takes in a String, the other an int. The int here should be string resource ID. This is where your error is. In the code below you're using setText(int) but not passing a valid string resource ID.

int roll = RollDice(sidesChosen);
...
tv1.setText(roll);

Do setText(String.valueOf(roll) to convert it to a String first

EDIT after log post:

The cause of your error is that you're passing 0 to the nextInt() method. This might happen because you've never selected a RadioButton (sidesChosen is 0 by default) or that even after selecting a RadioButton, none of the cases of the switch is entered.

EDIT after second log post: (...)

Remove setContentView(tv1); This is used to attach a layout to an activity. Why are you using it here?

Upvotes: 3

Ridcully
Ridcully

Reputation: 23665

It seems, that you pass 0 to the Random.nextInt() method, which is not allowed.

java.lang.IllegalArgumentException: n <= 0: 0
              at java.util.Random.nextInt(Random.java:182)

This happens, if none of your case branches is reached. So it seems, there is something wrong with how you handle the radio button clicks.

Upvotes: 1

Related Questions