为什么当我想要显示我的内容时,我的对话框不起作用?
问题描述:
目前,我正在开发一个android studio应用程序。这个应用程序需要显示一个地图上的按钮。如果我按下这个按钮,会弹出一个天气对话框。事情是,只要我按下它就会出现错误。 (我已经知道天气的作品,因为我可以在从地图活动为什么当我想要显示我的内容时,我的对话框不起作用?
这款名为对话框显示它在我的地图活动,但不为错误:
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
答
试试这个:
climaText = (TextView) mView.findViewById(R.id.climaText);
找到的TextView在对话视图。
答
你忘了说明climaText的含义。
尝试更换这样的:这个
climaText = (TextView) findViewById(R.id.climaText);
:
TextView climaText = (TextView) mView.findViewById(R.id.climaText);
如果这么想的工作尝试:
TextView climaText = (TextView) findViewById(R.id.climaText);
答
你应该从对话视图
虚增您的TextViewreplce
climaText = (TextView) findViewById(R.id.climaText);
与
climaText = (TextView) mView.findViewById(R.id.climaText);