Why does my dialog do not work when I want to display my content?

Viewed 125

Currently, I am working on an android studio app. This app needs to display a map where a button is on it. If I press this button, a dialog with the weather should pop up. The thing is that whenever I press on it an error appears. (I already know that the weather works because I can show it in my map activity but not in a dialog called from the map activity

This is the error:

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

This is the code for my button:

 public void startDialogClima(View view) {
        AlertDialog.Builder mBuilder = new AlertDialog.Builder(MapsActivity.this);
        View mView = getLayoutInflater().inflate(R.layout.dialog_playas, null);
        mBuilder.setView(mView);

        climaText = (TextView) findViewById(R.id.climaText);
        downloadTask.placeIdTask asyncTask = new downloadTask.placeIdTask(new downloadTask.AsyncResponse() {
            public void processFinish(String weather_city, String weather_description, String weather_temperature, String weather_humidity, String weather_pressure, String weather_updatedOn, String weather_iconText, String sun_rise) {


                climaText.setText(weather_temperature);
                //desc.setText(weather_description);
                //weatherIcon.setText(Html.fromHtml(weather_iconText));

            }
        });
        asyncTask.execute("-34.83", "-56.17"); //  asyncTask.execute("Latitude", "Longitude")

        AlertDialog dialog = mBuilder.create();
        dialog.show();
    }

XML code for the dialog:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/climaText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_marginTop="20dp"
        android:layout_marginStart="120dp"
        />
</LinearLayout
3 Answers

Try this:

climaText = (TextView) mView.findViewById(R.id.climaText);

to find TextView in a dialog view.

You forgot to indicate what climaText means.

Try replacing this:

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

with this:

TextView climaText = (TextView) mView.findViewById(R.id.climaText);

If it dosen't work try:

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

You should inflate your TextView from dialog view

replce

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

with

climaText = (TextView) mView.findViewById(R.id.climaText);
Related