2017-01-23 69 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。