我是初学者。我在android中编写一个天气应用程序。我想在活动中调用片段方法,但它有错误
我的主要活动代码在这里:
Autor = rss.Element("{http://purl.org/dc/elements/1.1/}creator").Value,
并显示错误
public class MainActivity extends FragmentActivity {
ViewPager viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager)findViewById(R.id.viewPager);
MyFragmentAdapter adapter = new MyFragmentAdapter(getSupportFragmentManager());
viewpager.setAdapter(adapter);
}
private class MyFragmentAdapter extends FragmentPagerAdapter{
public MyFragmentAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
switch (position) {
case 0:
return new WeatherCurrentFragment();
case 1:
return new WeatherForeCastFragment();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.weather, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if(item.getItemId() == R.id.change_city){
showInputDialog();
}
return super.onOptionsItemSelected(item);
}
private void showInputDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Change city");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
changeCity(input.getText().toString());
}
});
builder.show();
}
public void changeCity(String city){
WeatherCurrentFragment cf = (WeatherCurrentFragment)getSupportFragmentManager().findFragmentById(R.id.currentweather);
cf.updateWeatherData(city);
WeatherForeCastFragment ff = (WeatherForeCastFragmen)getSupportFragmentManager().findFragmentById(R.id.forecast);
ff.updateWeatherData(city);
new CityPreference(this).setCity(city);
}
}
如你所见,当我点击按钮时,我想从CurrentWeatherFragment调用方法一,从ForecastWeatherFragment调用一个方法。
答案 0 :(得分:2)
以下代码:
getSupportFragmentManager().findFragmentById(R.id.currentweather);
返回一个空对象引用。
这个问题可能会对您有所帮助,findFragmentById return null
顺便说一下,通常使用回调接口而不是在这样的活动中调用属于fragment的方法:
public interface OnCityChangedListener(){
public void onCityChanged(String city);
}
在Fragment中实现此接口:
public MyFragment extends Fragment implements OnCityChangedListener
并在Fragment中重写此方法:
@override
public void onCityChanged(String city){
updateWeatherData(city);
}
在活动中实例化片段,使用ArrayList
将其保存在活动中,将此ArrayList
传递给PageAdapter
的构造函数,
public MyFragmentAdapter(FragmentManager fm, ArrayList<Fragment> fs) {
super(fm);
this.fs = fs;
}
点击按钮更改城市时:
public void changeCity(String city){
fs.onCityChanged(city);
}
答案 1 :(得分:0)
问题是FragmentPagerAdapter
通过FragmentManager
管理传递给FragmentPagerAdapter
构造函数的片段。
所以,只要你致电getSupportFragmentManager().findFragmentById()
,就不能保证它不会返回null
。
更好的设计方法是制作CityPreference
单例对象并在Application.onCreate()
方法中实例化它:
public class WeatherApplication extends Application {
private static WeatherApplication instance;
private CityPreference cityPreference;
public static WeatherApplication getInstance() {
return instance;
}
@Override
public void onCreate() {
super.onCreate();
instance = this;
cityPreference = new CityPreference(this);
}
public CityPreference getCityPreference() {
return cityPreference;
}
}
然后你应该为&#34;城市改变&#34;添加听众。事件:
class CityPreference {
public CityPreference(Context context) {
// initialize SharedPreferences or any other storage
}
private final List<CityChangedListener> listeners = new ArrayList<>();
public void addListener(CityChangedListener listener) {
listeners.add(listener);
}
public void removeListener(CityChangedListener listener) {
listeners.remove(listener);
}
public void setCity(String city) {
// save city to shared preferences or some other place
for (listener: listeners) {
listener.onCityChanged();
}
}
public interface CityChangedListener {
void onCityChanged();
}
}
在每个片段中进行以下更改:
class WeatherCurrentFragment extends Fragment implements CityChangedListener {
@Override
public void onResume() {
super.onResume();
onCityChanged(); // force fragment update because city could be changed when fragment was hidden or even not yet created
WeatherApplication.getInstance().getCityPreference().addListener(this);
}
@Override
public void onPause() {
super.onPause();
WeatherApplication.getInstance().getCityPreference().removeListener(this);
}
public void onCityChanged() {
// update fragment
}
}
最后是你的MainActivity.changeCity()
方法:
public class MainActivity extends FragmentActivity {
// other code
private void changeCity(String city) {
WeatherApplication.getInstance().getCityPreference().setCity(city);
}
}