Android: Error: void android.widget.TextView.setText (java.lang.CharSequence) 'on a null object reference

2

Good morning, please support me with this code that I try to run but it throws me error:

  

Error: void android.widget.TextView.setText (java.lang.CharSequence) '   on a null object reference

The code is as follows:

package bernascompany.androidvolleyejemplo20170826;

import android.graphics.Picture;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.w3c.dom.Text;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button boton;
    TextView textView;


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

        //final TextView textView = (TextView)findViewById(R.id.textView); 
        TextView textView = (TextView)findViewById(R.id.textView);  //tambien probe con la linea anterior pero no funciona

        Button boton = (Button)findViewById(R.id.button);
        boton.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {


        RequestQueue queue = Volley.newRequestQueue(this);
        String URL = "http://192.168.1.37:8082/webservice20170818/androidvolley.php?id=XX3333XX";

        StringRequest stringRequest=new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG).show();  
                textView.setText("Resultado : "+response);  // aqui es donde encuentro el error pero nose como darle solucion, si quito esta linea funciona perfecto

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(MainActivity.this, "No se pudo realizar la solicitud......", Toast.LENGTH_LONG).show();
                //textView.setText("No se pudo realizar la solicitud......");
            }
        });
        queue.add(stringRequest);       
    }
}

I notice that the ID of the text view in the xml is well written:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:background="@drawable/ic_lightbulb"

    android:layout_marginTop="60dp"
    android:layout_centerHorizontal="true" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/ic_lightbulb" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageButton"
    android:src="@drawable/ic_lightbulb"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="76dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

here the Manifest:

    

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    android:name="bernascompany.AppController"
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Thanks in advance for your help. Greetings.

    
asked by Jberni 21.10.2017 в 13:11
source

1 answer

1

To enter context, this line indicates which is the layout to load in your Activity :

setContentView(R.layout.activity_main);

In this case it is activity_main.xml . But the error indicates that you can not use the setText() method in a null value instance of a TextView , usually the problem here is that the TextView with the id you indicate ( textView ) probably does not exist.

TextView textView = (TextView)findViewById(R.id.textView);

Reviewing your information actually this element exists, the problem is that you are getting the reference for the variable textView that you could only use within onCreateView() , you have already defined a variable for the whole class, so if you get the reference of The view can be used elsewhere in your Activity , this corrects the problem:

textView = (TextView)findViewById(R.id.textView);
    
answered by 21.10.2017 в 13:50