2017-02-28 10 views
-1

私はこのバージョンの以前のバージョンを使用しました。残念ながら、私は私のUSBに作業バージョンをコピーするのを忘れていたので、私は家でこれを複製しようとしました。私は5方向のラジオボタンを持っています。チェックされるたびに、関連する方向に対応するようにTextViewのテキストを変更することになっています。残念ながら、これは動作していないようです。アプリを実行しようとすると、クラッシュします。ラジオボタンのOnClickイベントは機能していません

デバッグ画面では、NULLポインタ例外についても何かがスローされます。また、ラジオボタンのxmlコードには、Mainで見つからないメソッドについての情報があります。どんな助けもありがとうございます。

<RadioButton 
    android:text="@string/north" 
    android:layout_width="79dp" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="12dp" 
    android:layout_marginStart="12dp" 
    android:layout_marginTop="29dp" 
    android:id="@+id/north" 
    android:onClick="onClick" /> 

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

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

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

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

</RadioGroup> 

<SeekBar 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="22dp" 
    android:id="@+id/speed" 
    android:layout_below="@+id/Speed" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

活動:

public class MainActivity extends AppCompatActivity { 
    RadioButton n, s, e, w, c; 
    // TextView text = (TextView)findViewById(R.id.text); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    } 

    public View.OnClickListener optionOnClickListener; 

    { 
     optionOnClickListener = new OnClickListener() { 

      public View v; 

      public void onClick(View v) { 
       this.v = v; 
       TextView text = (TextView) findViewById(R.id.text); 
       String str = null; 
       n = (RadioButton) findViewById(R.id.north); 
       s = (RadioButton) findViewById(R.id.south); 
       e = (RadioButton) findViewById(R.id.east); 
       w = (RadioButton) findViewById(R.id.west); 
       c = (RadioButton) findViewById(R.id.center); 


       // you can simply copy the string of clicked button. 
       str = ((RadioButton) v).getText().toString(); 
       text.setText(str); 

       // or, you can check each radiobutton and find which one is checked. 
       if (n.isChecked()) { 
        text.setText("North"); 
       } else if (s.isChecked()) { 
        text.setText("South"); 
       } else if (e.isChecked()) { 
        text.setText("East"); 
       } else if (w.isChecked()) { 
        text.setText("West"); 
       } else if (c.isChecked()) { 
        text.setText("Center"); 
       } 

      } 

     }; 
    } 

} 
+0

例外のスタックトレースを投稿できますか? –

+0

私はあなたがここで答えを見つけることができると思います。 http://stackoverflow.com/questions/9748070/radio-group-onclick-event-not-firing-how-do-i-tell-which-is-selected あなたの質問 – Shyamali

+0

ラジオ**グループ**に関する質問は、このアプリがなぜ始まらないのか彼に伝えます? –

答えて

0

あなたが持っているこのブロックは、XMLからのonClickメソッドを設定しているので:

public View.OnClickListener optionOnClickListener; 

{ 
    optionOnClickListener = new OnClickListener() { 

     public View v; 

//delete above here 
     public void onClick(View v) { 
      this.v = v; //delete this 
      TextView text = (TextView) findViewById(R.id.text); 
      String str = null; 
      n = (RadioButton) findViewById(R.id.north); 
      s = (RadioButton) findViewById(R.id.south); 
      e = (RadioButton) findViewById(R.id.east); 
      w = (RadioButton) findViewById(R.id.west); 
      c = (RadioButton) findViewById(R.id.center); 


      // you can simply copy the string of clicked button. 
      str = ((RadioButton) v).getText().toString(); 
      text.setText(str); 

      // or, you can check each radiobutton and find which one is checked. 
      if (n.isChecked()) { 
       text.setText("North"); 
      } else if (s.isChecked()) { 
       text.setText("South"); 
      } else if (e.isChecked()) { 
       text.setText("East"); 
      } else if (w.isChecked()) { 
       text.setText("West"); 
      } else if (c.isChecked()) { 
       text.setText("Center"); 
      } 

     } 

//delete below here 

    }; 
} 

は次のようになります。その後

//remove unneccesary variable and wrapper 

public void onClick(View v) { 
    ... // keep all the lines in this method except this.v = v; 
} 

それはコンパイルする必要がありますが、追加すると、すべてのクリックでそれを行うのではなく、すべてのfindViewById(...)をonCreateで行うことです。

+0

ラッパーを削除すると、エラーが発生します。新しいコードはどのように見えますか? –

+0

私の例とまったく同じ...私はあなたが何を意味するか分かりません。特にあなたが言うように、これは以前に働いていました。 「エラーの読み込み」とは何ですか? –

+0

変数とラッパーはなくなりました。それはまだクラッシュします。私は以前から自分のコードを引き出すことはできません。 –

0

さて、リスナーは必要ないそれを世話するようだ。また、Nick CardosoはonClickが予約されていると言ったときに正しいと思います。長い一日だった。助けてくれてありがとう!おやすみ。

+0

John Caseyあなたの問題は解決されていません。 –

関連する問題