qq_42252333
何陋之有
2018-12-21 01:04

安卓开发Fragment里面空指针问题求大神赐教

  • android-studio

报错代码:

12-20 10:21:08.243 4780-4780/? E/AndroidRuntime: FATAL EXCEPTION: main

                                             Process: com.example.asus.accuntingapp, PID: 4780
                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asus.accuntingapp/com.example.asus.accuntingapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.LinkedList com.example.asus.accuntingapp.RecordDatabaseHelper.getAvaliableDate()' on a null object reference
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                 at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:135)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.LinkedList com.example.asus.accuntingapp.RecordDatabaseHelper.getAvaliableDate()' on a null object reference
                                                 at com.example.asus.accuntingapp.MainViewPagerAdapter.initFragments(MainViewPagerAdapter.java:23)
                                                 at com.example.asus.accuntingapp.MainViewPagerAdapter.<init>(MainViewPagerAdapter.java:19)
                                                 at com.example.asus.accuntingapp.MainActivity.onCreate(MainActivity.java:28)
                                                 at android.app.Activity.performCreate(Activity.java:5933)
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                 at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                 at android.os.Looper.loop(Looper.java:135) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:5221) 
                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                 at java.lang.reflect.Method.invoke(Method.java:372) 
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

相关代码:

public class MainViewPagerAdapter extends FragmentPagerAdapter {
LinkedList fragments=new LinkedList<>();
LinkedList dates=new LinkedList<>();

public MainViewPagerAdapter(FragmentManager fm) {
    super(fm);
    initFragments();
}
private void initFragments(){

    dates = GlobalUtil.getInstance().databaseHelper.getAvaliableDate();

  if(!dates.contains(DateUtil.getFormattedDate())){
       dates.addLast(DateUtil.getFormattedDate());
   }
    for(String date:dates){
        Mainfragment fragment=new Mainfragment(date);
        fragments.add(fragment);
    }
}
public  void reload(){
    for(Mainfragment fragment:fragments){
        fragment.reload();
    }
}
public int getLastIndex(){
    return fragments.size()-1;
}

@Override
public Fragment getItem(int position) {
    return fragments.get(position);
}

@Override
public int getCount() {
    return fragments.size();
}

}

public class MainActivity extends AppCompatActivity {
private TickerView tickerView;
private ViewPager viewPager;
private MainViewPagerAdapter pagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getSupportActionBar().setElevation(0);
    viewPager=(ViewPager)findViewById(R.id.view_pager);
    pagerAdapter=new MainViewPagerAdapter(getSupportFragmentManager());
    pagerAdapter.notifyDataSetChanged();
    viewPager.setAdapter(pagerAdapter);
    viewPager.setCurrentItem(pagerAdapter.getLastIndex());
    findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent=new Intent(MainActivity.this,AddRecordActivity.class);
            startActivityForResult(intent,1);
        }
    });
    GlobalUtil.getInstance().setContext(getApplicationContext());
    LinkedList<String> dates=new LinkedList<>();
    dates = GlobalUtil.getInstance().databaseHelper.getAvaliableDate();
    Log.d("MainActivity","时间"+dates);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    pagerAdapter.reload();
}

}

我在别的地方调用这个getAvaliableDate()方法没问题,在MainViewPagerAdapter类里面的initFragments()方法里面调用getAvaliableDate()就会报空指针。我不知道为什么,望大神赐教!

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答

为你推荐

换一换