0

im trying to find error in my code. and i don't know why my apps keep crash. Im trying to add value of radio button click by user. But when im click submit button, my apps unfortunately stopped. and suppose the radio button can be only clicked once in the radio group but mine, all of the button can be clicked. Can u guys help me?

p/s im still new in this things so please don't laugh at my coding. I really don't know where to start.it has been two weeks since, so I would greatly appreciate it if you kindly give me some feedback on this.

Here is my xml

<TextView android:id="@+id/tv2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:fontFamily="serif" android:paddingTop="10dp" android:text=" 1. INTRODUCTION (Problem statement, research 
                objectives, research questions, hypothesis, limitation of the
                  study, significance of the study)" android:textAppearance="@style/TextAppearance.AppCompat" />

<RadioGroup android:id="@+id/rg1" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="10dp">

  <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal">

    <RadioButton android:id="@+id/rb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:checked="false" android:text="@string/_1" />

    <RadioButton android:id="@+id/rb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:checked="false" android:text="@string/_2" />

    <RadioButton android:id="@+id/rb3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:checked="false" android:text="@string/_3" />

    <RadioButton android:id="@+id/rb4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:checked="false" android:text="@string/_4" />

    <RadioButton android:id="@+id/rb5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:checked="false" android:text="@string/_5" />
  </LinearLayout>
</RadioGroup>

</LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" android:orientation="vertical">

  <TextView android:id="@+id/tv3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:fontFamily="serif" android:paddingTop="10dp" android:text=" 2. LITERATURE REVIEW (Relevant literature)" android:textAppearance="@style/TextAppearance.AppCompat"
  />

  <RadioGroup android:id="@+id/rg2" android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:paddingTop="10dp">

      <RadioButton android:id="@+id/rb6" style="@style/Widget.AppCompat.CompoundButton.RadioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" />

      <RadioButton android:id="@+id/rb7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" />

      <RadioButton android:id="@+id/rb8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" />

      <RadioButton android:id="@+id/rb9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" />

      <RadioButton android:id="@+id/rb10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" />
    </LinearLayout>
  </RadioGroup>
</LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" android:orientation="vertical">


  <TextView android:id="@+id/tv4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:fontFamily="serif" android:paddingTop="10dp" android:text=" 3. RESEARCH METHODOLOGY (Research design, research context, data collection, data analysis)"
    android:textAppearance="@style/TextAppearance.AppCompat" />

  <RadioGroup android:id="@+id/rg3" android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:paddingTop="10dp">

      <RadioButton android:id="@+id/rb11" style="@android:style/Widget.CompoundButton.RadioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" />

      <RadioButton android:id="@+id/rb12" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" />

      <RadioButton android:id="@+id/rb13" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" />

      <RadioButton android:id="@+id/rb14" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" />

      <RadioButton android:id="@+id/rb15" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" />
    </LinearLayout>
  </RadioGroup>
</LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

  <TextView android:id="@+id/tv5" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:fontFamily="serif" android:paddingTop="10dp" android:text=" 4. RESULT AND FEASIBILITY OF STUDY (Justification of the result, implication of the study, recommendation for future research"
    android:textAppearance="@style/TextAppearance.AppCompat" />

  <RadioGroup android:id="@+id/rg4" android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:paddingTop="10dp">

      <RadioButton android:id="@+id/rb16" style="@android:style/Widget.DeviceDefault.CompoundButton.RadioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" />

      <RadioButton android:id="@+id/rb17" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" />

      <RadioButton android:id="@+id/rb18" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" />

      <RadioButton android:id="@+id/rb19" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" />

      <RadioButton android:id="@+id/rb20" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" />
    </LinearLayout>
  </RadioGroup>
</LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

  <TextView android:id="@+id/tv6" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:fontFamily="serif" android:paddingTop="10dp" android:text=" 5. POSTER PRESENTATION (Type size and type face are easily read, aesthetically pleasing layout, appropriate pictures, and colour is strategically used)"
    android:textAppearance="@style/TextAppearance.AppCompat" />

  <RadioGroup android:id="@+id/rg5" android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:paddingTop="10dp">

      <RadioButton android:id="@+id/rb21" style="@android:style/Widget.DeviceDefault.Light.CompoundButton.RadioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" />

      <RadioButton android:id="@+id/rb22" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" />

      <RadioButton android:id="@+id/rb23" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" />

      <RadioButton android:id="@+id/rb24" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" />

      <RadioButton android:id="@+id/rb25" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" />
    </LinearLayout>
  </RadioGroup>
</LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

  <TextView android:id="@+id/tv7" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:fontFamily="serif" android:paddingTop="10dp" android:text=" 6. PRESENTATION SKILLS (Smooth transition, eye contact, voice and pacing, enthusiasm, body language)"
    android:textAppearance="@style/TextAppearance.AppCompat" />

  <RadioGroup android:id="@+id/rg6" android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="horizontal" android:paddingTop="10dp">

      <RadioButton android:id="@+id/rb26" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" />

      <RadioButton android:id="@+id/rb27" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" />

      <RadioButton android:id="@+id/rb28" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" />

      <RadioButton android:id="@+id/rb29" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" />

      <RadioButton android:id="@+id/rb30" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" />
    </LinearLayout>

  </RadioGroup>
</LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

  <TextView android:id="@+id/tv8" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:fontFamily="serif" android:paddingTop="10dp" android:text=" 7. CONCLUSION (End argument, defend the research, able to answer questions based on research)"
    android:textAppearance="@style/TextAppearance.AppCompat" />

  <RadioGroup android:id="@+id/rg7" android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="horizontal" android:paddingTop="10dp">

      <RadioButton android:id="@+id/rb31" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" />

      <RadioButton android:id="@+id/rb32" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="2" />

      <RadioButton android:id="@+id/rb33" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" />

      <RadioButton android:id="@+id/rb34" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" />

      <RadioButton android:id="@+id/rb35" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" />
    </LinearLayout>

  </RadioGroup>
</LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" android:orientation="vertical" android:weightSum="1">

  <TextView android:id="@+id/tv_comment" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:fontFamily="serif" android:paddingBottom="10dp" android:paddingTop="10dp" android:text="Comment" android:textAppearance="@style/TextAppearance.AppCompat"
    android:textSize="18sp" android:textStyle="bold" />

  <EditText android:id="@+id/et_comment" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="10dp" android:ems="10" android:fontFamily="serif" android:gravity="top" android:inputType="textMultiLine" android:lines="5"
    android:paddingTop="20dp" android:text="Please Enter Your Comment Here" android:layout_weight="0.85" />
</LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal">

  <Button android:id="@+id/btn_submit" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Submit" />
</LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal">

  <TextView android:id="@+id/tv_outcome" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>

and here is my coding

package com.example.posterevaluation;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.Button;

import android.widget.TextView;


public class FormActivity extends AppCompatActivity {

//RadioGroup rg1,rg2,rg3,rg4,rg5,rg6,rg7;
// RadioButton rb1, rb2,rb3,rb4,rb5,rb6,rb7;
// EditText et1;
Button btn_submit;
TextView tv_outcome;

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

    final RadioGroup rg1 = (RadioGroup) findViewById(R.id.rg1);
    final RadioGroup rg2 = (RadioGroup) findViewById(R.id.rg2);
    final RadioGroup rg3 = (RadioGroup) findViewById(R.id.rg3);
    final RadioGroup rg4 = (RadioGroup) findViewById(R.id.rg4);
    final RadioGroup rg5 = (RadioGroup) findViewById(R.id.rg5);
    final RadioGroup rg6 = (RadioGroup) findViewById(R.id.rg6);
    final RadioGroup rg7 = (RadioGroup) findViewById(R.id.rg7);
    //et1 = (EditText) findViewById (R.id.et1);

