2016-10-30 9 views
1

このチュートリアルでは簡単なクイズを作成していますが、チェックされたラジオボタンに関連する「次の回答」ボタンでアプリがクラッシュします。ボタンをクリックするとAndroidアプリがクラッシュする

メインコード:

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
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 Display extends Activity { 
    TextView tv; 
    Button btnNext; 
    RadioGroup rg; 
    RadioButton rb1, rb2,rb3; 
    String questions[]={"Capital of Portugal?", "Capital of Spain?", "Capital of France?"}; 
    String answers[]={"Lisbn","Madrid","Paris"}; 
    String options[]={"Zuriq", "London","Lisbon","Manchester","Buenos Aires","Madrid", "NY", "Paris","Moscow"}; 

    int flag=0; 

    public static int marks,correct,wrong; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.dispaly); 

     tv=(TextView) findViewById(R.id.tvque); 
     btnNext=(Button) findViewById(R.id.btnNext); 
     rb1= (RadioButton) findViewById(R.id.radioButton); 
     rb2= (RadioButton) findViewById(R.id.radioButton2); 
     rb3= (RadioButton) findViewById(R.id.radioButton3); 

     tv.setText(questions[flag]); 
     rb1.setText(options[flag*3]); 
     rb2.setText(options[(flag*3)+1]); 
     rb3.setText(options[(flag*3)+2]); 

     Toast.makeText(this, "Negative Marks: " + MainActivity.tbflag, 1000).show(); 

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

      //The error comes from the line bellow, i believe: 

       RadioButton user_answer= (RadioButton)findViewById(rg.getCheckedRadioButtonId()); 
       String answerText=user_answer.getText().toString(); 

       if(answerText.equalsIgnoreCase(answers[flag])){ 
        correct++; 
       }else{ 
        wrong++; 
        flag++; 
        if(flag<questions.length){ 
         tv.setText(questions[flag]); 
         rb1.setText(options[flag*3]); 
         rb2.setText(options[(flag*3)+1]); 
         rb3.setText(options[(flag*3)+2]); 
        } 
        else 
        { 
         if(MainActivity.tbflag) 
         { 
          marks=correct-wrong; 
         } 
         else 
         { 
          marks=correct; 
         } 
         Intent in =new Intent(getApplicationContext(), ResultActivity.class); 
         startActivity(in); 
        } 
       } 
      } 
     }); 
    } 
} 

ログメッセージ:

java.lang.NullPointerExceptionが仮想メソッドを呼び出す試み 'INT android.widget.RadioGroup.getCheckedRadioButtonId()' nullオブジェクトに参照

私はこのエラーを理解していないのですが、なぜメソッドが呼び出されていませんか?助けてもらえますか?

XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.vtorferreira.anew.MainActivity"> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/tvque" 
    tools:text="Questions" /> 
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:id="@+id/group"> 
    <RadioButton 
     android:text="RadioButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="32dp" 
     android:id="@+id/radioButton" /> 

    <RadioButton 
     android:text="RadioButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/radioButton" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:id="@+id/radioButton2" /> 

    <RadioButton 
     android:text="RadioButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/radioButton3" 
     android:layout_below="@+id/radioButton2" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

</RadioGroup> 

<Button 
    android:text="Next Question" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginLeft="60dp" 
    android:layout_marginStart="60dp" 
    android:layout_marginBottom="125dp" 
    android:id="@+id/btnNext" /> 
</RelativeLayout> 
+1

あなたの宣言変数 'rg'、その後:あなたはデフォルトがあなたのActivityRadioButtonを確認し設定することができ、また

<RadioGroup ... android:checkedButton="@+id/some-radio-button-id"> ... </RadioGroup> 

:デフォルトでラジオボタンを選択するには、あなたのXMLに以下を追加それと何もしない。しかし 'rg'を初期化する必要があります。 –

+0

私はこの行をOncreateに追加しました:rg =(RadioGroup)findViewById(R.id.group);それでも同じエラーのある – glassraven

+0

がクラッシュしますか?関連するXMLを投稿できますか? –

答えて

2

それが使用される前に、あなたがrgを膨らませる必要がありますので、あなたが最初NullPointerExceptionを得ている理由です。

第2のNullPointerExceptionは、ラジオボタンが選択されていない状態で「次へ」ボタンを押している可能性があります。これを防ぐには、OnClickListenerの内部にヌルチェックを追加するか、デフォルトでいずれかのラジオボタンを選択します。

rg.check(R.id.radioButton) 
関連する問題