Android NullPointerException同时在onCreateView上实例化ViewPager Fragments,但在浏览视图时却没有

时间:2014-06-24 16:37:36

标签: java android android-fragments nullpointerexception android-viewpager

我正在尝试使用能够在不同片段之间进行切换的片段来填充视图寻呼机。目前,当片段调用onCreateView()时,在尝试在TextView中设置文本时会抛出NullPointerException,但奇怪的是,如果我捕获异常,我可以浏览到其他所有正确显示的片段。

注意:第一个和第二个项目不会在ViewPager中呈现,但如果我转换到最后一个片段然后再返回它们,它们会正确显示。

这是我的片段类:

public class MyFragment extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";

public static LogicController controller;
public static MainActivity activity;

public static final MyFragment newInstance(String message, LogicController controller, MainActivity activity)
{
    MyFragment.activity = activity;
    MyFragment.controller = controller;
    MyFragment f = new MyFragment();
    Bundle bdl = new Bundle(1);
    bdl.putString(EXTRA_MESSAGE, message);
    f.setArguments(bdl);
    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
    try {
        String message = getArguments().getString(EXTRA_MESSAGE);
        //      View v = inflater.inflate(R.layout.myfragment_layout, container, false);
        View v = activity.getLayoutInflater().inflate(R.layout.myfragment_layout, null);

        //      View mainView = pager;

        TextView messageTextView = (TextView)v.findViewById(R.id.testertextView);

        System.out.println(controller.getUpdateGeoNetworkDelay() + messageTextView.getText().toString());



        messageTextView.setText(message);

        return v;


    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

}

这是我的片段寻呼机适配器:

public class MainPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;

public MainPageAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {

    Fragment frag = this.fragments.get(position);
    System.out.println("FRAGMENT: " + frag + " pos: " +  position);
    return frag;
}

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

抛出异常:

messageTextView.setText(message);

我有理由相信问题是由于没有正确引用textview,但这超出了我的专业水平。

Logcat:

06-24 17:30:44.962: W/System.err(25639): java.lang.NullPointerException
06-24 17:30:44.962: W/System.err(25639):    at com.g.geonet.items.MyFragment.onCreateView(MyFragment.java:48)
06-24 17:30:44.962: W/System.err(25639):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
06-24 17:30:44.962: W/System.err(25639):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
06-24 17:30:44.962: W/System.err(25639):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
06-24 17:30:44.962: W/System.err(25639):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-24 17:30:44.962: W/System.err(25639):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
06-24 17:30:44.962: W/System.err(25639):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
06-24 17:30:44.962: W/System.err(25639):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
06-24 17:30:44.962: W/System.err(25639):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
06-24 17:30:44.962: W/System.err(25639):    at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
06-24 17:30:44.962: W/System.err(25639):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
06-24 17:30:44.962: W/System.err(25639):    at android.view.View.measure(View.java:16504)
06-24 17:30:44.962: W/System.err(25639):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
06-24 17:30:44.962: W/System.err(25639):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-24 17:30:44.962: W/System.err(25639):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-24 17:30:44.962: W/System.err(25639):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-24 17:30:44.962: W/System.err(25639):    at android.view.View.measure(View.java:16504)
06-24 17:30:44.962: W/System.err(25639):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
06-24 17:30:44.962: W/System.err(25639):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-24 17:30:44.962: W/System.err(25639):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-24 17:30:44.962: W/System.err(25639):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-24 17:30:44.962: W/System.err(25639):    at android.view.View.measure(View.java:16504)
06-24 17:30:44.962: W/System.err(25639):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
06-24 17:30:44.962: W/System.err(25639):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-24 17:30:44.962: W/System.err(25639):    at android.view.View.measure(View.java:16504)
06-24 17:30:44.962: W/System.err(25639):    at com.sothree.slidinguppanel.SlidingUpPanelLayout.onMeasure(SlidingUpPanelLayout.java:516)
06-24 17:30:44.962: W/System.err(25639):    at android.view.View.measure(View.java:16504)
06-24 17:30:44.962: W/System.err(25639):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
06-24 17:30:44.962: W/System.err(25639):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
06-24 17:30:44.962: W/System.err(25639):    at android.view.View.measure(View.java:16504)
06-24 17:30:44.962: W/System.err(25639):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
06-24 17:30:44.962: W/System.err(25639):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-24 17:30:44.962: W/System.err(25639):    at android.view.View.measure(View.java:16504)
06-24 17:30:44.962: W/System.err(25639):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
06-24 17:30:44.962: W/System.err(25639):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-24 17:30:44.962: W/System.err(25639):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-24 17:30:44.962: W/System.err(25639):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-24 17:30:44.972: W/System.err(25639):    at android.view.View.measure(View.java:16504)
06-24 17:30:44.972: W/System.err(25639):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
06-24 17:30:44.972: W/System.err(25639):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-24 17:30:44.972: W/System.err(25639):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
06-24 17:30:44.972: W/System.err(25639):    at android.view.View.measure(View.java:16504)
06-24 17:30:44.972: W/System.err(25639):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
06-24 17:30:44.972: W/System.err(25639):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
06-24 17:30:44.972: W/System.err(25639):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
06-24 17:30:44.972: W/System.err(25639):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
06-24 17:30:44.972: W/System.err(25639):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
06-24 17:30:44.972: W/System.err(25639):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
06-24 17:30:44.972: W/System.err(25639):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
06-24 17:30:44.972: W/System.err(25639):    at android.view.Choreographer.doFrame(Choreographer.java:544)
06-24 17:30:44.972: W/System.err(25639):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
06-24 17:30:44.972: W/System.err(25639):    at android.os.Handler.handleCallback(Handler.java:733)
06-24 17:30:44.972: W/System.err(25639):    at android.os.Handler.dispatchMessage(Handler.java:95)
06-24 17:30:44.972: W/System.err(25639):    at android.os.Looper.loop(Looper.java:136)
06-24 17:30:44.972: W/System.err(25639):    at android.app.ActivityThread.main(ActivityThread.java:5001)
06-24 17:30:44.972: W/System.err(25639):    at java.lang.reflect.Method.invokeNative(Native Method)
06-24 17:30:44.972: W/System.err(25639):    at java.lang.reflect.Method.invoke(Method.java:515)
06-24 17:30:44.972: W/System.err(25639):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-24 17:30:44.972: W/System.err(25639):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-24 17:30:44.972: W/System.err(25639):    at dalvik.system.NativeStart.main(Native Method)

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

请检查此行:

System.out.println(controller.getUpdateGeoNetworkDelay() + messageTextView.getText().toString());
    messageTextView.setText(message);

我的猜测是,message是从newInstance传递的bundle中的参数的内容,所以它应该是这样的:

    messageTextView.setText(getArguments().getStringExtra(EXTRA_MESSAGE,"Message not passed"));

此行中的最终修饰符公共静态最终MyFragment newInstance 似乎不合适,因此将其删除。