setText() crashing the activity

Related searches

I'm very new to the android/java programming scene and am at the last stage of finalizing my Symptom Checker but have hit a snag yet again. A user is asked questions and can answer them with the provided yes/no radio buttons, there is a pair for each question. The problem I'm facing is that I want the 'textView' found in the "diagonsis" activity to display different information correlating to which question has 'yes' checked. This will allow patient symptom diagnosis etc. The application loads fine but crashes upon starting the "answers" activity which currently has the setText() function.

Here is a link to the GitHub repository, if cloning it helps: https://github.com/Akhlz001/MHT_Application

Do I need to implement setText() in the "questions" activity or the "answers" activity? Also, if implemented in the questions activity how would I go about changing text in another activity?

At the moment I'm using an if-else statement to make the 'textView' display text when a radio button in the previous "Questions" activity is checked yes as shown in the "answers" java file below:

public class answers extends AppCompatActivity {

private Button button7;

private RadioButton radioButton;
private RadioButton radioButton2;
private RadioButton radioButton3;
private RadioButton radioButton4;
private RadioButton radioButton5;
private RadioButton radioButton6;
private RadioButton radioButton7;
private RadioButton radioButton8;
private RadioButton radioButton9;
private RadioButton radioButton10;
private RadioButton radioButton11;
private RadioButton radioButton12;
private RadioButton radioButton13;
private RadioButton radioButton14;
private RadioButton radioButton15;
private RadioButton radioButton16;
private RadioButton radioButton17;
private RadioButton radioButton18;

private TextView textView;
private TextView textView2;
private TextView textView3;


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

    button7 = findViewById(R.id.button7);
    button7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            reset();
        }
    });

    diagnosistext();

    //Question 1
    radioButton = findViewById(R.id.radioButton); //yes
    radioButton2 = findViewById(R.id.radioButton20); //no
    //Question 2
    radioButton3 = findViewById(R.id.radioButton4); //yes
    radioButton4 = findViewById(R.id.radioButton2); //no
    //Question 3
    radioButton5 = findViewById(R.id.radioButton6); //yes
    radioButton6 = findViewById(R.id.radioButton5); //no
    //Question 4
    radioButton7 = findViewById(R.id.radioButton7); //yes
    radioButton8 = findViewById(R.id.radioButton8); //no
    //Question 5
    radioButton9 = findViewById(R.id.radioButton9); //yes
    radioButton10 = findViewById(R.id.radioButton10); //no
    //Question 6
    radioButton11 = findViewById(R.id.radioButton12); //yes
    radioButton12 = findViewById(R.id.radioButton11); //no
    //Question 7
    radioButton13 = findViewById(R.id.radioButton13); //yes
    radioButton14 = findViewById(R.id.radioButton14); //no
    //Question 8
    radioButton15 = findViewById(R.id.radioButton15); //yes
    radioButton16 = findViewById(R.id.radioButton16); //no
    //Question 9
    radioButton17 = findViewById(R.id.radioButton17); //yes
    radioButton18 = findViewById(R.id.radioButton18); //no


    textView = findViewById(R.id.textView16);
    textView2 = findViewById(R.id.textView17);
    textView3 = findViewById(R.id.textView18);
}

public void diagnosistext() {

    if (radioButton.isChecked()) {
        textView.setText("testing");
        textView2.setText("more testing");
        textView3.setText("even more testing");
    }
    else {
        textView.setText("test");
        textView2.setText("ing");
        textView3.setText("df");
    }
}


public void reset() {
    Intent intent = new Intent(this, Questions.class);
    startActivity(intent);
}

}

This is the "Questions" java file:

public class Questions extends AppCompatActivity {

private Button button2;

private RadioButton radioButton;
private RadioButton radioButton2;
private RadioButton radioButton3;
private RadioButton radioButton4;
private RadioButton radioButton5;
private RadioButton radioButton6;
private RadioButton radioButton7;
private RadioButton radioButton8;
private RadioButton radioButton9;
private RadioButton radioButton10;
private RadioButton radioButton11;
private RadioButton radioButton12;
private RadioButton radioButton13;
private RadioButton radioButton14;
private RadioButton radioButton15;
private RadioButton radioButton16;
private RadioButton radioButton17;
private RadioButton radioButton18;


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

