我在手机和平板电脑上做了不同布局的应用。对于手机,我有ViewPager with Fragments和平板电脑,我在.xml布局文件中有片段。当我尝试从该片段中的活动中获取特定片段和使用方法时,我得到Null Pointer Exception 我该如何解决?当我使用ViewPager在手机上运行应用程序时,我得到Null Pointer但是当我的平板电脑包含.xml布局的片段时,它是可以的。
MainActivity中的方法:
private ViewPager viewPager;
private PagerAdapter adapter;
private TabLayout tableLayout;
private void setViewPager(){
viewPager = findViewById(R.id.viewpager);
adapter = new PagerAdapter(getSupportFragmentManager(), 2);
adapter.addFragment(new SunFragment(), "Sun");
adapter.addFragment(new MoonFragment(), "Moon");
tableLayout = findViewById(R.id.tabLayout);
if(viewPager != null) {
viewPager.setAdapter(adapter);
}
if(tableLayout != null){
tableLayout.setupWithViewPager(viewPager);
}
}
private void refreshView(){
if(viewPager != null) {
adapter.getSunFragment().calculateWeather();
adapter.getMoonFragment().calculateWeather();
}else if(sunFragment != null){
sunFragment.calculateWeather();
moonFragment.calculateWeather();
}
}
当我尝试使用activity中的方法并得到一个double值时,使用Null指针部分片段:
public void calculateWeather(){
longitude = ((MainActivity)getActivity()).getLon();
latitude = ((MainActivity)getActivity()).getLat();
}
查看寻呼机适配器:
public class PagerAdapter extends FragmentStatePagerAdapter {
private int fragmentNumber;
private List<Fragment> fragmentList = new ArrayList<>();
private List<String> fragmentsNameList = new ArrayList<>();
public PagerAdapter(FragmentManager fm, int fragmentNumber){
super(fm);
this.fragmentNumber = fragmentNumber;
}
public void addFragment(Fragment fragment, String name){
fragmentList.add(fragment);
fragmentsNameList.add(name);
}
public SunFragment getSunFragment(){
return (SunFragment)fragmentList.get(0);
}
public MoonFragment getMoonFragment(){
return (MoonFragment)fragmentList.get(1);
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentNumber;
}
@Override
public CharSequence getPageTitle(int position) {
return fragmentsNameList.get(position);
}
}
//修改
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mariusz.astroweather, PID: 4970
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mariusz.astroweather/com.example.mariusz.astroweather.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'double com.example.mariusz.astroweather.MainActivity.getLon()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double com.example.mariusz.astroweather.MainActivity.getLon()' on a null object reference
at com.example.mariusz.astroweather.SunFragment.calculateWeather(SunFragment.java:56)
at com.example.mariusz.astroweather.MainActivity.refreshView(MainActivity.java:124)
at com.example.mariusz.astroweather.MainActivity.onCreate(MainActivity.java:49)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
com.example.mariusz.astroweather.SunFragment.calculateWeather(SunFragment.java:56)
是行:
经度=((MainActivity)getActivity())。getLon();
添加MainActivity的创建:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setViewPager();
refreshView();
}
答案 0 :(得分:0)
根据你的意见,如果。你可以在活动中使用片段的权力,然后你就必须做这样的事情
YourFragment fragment = (YourFragment) getFragmentManager().findFragmentById(R.id.YourFragment);
//any mothed of your fragment that you like
fragment.calculateWeather();
为了调用片段内部的活动方法,这是可行的方法
因为你的logCat我预见到你在错误的地方使用你的片段来调用活动方法! 你应该在片段的OnActivityCreated()中使用你的这种方法,而不是在其他地方使用它吗?
// use this
public void calculateWeather(){
longitude = ((MainActivity)getActivity()).getLon();
latitude = ((MainActivity)getActivity()).getLat();
}
//as
//in your fragment
@Override
public void OnActvityCreated(){
// .... super()....
//......
calculateWeather();
//......
}