选项卡片段滑动时Android应用程序意外关闭

时间:2015-04-29 07:10:54

标签: android android-fragments

我有一个包含三个片段的片段活动 1)DayFragment 2)WeekFragment 3)MonthFragment 当我滑动第一个片段应用程序关闭并给出异常 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)

RepeatRemainderFragmentActivity.java是我的片段活动

public class RepeatRemainderFragmentActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "Day", "Week", "Month" };

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

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

}

Dayfragment.java

public class DayFragment extends Fragment implements OnClickListener, OnItemSelectedListener{

    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_day, container, false);

        InitializeUI();
        setListeners();
        setCurrentDate();
        dayRepeatSpinner();

        return rootView;
    }

    private void setCurrentDate() {
        // TODO Auto-generated method stub

            Calendar c = Calendar.getInstance();
            SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            String formattedDate = df.format(c.getTime());
            day_startsOn_tv.setText(formattedDate);
    }
    .
    .
    .

WeekFragment.java

public class WeekFragment extends Fragment implements OnClickListener, OnItemSelectedListener{

    private View rootView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_week, container, false);

        InitializeUI();
        setListeners();
        setCurrentDate();
        weekRepeatSpinner();

        return rootView;
    }

    private void setCurrentDate() {
        // TODO Auto-generated method stub

            Calendar c = Calendar.getInstance();
            SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            String formattedDate = df.format(c.getTime());
            week_startsOn_tv.setText(formattedDate);

    }

    private void setListeners() {
        // TODO Auto-generated method stub
        week_startsOn_rl.setOnClickListener(this);
        week_repeatEvery_rl.setOnClickListener(this);
        week_ends_rl.setOnClickListener(this);
        weekSpinner.setOnItemSelectedListener(this);
        sun_tv.setOnClickListener(this);
        mon_tv.setOnClickListener(this);
        tue_tv.setOnClickListener(this);
        wed_tv.setOnClickListener(this);
        thu_tv.setOnClickListener(this);
        fri_tv.setOnClickListener(this);
        sat_tv.setOnClickListener(this);
    }

    .
    .
    .

MonthFragment.java

public class MonthFragment extends Fragment implements OnClickListener, OnItemSelectedListener{

    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_month, container, false);

        InitializeUI();
        setListeners();
        setCurrentDate();
        monthRepeatSpinner();

        return rootView;
    }

    private void setCurrentDate() {
        // TODO Auto-generated method stub

            Calendar c = Calendar.getInstance();
            SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            String formattedDate = df.format(c.getTime());
            month_startsOn_tv.setText(formattedDate);// showing exception inthis line

    }

    private void setListeners() {
        // TODO Auto-generated method stub
        month_startsOn_rl.setOnClickListener(this);
        month_repeatEvery_rl.setOnClickListener(this);
        month_repearBy_rl.setOnClickListener(this);
        month_ends_rl.setOnClickListener(this);
        monthSpinner.setOnItemSelectedListener(this);
    }

private void InitializeUI() {
  // TODO Auto-generated method stub
    month_startsOn_rl = (RelativeLayout) rootView.findViewById(R.id.month_startOn_relativeLayout);
    month_repeatEvery_rl = (RelativeLayout) rootView.findViewById(R.id.month_repeat_relativeLayout);
    month_repearBy_rl = (RelativeLayout) rootView.findViewById(R.id.month_repeatBy_relativeLayout);
    month_ends_rl = (RelativeLayout) rootView.findViewById(R.id.month_ends_relativeLayout);

    monthSpinner = (Spinner) rootView.findViewById(R.id.month_spinner);

}

以下是我的logcat

04-29 02:27:14.366: E/AndroidRuntime(1499): FATAL EXCEPTION: main
04-29 02:27:14.366: E/AndroidRuntime(1499): Process: com.ielts.notes, PID: 1499
04-29 02:27:14.366: E/AndroidRuntime(1499): java.lang.NullPointerException
04-29 02:27:14.366: E/AndroidRuntime(1499):     at com.ielts.notes.tabswipe.MonthFragment.setCurrentDate(MonthFragment.java:54)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at com.ielts.notes.tabswipe.MonthFragment.onCreateView(MonthFragment.java:42)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:488)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at android.support.v4.view.ViewPager$3.run(ViewPager.java:249)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at android.view.Choreographer.doFrame(Choreographer.java:543)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at android.os.Handler.handleCallback(Handler.java:733)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at android.os.Looper.loop(Looper.java:136)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at java.lang.reflect.Method.invokeNative(Native Method)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at java.lang.reflect.Method.invoke(Method.java:515)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-29 02:27:14.366: E/AndroidRuntime(1499):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

month_startsOn_tv在哪里定义?我认为错误就像是

TextView month_startsOn_tv = (TextView)findViewById(R.id.month_startsOn_tv);
缺少

,因为错误消息是针对null元素的,并且当未定义此textView时,它为null,并且您无法设置其文本。 要在任何地方使用它,您应该像private View rootView; private TextView month_startsOn_tv;

一样在顶部对其进行初始化