    tv_outcome = (TextView) findViewById(R.id.tv_outcome);

    btn_submit = (Button) findViewById(R.id.btn_submit);


    btn_submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String value1 = ((RadioButton) findViewById(rg1.getCheckedRadioButtonId())).getText().toString();
            String value2 = ((RadioButton) findViewById(rg2.getCheckedRadioButtonId())).getText().toString();
            String value3 = ((RadioButton) findViewById(rg3.getCheckedRadioButtonId())).getText().toString();
            String value4 = ((RadioButton) findViewById(rg4.getCheckedRadioButtonId())).getText().toString();
            String value5 = ((RadioButton) findViewById(rg5.getCheckedRadioButtonId())).getText().toString();
            String value6 = ((RadioButton) findViewById(rg6.getCheckedRadioButtonId())).getText().toString();
            String value7 = ((RadioButton) findViewById(rg7.getCheckedRadioButtonId())).getText().toString();


            float marks = Float.parseFloat(value1 + value2 + value3 + value4 + value5 + value6 + value7);

            //calculate total

            float total = makeCalculations(marks);

            tv_outcome.setText(String.valueOf(total));

        }
    });   

}

private float makeCalculations(float marks) {

    return ((marks / 35) * 100);

}
}
Niraj Niroula
  • 2,376
  • 1
  • 17
  • 36
lyle
  • 15
  • 6

2 Answers2

2

You have your xml and java file posted here, but it would have been more helpful if we had your logs too. So, please don't forget your logs next time.

As everyone's request, first of all remove the LinearLayout from the RadioGroup, if you want a radio button to be clicked at a time and set the orientation of RadioGroup to horizontal if you want those radio buttons in a line as this;

<RadioGroup
  android:id="@+id/rg1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">

  <RadioButton
    android:id="@+id/rb1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/_1" />

  <RadioButton
    android:id="@+id/rb2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/_2" />

  <RadioButton
    android:id="@+id/rb3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/_3" />

  <RadioButton
    android:id="@+id/rb4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/_4" />

  <RadioButton
    android:id="@+id/rb5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/_5" />
</RadioGroup>

And, I assume, since we don't have logs here, this is where your code seems odd to me. Try

 float marks = Float.parseFloat(value1) + Float.parseFloat(value2) + Float.parseFloat(value3) + Float.parseFloat(value4) + Float.parseFloat(value5) + Float.parseFloat(value6) + Float.parseFloat(value7);

instead of

float marks = Float.parseFloat(value1 + value2 + value3 + value4 + value5 + value6 + value7);
Niraj Niroula
  • 2,376
  • 1
  • 17
  • 36
1

You don't need to use a LinearLayout inside the RadioGroup. Set orientation for the RadioGroup and place the RadioButton directly inside the RadioGroup.

<RadioGroup
    android:id="@+id/rg1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="10dp">

    <RadioButton
        android:id="@+id/rb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:checked="false"

        android:text="@string/_1" />

    <RadioButton
        android:id="@+id/rb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:checked="false"

        android:text="@string/_2" />

    <RadioButton
        android:id="@+id/rb3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:checked="false"

        android:text="@string/_3" />

    <RadioButton
        android:id="@+id/rb4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:checked="false"

        android:text="@string/_4" />

    <RadioButton
        android:id="@+id/rb5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:checked="false"

        android:text="@string/_5" />
</RadioGroup>
Deena
  • 41
  • 3
  • i want it to be in horizontal line. and also i used scrollview. if i remove linear layout it will become error – lyle Apr 05 '17 at 07:01
  • You can set the `RadioGroup` orientation to horizontal. Remove the `LinearLayout` inside `RadioGroup`. – Deena Apr 05 '17 at 07:05
  • thanks. it helps but there is something wrong with the calculation. it suppose to be 100% when i hit the all the radio button "5". but it became 1.5873014E7. can u please see my calculation part? – lyle Apr 05 '17 at 07:36