1

我的目标是在主要活动中初始化视图,但我得到了 nullpointerexception。这是我的示例代码:

public class MainActivity extends SherlockFragmentActivity {
    private static final String TAG = "MainActivity";

    Fragment one, two, three;
    List<Fragment> fragments;
    ViewPager pager;
    private MyPagerAdapter mPagerAdapter;


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

    private void initialisePaging() {
        Log.d(TAG, "initialisePaging");
        fragments = new Vector<Fragment>();
        one = Fragment.instantiate(getApplicationContext(), One.class.getName());
        two = Fragment.instantiate(getApplicationContext(), Two.class.getName());
        three = Fragment.instantiate(getApplicationContext(), Three.class.getName());
        fragments.add(one);
        fragments.add(two);
        fragments.add(three);
        mPagerAdapter  = new MyPagerAdapter(getSupportFragmentManager(), fragments) ;
        pager = (ViewPager) findViewById(R.id.trinity_viewpager);
        pager.setAdapter(mPagerAdapter);
     }

    private void initTwo (){
        View twoView = mPagerAdapter.getItem(1).getView();
        TextView twoTv = (TextView) twoView.findViewById(R.id.frag_two_tv);
        twoTv.setText("2!!!!");
    }

    class MyPagerAdapter extends FragmentStatePagerAdapter  {
        private List<Fragment> fragments;

        public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;

        }

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

        @Override
        public int getCount() {
            return 3;
        }
    }
}

片段一、二和三仅包含 textView。我尝试初始化这些视图并得到 NullPointerException:

E/AndroidRuntime(2104): Caused by: java.lang.NullPointerException
E/AndroidRuntime(2104):     at ru.alxr.viewpagerapp.MainActivity.initTwo(MainActivity.java:53)
E/AndroidRuntime(2104):     at ru.alxr.viewpagerapp.MainActivity.onCreate(MainActivity.java:34)
E/AndroidRuntime(2104):     at android.app.Activity.performCreate(Activity.java:5008)
E/AndroidRuntime(2104):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
E/AndroidRuntime(2104):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
4

2 回答 2

2

在使用 findViewById 之前尝试膨胀视图。

于 2013-08-24T06:42:23.077 回答
0

更新我找到了解决问题的方法。最简单的方法是通过接口传递 vie:

这是适配器:

static class MyPagerAdapter extends FragmentPagerAdapter  {
    public static MyPagerAdapter instance = null;
    public static MyPagerAdapter getInstance(FragmentManager fm){
        if (instance == null) instance = new MyPagerAdapter(fm);
        return instance;
    }

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

   @Override
   public Fragment getItem(int position) {
   Fragment r = null;
        switch (position){
        default : break;
        case 0 : {
            r = One.newInstance();
            break;
            }

        case 1 : {
            r = Two.newInstance();
            break;
            }
        case 2 : {
            r = Three.newInstance();
            break;
            }
        }
    return r;
   }

    @Override
    public int getCount() {
        return 3;
    }
}

和片段之一:

  public final class One extends SherlockFragment {
        ViewOneCreated viewOneCreated;
        static One one;
        Button button;
        ExpandableListView someListView;

    Map <String, Object> classOneObjects;

    private static final String     
            FRAGMENT = "FRAGMENT", 
            LISTVIEW = "LISTVIEW",
            BUTTON = "BUTTON";

    public interface ViewOneCreated{
        public void fragmentOneCreated(Map <String, Object> classOneObjects);
    }

      @Override
      public void onAttach(Activity activity) {
        super.onAttach(activity);
            try {
                viewOneCreated = (ViewOneCreated) activity;
            } catch (ClassCastException e) {
                throw new ClassCastException(activity.toString() + " must implement onSomeEventListener");
            }
      }

        public static One instance() {
            if (one == null) one = new One();
            return one;
        }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View baseView = inflater.inflate(R.layout.one_fragment, null);
        button = (Button) baseView.findViewById(R.id.fragment_one_translate_button);
        someListView= (ExpandableListView) baseView.findViewById(R.id.one_fragment_exp_list_view);
        return baseView;
      }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

      }

    public void onViewCreated(View view, Bundle savedInstanceState){
        super.onViewCreated(view, savedInstanceState);
        classOneObjects = new TreeMap<String, Object>();
        classOneObjects .put(FRAGMENT, (SherlockFragment) this);
        classOneObjects .put(BUTTON, button);
        classOneObjects .put(LISTVIEW, someListView);
        viewOneCreated.fragmentOneCreated(classOneObjects);
    }
//...
}
于 2013-08-19T17:03:55.843 回答