I am working on a project where I need 3 textViews to change when a button is clicked.
Here is my MainActivity class:
package davidferrara.insultgenerator;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private boolean mHomeLayout = true;
private Resources res;
private TextView tvInsult1;
private TextView tvInsult2;
private TextView tvInsult3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(mHomeLayout) {
setContentView(R.layout.home_screen);
}
else {
setContentView(R.layout.insult_screen);
}
findViewById(R.id.generateInsultButton).setOnClickListener(btnMainButton);
assignTextViews();
}
//The generate insult button
final View.OnClickListener btnMainButton = new View.OnClickListener() {
@Override
public void onClick(final View v) {
mHomeLayout = false;
setContentView(R.layout.insult_screen);
generateInsult();
}
};
private void generateInsult(){
res = getResources();
String[] array1 = res.getStringArray(R.array.insults0);
String randomStr1 = array1[new Random().nextInt(array1.length)];
tvInsult1.setText(randomStr1);
String[] array2 = res.getStringArray(R.array.insults1);
String randomStr2 = array2[new Random().nextInt(array2.length)];
tvInsult2.setText(randomStr2);
String[] array3 = res.getStringArray(R.array.insults2);
String randomStr3 = array3[new Random().nextInt(array3.length)];
tvInsult3.setText(randomStr3);
}
private void assignTextViews(){
tvInsult1 = findViewById(R.id.insultText1);
tvInsult2 = findViewById(R.id.insultText2);
tvInsult3 = findViewById(R.id.insultText3);
}
}
In this class I have 3 textViews, tvInsult1
, tvInsult2
, and tvInsult3
that are declared. These are then assigned the corresponding textViews from the insult_screen
using the assignTextViews
method.
When the button is clicked, it sets the layout to insult_screen
and the generateInsult
method is called. In this method, I set the string variable randomStr1
a string from an array of strings stored in my values folder. The textView tvInsult1
then gets the text set using randomStr1
, or so it should.
When I ran the program, I got this error for the line tvInsult1.setText(randomStr1);
:
01-04 18:44:51.160 7699-7699/davidferrara.insultgenerator E/AndroidRuntime: FATAL EXCEPTION: main
Process: davidferrara.insultgenerator, PID: 7699
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
I went back and put a break right before that line of code to debug it and it turns out that tvInsult1
was null. This is where I'm stuck. I already assigned the views using the findViewById. I've looked at multiple other issues similar to this one but none of the solutions worked for me.
Here is the other related code:
Home Screen XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/welcome_message" />
<Button
android:id="@+id/generateInsultButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/generate_button" />
</LinearLayout>
Insult Screen XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="davidferrara.insultgenerator.MainActivity">
<TextView
android:id="@+id/insultText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible" />
<TextView
android:id="@+id/insultText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible" />
<TextView
android:id="@+id/insultText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible" />
<Button
android:id="@+id/generateInsultButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/generate_button" />
</LinearLayout>
Here is a picture of the debugger saying that the array isn't empty and that the randomStr1
has a value assigned to it from the array: