0

when I try to generate this code, this error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference.

That is my code:

public class MainActivity extends AppCompatActivity implements   View.OnTouchListener{
private Tablero fondo;
int x;
int y;
private boolean activo = true;
int intento = 10;
String numerointentos;


     @Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
    TextView Tiradas = (TextView) findViewById(R.id.Intentos) ;
    numerointentos = Integer.toString(intento);
    Tiradas.setText(numerointentos);

    fondo = new Tablero(this);
    fondo.setOnTouchListener(this);
    linearLayout1.addView(fondo);
    fondo.casillas = new Casilla[8][8];
    for (int f = 0; f < 8; f++){
        for (int c = 0; c < 8; c++){
            fondo.casillas[f][c] = new Casilla();
        }
    }

And this is the .xml code:

<?xml version= "1.01" encoding="utf-8"?>

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width= "fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="1">
 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/reiniciar"
    android:id="@+id/btnReiniciar"
    android:onClick="presionado"
    android:layout_gravity="center_horizontal" />

 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textAppearance="?android:attr/textAppearanceMedium"
     android:id="@+id/Intentos"
     android:layout_gravity="center_horizontal" />

Obviously there is more code and code and clases, but that is the unique that contains the setText.

And another question. If I want to modify the value of the TextView in another public voids or methods what should I do?

I appreciate your help guys, I searched for anwsers on that web but I can't solve it. I don't know the reason. Thanks!

2 Answers2

0

Why don't you try making text view a global variable? Then you can access that text field in other functions too.

Like this.

public class MainActivity extends AppCompatActivity { TextView Tiradas; @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManAger.LayoutParams.FLAG_FULLSCREEN); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1); Tiradas = (TextView) findViewById(R.id.Intentos) ; numerointentos = Integer.toString(intento); Tiradas.setText(numerointentos); fondo = new Tablero(this); } }

Snippy Valson
  • 231
  • 3
  • 9
0

Move the declaration of the TextView as the class' property

public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
    public TextView Tiradas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView( ... );

        Tiradas = (TextView) findViewById(R.id.Intentos);
        Tiradas.setText(numerointentos);
    }
}

EDIT:

Make sure that you have no other variants of activity_main.xml file (such as for landscape or bigger screen size). If you have, add the missing views to that layout file.

Because findViewById(int) will return null if the view is not found in the referenced layout.

Wilik
  • 7,630
  • 3
  • 29
  • 35
  • Hi, I'm going to add what I have before @override, look it, because i don't have tha same public class. Thanks! – Daniel Leon Gonzalez Sep 28 '16 at 20:26
  • There is the new code, thanks! – Daniel Leon Gonzalez Sep 28 '16 at 20:28
  • it's the same :) as long as the `Tiradas` variable is the class' variable (global variable) (not as local variable inside the `onCreate` method, then the NPE error will not occur if you're doing `setText` inside `onCreate`. I have updated my code btw, hope this helps – Wilik Sep 28 '16 at 20:33
  • Hi I tried to do like you and keeps crashing at the same point. Thanks! – Daniel Leon Gonzalez Sep 28 '16 at 20:42
  • from [findViewById documentation](https://developer.android.com/reference/android/app/Activity.html#findViewById(int)), it looks like the view is not found. Are you sure that `activity_main` xml layout file has a view with id `Intentos` ? or probably you're referencing another layout instead. Also make sure that you have no other varians of `activity_main` xml file (for landscape or bigger screen size) – Wilik Sep 28 '16 at 20:53
  • THANKS!! That's IT! I don't know why I have the folder /layout/activity_main.xml and /layout-v21/activity:main.xml Only the file of /layout was modified. Now it Works! THANKS A LOT to all! – Daniel Leon Gonzalez Sep 28 '16 at 21:05
  • wohoo :D please upvote and mark my answer as accepted answer. I have edited my answer that solves your question. – Wilik Sep 28 '16 at 21:08