2016-05-30 2 views
-1

Я работаю над Quiz App, где у меня есть Radio Group и 4 переключателя для опций. Когда я выбираю вариант и нажимая на кнопку «Далее» для следующего вопроса, он работает нормально. Но, когда в группе радио не выбран вариант, он показывает мне java.lang.nullpointerexception в android. Я знаю, что получаю getCheckedRadioButtonId() = -1. То, что я хочу сделать, - это когда пользователь нажимает дальше, не выбирая какой-либо опции, чтобы он отображал текст. Выберите «Ответ», и он должен оставаться на одном вопросе, пока он не ответит на него. Нужна помощь по этому вопросуAndroid Radio Group java.lang.nullpointerexception

import android.content.Intent; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 
import android.widget.Toast; 


public class QuizActivity extends AppCompatActivity{ 

TextView tv, stv, tv1, sans; 
Button btnNext, btnResult; 
RadioGroup rg; 
RadioButton rb1, rb2, rb3, rb4; 

String Qcount [] = {"Q1","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10","Q11","Q12","Q13","Q14","Q15","Q16","Q17","Q18","Q19","Q20"}; 

String questions [] = {"You can use the formula palette to ?", "When a range is selected, how can you activate the previous cell?", "Which tool you will use to join some cells and place the content at the middle of joined cell?","Tab scroll buttons are place on Excel screen ?", 
         "The Name box on to the left of formula bar","Each excel file is a workbook that contains different sheets. Which of the following cannot be a sheet in workbook ?","Which of the following is not the correct method of editing the cell content ?","You can merge the main document with data source in Excel. In mail merge operation, Word is usually ?", 
         "How can you update the values of formula cells if Auto Calculate mode of Excel is disabled ?","You want to set such that when you type Baishakh and drag the fill handle, Excel should produce Jestha, Aashadh and so on. What will you set to affect that ?","Where can you change automatic or manual calculation mode in Excel ?","How can you show or hide the gridlines in Excel Worksheet ?", 
         "Which of the following Excel screen components can NOT be turned on or off ?","What happens when you press Ctrl + X after selecting some cells in Excel ?","Which of the following option is not available in Paste Special dialog box ?","Which command will you choose to convert a column of data into row ?","It is acceptable to let long text flow into adjacent cells on a worksheet when", 
         "Which of the cell pointer indicates you that you can make selection ?","Which of the cell pointer indicates that you can fill series ?","Which of the cell pointer indicate that you can move the content to other cell ?"}; 

String ans [] = {"create and edit formula containing functions", "None of above", "Click on Merge and Centre tool on formatting toolbar","towards the bottom left corner","shows the name of cell or range currently working on","data sheet","Press the Alt key","client","F9","Custom List","Go to Tools >> Options >> Calculation and mark the corresponding radio button","Both A & B","None of above","The cells selected are marked for cutting","Divide", 
       "Edit >> Paste Special >> Transpose","no data will be entered in the adjacent cells","Doctor’s symbol (Big Plus)","small thin plus icon","Mouse Pointer with anchor at the tip"}; 

String opt [] = {"format cells containing numbers","create and edit formula containing functions","enter assumptions data","copy a range of cells","Press the Alt key","Press Tab","Press Enter","None of above", 
        "From Format Cells dialog box click on Merge Cells check box","From Format Cells dialog box select the Centred alignment","From Format Cells dialog box choose Merge and Centre check box","Click on Merge and Centre tool on formatting toolbar","towards the bottom right corner","towards the bottom left corner","towards the top right corner","towards the top left corner","shows the name of workbook currently working on","shows the name of worksheet currently working on", 
       "shows the name of cell or range currently working on","None of above","work sheet","chart sheet","module sheet","data sheet","Press the Alt key","Press the F2 key","Click the formula bar","Double click the cell","server","source","client","none","F8","F9","F10","F11","Custom List","Auto Fill Options","Fill Across Worksheet","Fill Series","Double CAL indicator on status bar","Go to Tools >> Options >> Calculation and mark the corresponding radio button","Both A & B","None of above","Go to Tools >> Options >> View tab and mark or remove the check box named Gridline","Click Gridline tool on Forms toolbar","Both A & B","None of above", 
       "Formula Bar","Status Bar","Tool Bar","None of above","The cell content of selected cells disappear from cell and stored in clipboard","The cells selected are marked for cutting","The selected cells are deleted and the cells are shifted left","The selected cells are deleted and cells are shifted up","Add","Subtract","Divide","SQRT","Cut and Paste","Edit >> Paste Special >> Transpose","Both A & B","None of above","data will be entered in the adjacent cells","no data will be entered in the adjacent cells","there is no suitable abbreviation for the text","there is no time to format the text", 
       "Doctor’s symbol (Big Plus)","small thin plus icon","Mouse Pointer with anchor at the tip","None of above","Doctor’s symbol (Big Plus)","small thin plus icon","Mouse Pointer with anchor at the tip","None of above","Doctor’s symbol (Big Plus)","small thin plus icon","Mouse Pointer with anchor at the tip","None of above"}; 

int flag = 0; 
public static int marks, correct, wrong, correctperct, wrongperct; 

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

    tv = (TextView) findViewById(R.id.textView2); 
    tv1 = (TextView) findViewById(R.id.textView13); 
    btnNext = (Button) findViewById(R.id.button2); 
    btnResult = (Button) findViewById(R.id.button4); 
    rg = (RadioGroup) findViewById(R.id.radioGroup2); 
    rb1 = (RadioButton) findViewById(R.id.radioButton3); 
    rb2 = (RadioButton) findViewById(R.id.radioButton4); 
    rb3 = (RadioButton) findViewById(R.id.radioButton5); 
    rb4 = (RadioButton) findViewById(R.id.radioButton6); 
    sans = (TextView) findViewById(R.id.textView14); 

    btnResult.setEnabled(false); 

    tv.setText(questions[flag]); 
    tv1.setText(Qcount[flag]); 
    rb1.setText(opt[0]); 
    rb2.setText(opt[1]); 
    rb3.setText(opt[2]); 
    rb4.setText(opt[3]); 

    stv = (TextView) findViewById(R.id.textView6); 

    if (OptionActivity.tbflag) 
    { 
     stv.setText("ON"); 
    } 
    else 
    { 
     stv.setText("OFF"); 
    } 

    btnNext.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId()); 
      String ansText = uans.getText().toString(); 
      rg.clearCheck(); 
      sans.setText(""); 

      if (rg.getCheckedRadioButtonId() == -1) 
      { 
       sans.setText("Select Answer"); 
      } 
      if (ansText.equalsIgnoreCase(ans[flag])) 
      { 
       correct++; 
      } 
      else 
      { 
       wrong++; 
      } 
      flag++; 
      if (flag == 20) 
      { 
       rb1.setEnabled(false); 
       rb2.setEnabled(false); 
       rb3.setEnabled(false); 
       rb4.setEnabled(false); 
       btnNext.setEnabled(false); 
       btnResult.setEnabled(true); 
      } 
      if (flag < questions.length) 
      { 
       tv.setText(questions[flag]); 
       tv1.setText(Qcount[flag]); 
       rb1.setText(opt[flag*4]); 
       rb2.setText(opt[(flag*4)+1]); 
       rb3.setText(opt[(flag*4)+2]); 
       rb4.setText(opt[(flag*4)+3]); 
      } 
      else 
      { 
       if (OptionActivity.tbflag) 
       { 
        marks = correct - wrong; 
        correctperct = (int) (((double)correct/(double)20)*100); 
        wrongperct = (int) (((double)wrong/(double)20)*100); 
       } 
       else 
       { 
        marks = correct; 
        correctperct = (int) (((double)correct/(double)20)*100); 
        wrongperct = (int) (((double)wrong/(double)20)*100); 
       } 

      } 
     } 
    }); 

    btnResult.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent i = new Intent(getApplicationContext(), ResultActivity.class); 
      startActivity(i); 

     } 
    }); 
} 


} 

ответ

0

просто нужно держать onClick событие следующей кнопки при следующих условиях;

if (rg.getCheckedRadioButtonId() != -1) {} 
Смежные вопросы