2017-01-23 58 views
0

我在我的應用程序中遇到了一個奇怪的問題。我想從另一個類(不是活動)更新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,即使contextMainActivity就像在另外一個,沒有任何字段(citytemp等)分配一個值和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並創建新對象。

+0

而不是鑄造'活動',嘗試'MainActivity' – adnbsr

+0

謝謝你的建議,但沒有解決它。 – dgabka95

+0

嘗試'city'而不是'this.city' – W4R10CK

回答

0

你可以使用LocalBroadcastManager將數據廣播到其他類

可以將數據通過

Intent intent = new Intent("myFunction"); 
       // add data 
       intent.putExtra("variable1", "Data1"); 
       intent.putExtra("variable2", "Data2"); 
       intent.putExtra("variable3", "Data3"); 
       LocalBroadcastManager.getInstance(this).sendBroadcast(intent); 

類,其中更新視圖定義把這段代碼設置爲LocalBroadcastManager與現在:

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      // Extract data included in the Intent 
      String t = intent.getStringExtra("variable1"); 
      String t1 = intent.getStringExtra("variable2"); 
      String t2 = intent.getStringExtra("variable3"); 
      //Updating textview function 
      updateTheTextView(t, t1, t2); 
     } 
    }; 
    @Override 
    public void onResume() { 
     super.onResume(); 
     LocalBroadcastManager.getInstance(this.getActivity()).registerReceiver(mMessageReceiver, 
       new IntentFilter("myFunction")); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     LocalBroadcastManager.getInstance(this.getActivity()).unregisterReceiver(mMessageReceiver); 
    } 

我不確定這是否對您有幫助!但這就是我如何將數據發送到另一個類中定義的texviews。