Android视图是片段内的空对象引用

时间:2018-04-02 22:17:51

标签: java android android-fragments

我在使用某些代码时遇到了困难。我正在使用带有2个片段的滑动标签。在我的一个片段中,我试图获得一个LinearLayout并在其中添加一些东西,但每次我尝试getView().findViewByID()我都得到了空对象引用

我正在onCreateView()之后调用该函数我尝试创建一个View类型的变量并将我在onCreateView中膨胀的View放入其中但我一直得到相同的错误。我还尝试在之前调用if()来检查view是否为null,但即使这样,我也得到null对象引用。有人能帮助我吗?

public class listAlarmFragment extends Fragment{

  public View view = null;
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle bundle){
     View v = inflater.inflate(R.layout.list_alarm,vg,false);
     view = v;
     return v;
  }


   public void showAlarm(Cursor c){
     if(view != null) { //even with that check, I got a null pointer exception
        LinearLayout baseList = (LinearLayout) view.findViewById(R.id.baseList); // NullPointerException: Attempt to invoke virtual method on a null object reference
      }
   }
 }

用getView()替换视图不会改变任何东西。 这就是我从主活动中调用函数的方法

   pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
        Fragment c = adapter.getItem(0);
         ((listAlarmFragment)(c)).showAlarm(dbHelper.getAlarm()); // where I call the function
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

当我的应用程序打开时,我正在打开这个片段。当我试图切换到另一个片段时崩溃。

这是堆栈跟踪:

04-02 18:41:54.615 31584-31584/al.demo.alarmmanagerdemo E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: al.demo.alarmmanagerdemo, PID: 31584
                                                                      java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
                                                                          at al.demo.alarmmanagerdemo.fragment.listAlarmFragment.showAlarm(listAlarmFragment.java:83)
                                                                          at al.demo.alarmmanagerdemo.MainActivity$1.onPageSelected(MainActivity.java:54)
                                                                          at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1971)
                                                                          at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:689)
                                                                          at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:673)
                                                                          at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2288)
                                                                          at android.view.View.dispatchTouchEvent(View.java:10779)
                                                                          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2858)
                                                                          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2534)
                                                                          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2864)
                                                                          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2549)
                                                                          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2864)
                                                                          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2549)
                                                                          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2864)
                                                                          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2549)
                                                                          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2864)
                                                                          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2549)
                                                                          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2864)
                                                                          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2549)
                                                                          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2864)
                                                                          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2549)
                                                                          at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:605)
                                                                          at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1895)
                                                                          at android.app.Activity.dispatchTouchEvent(Activity.java:3241)
                                                                          at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:67)
                                                                          at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:67)
                                                                          at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:567)
                                                                          at android.view.View.dispatchPointerEvent(View.java:11008)
                                                                          at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5155)
                                                                          at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5007)
                                                                          at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4532)
                                                                          at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4585)
                                                                          at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4551)
                                                                          at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4684)
                                                                          at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4559)
                                                                          at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4741)
                                                                          at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4532)
                                                                          at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4585)
                                                                          at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4551)
                                                                          at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4559)
                                                                          at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4532)
                                                                          at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7092)
                                                                          at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7024)
                                                                          at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6985)
                                                                          at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7202)
                                                                          at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                                                                          at android.os.MessageQueue.nativePollOnce(Native Method)
                                                                          at android.os.MessageQueue.next(MessageQueue.java:323)
                                                                          at android.os.Looper.loop(Looper.java:136)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6776)
04-02 18:41:54.615 31584-31584/al.demo.alarmmanagerdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1510)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1400)

1 个答案:

答案 0 :(得分:2)

我会做这样的事情,

public class listAlarmFragment extends Fragment{

  //public View view = null; // no need
  private LinearLayout baseList; 
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle bundle){
     View v = inflater.inflate(R.layout.list_alarm,vg,false);
     baseList = (LinearLayout) v.findViewById(R.id.baseList);
     //view = v; no need
     return v;
  }


   public void showAlarm(Cursor c){
     if(baseList!=null){
     //do something with your baseList 
     }
   }
 }

同时检查布局xml R.layout.list_alarm中是否存在R.id.baseList。 它应该是LinearLayout with

id=@+id/baseList