    button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            opendiagnosis();
        }
    });

    //Question 1
    radioButton = findViewById(R.id.radioButton); //yes
    radioButton2 = findViewById(R.id.radioButton20); //no
    //Question 2
    radioButton3 = findViewById(R.id.radioButton4); //yes
    radioButton4 = findViewById(R.id.radioButton2); //no
    //Question 3
    radioButton5 = findViewById(R.id.radioButton6); //yes
    radioButton6 = findViewById(R.id.radioButton5); //no
    //Question 4
    radioButton7 = findViewById(R.id.radioButton7); //yes
    radioButton8 = findViewById(R.id.radioButton8); //no
    //Question 5
    radioButton9 = findViewById(R.id.radioButton9); //yes
    radioButton10 = findViewById(R.id.radioButton10); //no
    //Question 6
    radioButton11 = findViewById(R.id.radioButton12); //yes
    radioButton12 = findViewById(R.id.radioButton11); //no
    //Question 7
    radioButton13 = findViewById(R.id.radioButton13); //yes
    radioButton14 = findViewById(R.id.radioButton14); //no
    //Question 8
    radioButton15 = findViewById(R.id.radioButton15); //yes
    radioButton16 = findViewById(R.id.radioButton16); //no
    //Question 9
    radioButton17 = findViewById(R.id.radioButton17); //yes
    radioButton18 = findViewById(R.id.radioButton18); //no
}

public void opendiagnosis() {
    // for all pairs: one of each pair has to be checked
    boolean shouldStartNextActivity = (radioButton.isChecked() || radioButton2.isChecked())
            && (radioButton3.isChecked() || radioButton4.isChecked()) && (radioButton5.isChecked() || radioButton6.isChecked())
            && (radioButton7.isChecked() || radioButton8.isChecked())&& (radioButton9.isChecked() || radioButton10.isChecked())
            && (radioButton11.isChecked() || radioButton12.isChecked())&& (radioButton13.isChecked() || radioButton14.isChecked())
            && (radioButton15.isChecked() || radioButton16.isChecked())&& (radioButton17.isChecked() || radioButton18.isChecked());


    if (shouldStartNextActivity){
        Intent intent = new Intent(this, answers.class);
        startActivity(intent);
    } else{
        Toast.makeText(getBaseContext(), "Please answer all the questions for an accurate diagnosis", Toast.LENGTH_LONG).show();
    }
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);


    savedInstanceState.putBoolean("myOption1", radioButton.isChecked());
    savedInstanceState.putBoolean("myOption2", radioButton2.isChecked());
    savedInstanceState.putBoolean("myOption3", radioButton3.isChecked());
    savedInstanceState.putBoolean("myOption4", radioButton4.isChecked());
    savedInstanceState.putBoolean("myOption5", radioButton5.isChecked());
    savedInstanceState.putBoolean("myOption6", radioButton6.isChecked());
    savedInstanceState.putBoolean("myOption7", radioButton7.isChecked());
    savedInstanceState.putBoolean("myOption8", radioButton8.isChecked());
    savedInstanceState.putBoolean("myOption9", radioButton9.isChecked());
    savedInstanceState.putBoolean("myOption10", radioButton10.isChecked());
    savedInstanceState.putBoolean("myOption11", radioButton11.isChecked());
    savedInstanceState.putBoolean("myOption12", radioButton12.isChecked());
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    radioButton.setChecked(savedInstanceState.getBoolean("myOption1"));
    radioButton2.setChecked(savedInstanceState.getBoolean("myOption2"));
    radioButton3.setChecked(savedInstanceState.getBoolean("myOption3"));
    radioButton4.setChecked(savedInstanceState.getBoolean("myOption4"));
    radioButton5.setChecked(savedInstanceState.getBoolean("myOption5"));
    radioButton6.setChecked(savedInstanceState.getBoolean("myOption6"));
    radioButton7.setChecked(savedInstanceState.getBoolean("myOption7"));
    radioButton8.setChecked(savedInstanceState.getBoolean("myOption8"));
    radioButton9.setChecked(savedInstanceState.getBoolean("myOption9"));
    radioButton10.setChecked(savedInstanceState.getBoolean("myOption10"));
    radioButton11.setChecked(savedInstanceState.getBoolean("myOption11"));
    radioButton12.setChecked(savedInstanceState.getBoolean("myOption12"));

}
}

