2017-04-05 30 views
0

即時通訊設法在我的代碼中發現錯誤。我不知道爲什麼我的應用程序保持崩潰。我試圖增加用戶單選按鈕的值。但是當我點擊提交按鈕時,我的應用程序不幸停止。並假設單選按鈕只能在收音機組中點擊一次,但是我的所有按鈕都可以點擊。你能幫我嗎?如何將單選按鈕的浮點值一起添加?

p/s im這個東西還是新的,所以請不要嘲笑我的編碼。我真的不知道從哪裏開始,這已經過去兩週了,所以如果您就此向我提供一些反饋,我將不勝感激。

這裏是我的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> 

,這裏是我的編碼

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

} 
} 
+0

>檢查這個【答案】(http://stackoverflow.com/questions/11194515/android-get-value-of-the-selected-radio-button ) –

+0

if(rg.isChecked()),把它放在值中。然後添加它。 –

+0

我需要爲我的每個無線電組編碼嗎? – lyle

回答

1

你有你的XML的Java文件張貼在這裏,但它有更多的幫助,如果我們有您的日誌了。所以,下次請不要忘記你的日誌。

由於每個人的要求,首先從RadioGroup刪除LinearLayout,如果你想被點擊一次一個單選按鈕,如果你想在一個行,因爲這與單選按鈕RadioGroup方向設置爲horizontal;

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

而且,我認爲,因爲我們這裏沒有日誌,所以這是您的代碼對我來說似乎很古怪的地方。嘗試

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

,而不是

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

我的天啊。非常感謝!它的作品..我會問兩個星期前,如果我知道這會發生... tqvm! – lyle

+0

那麼那就是問題了。歡迎和愉快的編碼。 – Journey

1

你並不需要使用LinearLayoutRadioGroup內。設置RadioGroup的方向並將RadioButton直接放置在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> 
+0

我希望它在水平線上。我也用scrollview。如果我刪除線性佈局,它將變成錯誤 – lyle

+0

您可以將'RadioGroup'方向設置爲水平。在RadioGroup裏刪除'LinearLayout'。 – Deena

+0

謝謝。它有幫助,但計算有問題。當我點擊所有單選按鈕「5」時,它假設爲100%。但它變成了1.5873014E7。你能看到我的計算部分嗎? – lyle

1

RadioButton應該是RadioGroup的直接子節點,否則分組不起作用。

例如:

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

    <RadioButton android:id="@+id/rb16" 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" /> 
</RadioGroup> 
相關問題