2017-10-04 12 views
1

현재 저는 Android 스튜디오 앱을 개발 중입니다. 이 앱은 버튼이있는지도를 표시해야합니다. 이 버튼을 누르면 날씨가 표시된 대화 상자가 나타납니다. 문제는 내가 누를 때마다 오류가 나타나는 것입니다. 은 (이미 날씨가 나는지도 활동내 콘텐츠를 표시하고 싶을 때 대화 상자가 작동하지 않는 이유는 무엇입니까?

이 호출 대화 상자에서 내지도 활동에 보여하지만 할 수 없기 때문에 작동 오류 것을 알고 :이 코드입니다

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

내 버튼 : 대화 상자에 대한

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 코드 :

<?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 

답변

1

이것을보십시오 :

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

가 대화보기에서 텍스트 뷰를 찾을 수 있습니다.

1

climaText가 의미하는 것을 잊어 버렸습니다.

이 교체하십시오 :이와

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

을 :

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

이 작업의 시도 나던 경우

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

당신은 대화보기

에서 텍스트 뷰를 팽창한다을

replce

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

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