Logcat errors listed:

2020-01-28 01:24:42.250 12031-12031/com.example.mhtapplication 
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mhtapplication, PID: 12031
java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.mhtapplication/com.example.mhtapplication.answers}: 
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean 
android.widget.RadioButton.isChecked()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.RadioButton.isChecked()' on a null object reference
    at com.example.mhtapplication.answers.diagnosistext(answers.java:91)
    at com.example.mhtapplication.answers.onCreate(answers.java:53)
    at android.app.Activity.performCreate(Activity.java:7802)
    at android.app.Activity.performCreate(Activity.java:7791)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:214) 
    at android.app.ActivityThread.main(ActivityThread.java:7356) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

I'm sorry if this isn't worded correctly, currently quite tired and need this working. Thank you.

Based on your logcat, you are trying to fetch the value of your checkboxes in the diagnosistext function which are not available in the layout. And hence, you are getting a NullPointerException.

Though its difficult to understand without seeing your layout for the answers class, however, I assume the layout for your answer class does not have the radio buttons. Hence the findViewById is returning null and you are trying to get the getCheck information from a null object reference.

I would suggest delivering the input for the text views in your answer activity from the Questions activity using intent extras. Here's how you can pass data between activities.

So the idea is, before launching the answers activity from the Question activity, you need to read all the values of your checkboxes, and make the texts that you want to show in the answers activity. Then pass the texts to the answer activity using intent extra as stated in the answer linked above. Then in the answer activity, populate the text views based on what you got from the previous activity.

I hope that helps.

TextView.setText() in fragment crashes app, setText (Android) is causing crashes.. any idea why , You need to call setContentView() before initializing the TextView so that your Activity has access to all the� AndroiDev: TextView.setText crash One thing I yearn for when I am programming is a debugger like Perl’s interpreter. If you had a bug in your code, it would tell you almost exactly where it was, and even suggest fixes sometimes (Ex: You probably forgot a semicolon at the end of line X).

You need to put diagnosistext() at the very bottom code because you did not initialized the view yet for the radio button.

textView = findViewById(R.id.textView16);
textView2 = findViewById(R.id.textView17);
textView3 = findViewById(R.id.textView18);

//Move this method to here
diagnosistext();

10 Ways to Avoid Mistakes When Programming Your Android App , public class MainActivity extends Activity. implements OnClickListener static void show() {. textView. setText("Hello") , the app crashes. The user sees an� When I run it, it crashes. If I change the setText() method to handle a String instead of an integer then it works perfectly. So I either need to figure out how to change the integer back into a string, or find out what I'm doing wrong in the first place.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.RadioButton.isChecked()' on a null object reference - means you have't yet find this view.

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

    button2 = (Button) findViewById(R.id.button2);

    textView = findViewById(R.id.textView16);
    textView2 = findViewById(R.id.textView17);
    textView3 = findViewById(R.id.textView18);

    //Question 1
    radioButton = findViewById(R.id.radioButton); //yes
    radioButton2 = findViewById(R.id.radioButton20); //no
    //Question 2
    radioButton3 = findViewById(R.id.radioButton4); //yes
    radioButton4 = findViewById(R.id.radioButton2); //no
    //Question 3
    radioButton5 = findViewById(R.id.radioButton6); //yes
    radioButton6 = findViewById(R.id.radioButton5); //no
    //Question 4
    radioButton7 = findViewById(R.id.radioButton7); //yes
    radioButton8 = findViewById(R.id.radioButton8); //no
    //Question 5
    radioButton9 = findViewById(R.id.radioButton9); //yes
    radioButton10 = findViewById(R.id.radioButton10); //no
    //Question 6
    radioButton11 = findViewById(R.id.radioButton12); //yes
    radioButton12 = findViewById(R.id.radioButton11); //no
    //Question 7
    radioButton13 = findViewById(R.id.radioButton13); //yes
    radioButton14 = findViewById(R.id.radioButton14); //no
    //Question 8
    radioButton15 = findViewById(R.id.radioButton15); //yes
    radioButton16 = findViewById(R.id.radioButton16); //no
    //Question 9
    radioButton17 = findViewById(R.id.radioButton17); //yes
    radioButton18 = findViewById(R.id.radioButton18); //no

button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            opendiagnosis();
        }
    });
}

