我在我的應用程序中遇到了一個奇怪的問題。我想從另一個類(不是活動)更新TextViews。我可以通過以下兩種方式調用getUpdate()
方法:findViewById從另一個類中調用有時返回null
這就是我從MainActivity調用它們的方式: 這種方式可以正常工作。
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.menu_location_button:
if (this.cwu == null)
this.cwu = new CurrentWeatherUpdater(this);
this.cwu.getUpdate(lf.getLongitude(), lf.getLatitude());
break;
.
.
.
}
return super.onOptionsItemSelected(item);
}
這一個不:
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
if (this.cwu == null)
this.cwu = new CurrentWeatherUpdater(this);
this.cwu.getUpdate(query);
}
}
query
從搜索查看
CurrentWeatherUpdater cwu;
在MainActivity.java
宣佈收到這是我CurrentWeatherUpdater類:
public class CurrentWeatherUpdater extends Updater{
private TextView city;
private TextView temp;
private TextView desc;
private TextView humidity;
private TextView pressure;
private TextView wind;
public CurrentWeatherUpdater(Context context) {
super(context);
this.city = (TextView) ((Activity)context).findViewById(R.id.city_name);
this.temp = (TextView) ((Activity)context).findViewById(R.id.current_temperature);
this.desc = (TextView) ((Activity)context).findViewById(R.id.current_weather_desc);
this.humidity = (TextView) ((Activity)context).findViewById(R.id.humidity);
this.pressure = (TextView) ((Activity)context).findViewById(R.id.pressure);
this.wind = (TextView) ((Activity)context).findViewById(R.id.wind);
}
public void getUpdate(String cityName) {
updateView(super.getUpdate(Updater.REQUEST_WEATHER, cityName));
}
public void getUpdate(double longitude, double latitude) {
updateView(super.getUpdate(Updater.REQUEST_WEATHER, longitude, latitude));
}
private void updateView(JSONObject result) {
try {
if (result.getInt("cod") == 200) {
this.city.setText(result.getString("name"));
JSONObject main = result.getJSONObject("main");
temp.setText(Integer.toString((int) Math.round(Double.parseDouble(main.getString("temp")))) + "\u00b0");
pressure.setText(main.getString("pressure") + "hPa");
humidity.setText(main.getString("humidity") + "%");
JSONObject windInfo = result.getJSONObject("wind");
wind.setText(windInfo.getString("speed") + "m/s");
JSONArray weatherArray = result.getJSONArray("weather");
JSONObject weather = weatherArray.getJSONObject(0);
desc.setText(weather.getString("main"));
} if (result.getInt("cod") == 404) {
Toast.makeText(mContext, "Location not found", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
CurrentWeatherUpdater cwu;
在MainActivity.java
聲明基本上當我打電話CurrentWeatherUpdater(this)
構造從onOptionsItemSelected
它工作正常。但是,當我把它從doSearch
,即使context
點MainActivity
就像在另外一個,沒有任何字段(city
,temp
等)分配一個值和updateView
方法下山與NullPointerException
火焰上this.city.setText(result.getString("name"))
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
即使當它第一次從onOptionsItemSelected
叫和CurrentWeatherUpdater cwu
對象應該已經創建了if (this.cwu == null)
計算結果爲true
並創建新對象。
而不是鑄造'活動',嘗試'MainActivity' – adnbsr
謝謝你的建議,但沒有解決它。 – dgabka95
嘗試'city'而不是'this.city' – W4R10CK