2017-08-10 12 views
-4

アクティビティが1つのみの単純なアプリケーションでは、レイアウトのTextViewを検索してnullと同じことを示す問題が発生しています。親切に、アプリが[送信]ボタンをクリックしてクラッシュする理由を同じようにデバッグします。私はStack Overflowで多くのソリューションを試しましたが、多くのソリューションではビューが指定されたアクティビティの外側に位置する可能性がありますが、ここでは唯一のアクティビティがあります。研究は私を助けませんでしたが、今私は自分が犯した可能性のある欠陥を予測できる同僚の開発者を残しています。textView setText()ヌルポインタ例外[単一アクティビティアプリケーション]

MainActivity.java

package me.thirumurugan.quiz; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.RadioButton; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 

    Button submit; 
    TextView result; 
    RadioButton answer_a; 
    EditText answer_b; 
    CheckBox answer_c1, answer_c2, answer_c3, answer_c4; 
    EditText answer_d; 
    int correct = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     result = (TextView) findViewById(R.id.result); 
     answer_a = (RadioButton) findViewById(R.id.answer_a); 
     answer_b = (EditText) findViewById(R.id.answer_b); 
     answer_c1 = (CheckBox) findViewById(R.id.answer_c1); 
     answer_c2 = (CheckBox) findViewById(R.id.answer_c2); 
     answer_c3 = (CheckBox) findViewById(R.id.answer_c3); 
     answer_c4 = (CheckBox) findViewById(R.id.answer_c4); 
     answer_d = (EditText) findViewById(R.id.answer_d); 
     submit = (Button) findViewById(R.id.submit); 
     submit.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       correct = 0; 
       if (answer_a.isChecked()){ 
        correct++; 
       } 
       if (answer_b.getText().toString().equals("2020")){ 
        correct++; 
       } 
       if (answer_c1.isChecked()&&answer_c2.isChecked()&&answer_c3.isChecked()&&!answer_c4.isChecked()){ 
        correct++; 
       } 
       if (answer_d.getText().toString().toLowerCase().equals("arun jaitley")){ 
        correct++; 
       } 
       result.setText("The Score is " + correct + " on 4!"); 
      } 
     }); 

    } 
} 

activity_main.xmlエラーメッセージがトレースに以下た

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:layout_height="match_parent" 
    tools:context="me.thirumurugan.quiz.MainActivity"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/welcome_messege" 
     android:textAlignment="center" 
     android:id="@+id/result" 
     android:padding="@dimen/padding" 
     android:textStyle="bold" 
     android:textSize="16sp" 
     android:background="@color/colorAccent" 
     android:textColor="#FFF"/> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="@dimen/padding" 
      android:orientation="vertical"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="@dimen/padding" 
       android:text="@string/question_1" 
       android:textColor="@color/colorPrimary" 
       android:textStyle="bold" /> 

      <RadioGroup 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

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

       <RadioButton 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/answer_1_b" /> 

      </RadioGroup> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="@dimen/padding" 
       android:layout_marginTop="@dimen/padding" 
       android:text="@string/question_2" 
       android:textColor="@color/colorPrimary" 
       android:textStyle="bold" /> 

      <EditText 
       android:id="@+id/answer_b" 
       android:hint="@string/answer_hint" 
       android:textColorHint="@color/colorAccent" 
       android:layout_width="match_parent" 
       android:textSize="14sp" 
       android:layout_height="wrap_content" 
       android:backgroundTint="@color/colorPrimary" 
       tools:targetApi="lollipop" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="@dimen/padding" 
       android:layout_marginTop="@dimen/padding" 
       android:text="@string/question_3" 
       android:textColor="@color/colorPrimary" 
       android:textStyle="bold" /> 

      <CheckBox 
       android:id="@+id/answer_c1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/answer_3_a" /> 

      <CheckBox 
       android:id="@+id/answer_c2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/answer_3_b" /> 

      <CheckBox 
       android:id="@+id/answer_c3" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/answer_3_c" /> 

      <CheckBox 
       android:id="@+id/answer_c4" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/answer_3_d" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="@dimen/padding" 
       android:layout_marginTop="@dimen/padding" 
       android:text="@string/question_4" 
       android:textColor="@color/colorPrimary" 
       android:textStyle="bold" /> 

      <EditText 
       android:id="@+id/answer_d" 
       android:hint="@string/answer_hint" 
       android:textSize="14sp" 
       android:textColorHint="@color/colorAccent" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:backgroundTint="@color/colorPrimary" 
       tools:targetApi="lollipop" /> 

      <Button 
       android:id="@+id/submit" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:backgroundTint="@color/colorPrimary" 
       android:text="@string/submit" 
       android:textColor="#FFFFFF" 
       android:textStyle="bold" 
       tools:targetApi="lollipop" /> 

     </LinearLayout> 

    </ScrollView> 

</LinearLayout> 

08-10夜九時58分:06.301 20947-20947/me.thirumurugan.quiz E/AndroidRこの問題を解決するには、次の手順を実行します。: 致命的な例外:メインプロセス:me.thirumurugan.quiz、PID:20947
java.lang.NullPointerException:仮想メソッド 'void android.widget.TextView.setText(java.lang.CharSequence)'を起動しようとしました。 null null オブジェクト参照 me.thirumurugan.quiz.MainActivity $ 1.onClick(MainActivity.java:52)at android.view.View.performClick(View.java:5637)at android.view.View $ PerformClick。 (View.java:22429)at android.os.Handler.handleCallback(Handler.java:751)at android.os.Handler.dispatchMessage(Handler.java:95)at android.os.Looper.loop Looper.java:154)at android.app.ActivityThread.main(ActivityThread.java:6119)at java.util.reflect.Method.invoke ZygoteInit.java:776)

+0

私はちょうどあなたのコードをコピーして自分のアンドロイドのスタジオに貼り付けると、それは何もここに壊れ見えない、うまくきれいにし、あなたのアプリケーションを構築し、また、私はアリに同意するあなたのアプリ – Ali

+1

試して動作しません。きれいにしてビルドする必要があります。 – Ali

+0

をデバッグするためにブレークポイントを使用するには、エラー – anomeric

答えて

0

清潔で再構築が必要です。あなたのコードには何も問題はありません。複数のユーザーが、コードをコピーして自分自身を含めて、自分のアプリケーションで成功にそれを使用している:私はあなたのdimensや文字列ファイルを持っていないので、

enter image description here

テキストが欠落しています。

他にも示唆しているように、範囲の問題はありません。メンバー変数には、作成した匿名の内部クラスからアクセス可能である必要があります。

共有コードの行が見つからない場合があります。その場合、誰もそれを複製することができないため、コミュニティは問題を解決できません。

関連する問題