public void opendiagnosis() {
    // for all pairs: one of each pair has to be checked
    boolean shouldStartNextActivity = (radioButton.isChecked() || radioButton2.isChecked())
            && (radioButton3.isChecked() || radioButton4.isChecked()) && (radioButton5.isChecked() || radioButton6.isChecked())
            && (radioButton7.isChecked() || radioButton8.isChecked())&& (radioButton9.isChecked() || radioButton10.isChecked())
            && (radioButton11.isChecked() || radioButton12.isChecked())&& (radioButton13.isChecked() || radioButton14.isChecked())
            && (radioButton15.isChecked() || radioButton16.isChecked())&& (radioButton17.isChecked() || radioButton18.isChecked());


    if (shouldStartNextActivity){
        Intent intent = new Intent(this, answers.class);
        startActivity(intent);
    } else{
        Toast.makeText(getBaseContext(), "Please answer all the questions for an accurate diagnosis", Toast.LENGTH_LONG).show();
    }
}


give a try

[Android] Using setText methods crashes the app, Attempt to invoke , But everywhere I used set text it just crashes the app. To be clear, this is a help "activity" in settings, with only 1 text view, which I'm trying to change the content� [Android] Using setText methods crashes the app, Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference No idea what's going on. But everywhere I used set text it just crashes the app.

AndroiDev: TextView.setText crash, setText crash. One thing RuntimeException: Unable to start activity setText(). This really shouldn't be a shock as that is the topic of this post. can You please tell me why my application crashes when I try to modify textview in onCreate? when I do the same thing in onPostCreate the string is properly modified in textView.

TextView.setText() in fragment crashes app, setText() in fragment crashes app. android multiple fragments in one activity example github textview settext crash android fragment example source code Crashing is another schedule compression technique where you add extra resources to the project to compress the schedule. In crashing, you review the critical path and see which activities can be completed by adding extra resources. You try to find the activities that can be reduced the most by adding the least amount of cost.

By crashing this activity, we see that two paths become critical path, 1-2-3-4-5 and 1-2-4-5 as shown in Fig. 29.9 below: Now we see that after crashing activities 2-3, 4-5 and 3-4, there is no other activity on both the critical paths which have cost slopes less than indirect cost.

Comments
  • What does your logcat say? Can you please post the logcat?
  • I have just posted the logcat on github gist.github.com/Akhlz001/809b594956f18b00e7e914ef5c3f7c96
  • Please post the logcat here with your question. That will help others to answer your question.
  • @ReazMurshed posted, thank you for the advice
  • This does seem feasible actually, although it is quite a bit to get around. Thank you
  • You cannot get the view elements that are inflated in another activity. I am not proud of the solution that I posted and definitely there are other graceful ways of doing such things. However, I thought as a beginner (as you have stated), the procedure I mentioned should be easier to follow.
  • As long as it works when implemented, I'll be fine with it, thanks.
  • Unfortunately, after trying this the application still crashes at the exact same place
  • I think this the not the reason why the app is crashing. He does not have the radio buttons in the layout for the answer activity.
  • Unfortunately I still get the same problem, will have to use intent extras.
  • can you share your answer activity