-2

試み私はこのヌル例外取得してい

のjava.lang.NullPointerException:起動しようとします仮想メソッド 'java.lang.CharSequence android.widget.TextView.getText()' nullオブジェクトの参照

と私はなぜわかりません。

protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setTheme(R.style.LoginRegister); 
     setContentView(R.layout.activity_report); 

final TextView infoid = (TextView) findViewById(R.id.idInfo); 

     RadioGroup rg = (RadioGroup)findViewById(R.id.rg); 
     final String radiovalue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString(); 

... 

     Button sendButton = (Button) findViewById(R.id.button2); 
     sendButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       String text = infoid.getText().toString(); //what is wrong? 
       Log.w("message: ", radiovalue); 
      } 
     }); 


} 

私のxml

<TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/idInfo" 
     android:hint="Add some extra information" 
     android:layout_weight="0.09" 
     android:layout_marginTop="20dp" 
     android:layout_marginBottom="20dp" /> 

    <Button 
     android:text="Report" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/button2" /> 

任意のアイデア?

+0

あなたの迅速な答えを与えるだろうデバッガを使用するのに役立ちます

RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rg); // get selected radioButton from radioGroup int selectedId = radioGroup.getCheckedRadioButtonId(); // find the radioButton by returned id radioButton = (RadioButton) findViewById(selectedId); // radioButton text String radiovalue = radioButton.getText(); 

希望 –

+2

なぜあなたは法を開始した後でテキストを取得しようとしていますか?とにかく、ivity? –

+0

@ cricket_007私はこのアクティビティに取り組んでいます。この目的をレポート関数onResponseに変更します... –

答えて

1
  1. TextViewidInfoがグローバルとしてidInfoを宣言してください、あなたのactivity_report.xml
  2. に存在していることを確認します。

    .............. 
    .................... 
    
    TextView infoid; 
    
    protected void onCreate(Bundle savedInstanceState) { 
    
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_report); 
    
        infoid = (TextView) findViewById(R.id.idInfo); 
    
        ............. 
        ................... 
    
        Button sendButton = (Button) findViewById(R.id.button2); 
        sendButton.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View view) { 
          String text = infoid.getText().toString(); 
          .......... 
          ................. 
         } 
        }); 
    
    } 
    

UPDATE:

が選択取得するためにradioButtonテキスト:これは〜

+0

問題がラジオボタンにあることがわかりました。 'RadioGroup rg =(RadioGroup)findViewById(R.id.rg); final String radiovalue =((RadioButton)findViewById(rg.getCheckedRadioButtonId()))。getText()。toString(); 'このコードを削除しています。しかし今は選択したアイテムのユーザーを読む別の方法が必要です...ありがとうございます。 –

+1

ラジオボタンのテキストを選択するには、更新された回答を試してみてください。それはあなたのために動作することを願って... – FAT

+0

喜んで:) – FAT

関連する問題