无法暂停活动。 NPE

时间:2015-01-14 07:49:18

标签: java android actionbarsherlock

当我使用主页按钮或切换到横向时,我会捕获运行时异常:无法暂停活动。 NullPointerException。请帮助。

  

01-14 09:23:49.435:E / AndroidRuntime(331):致命异常:主01-14   09:23:49.435:E / AndroidRuntime(331):java.lang.RuntimeException:   无法暂停活动   {com.example.actiontest / com.example.actiontest.MainActivity}:   java.lang.NullPointerException 01-14 09:23:49.435:   E / AndroidRuntime(331):at   android.app.ActivityThread.performPauseActivity(ActivityThread.java:2354)   01-14 09:23:49.435:E / AndroidRuntime(331):at   android.app.ActivityThread.performPauseActivity(ActivityThread.java:2311)   01-14 09:23:49.435:E / AndroidRuntime(331):at   android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2291)   01-14 09:23:49.435:E / AndroidRuntime(331):at   android.app.ActivityThread.access $ 1700(ActivityThread.java:117)01-14   09:23:49.435:E / AndroidRuntime(331):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:938)   01-14 09:23:49.435:E / AndroidRuntime(331):at   android.os.Handler.dispatchMessage(Handler.java:99)01-14   09:23:49.435:E / AndroidRuntime(331):at   android.os.Looper.loop(Looper.java:123)01-14 09:23:49.435:   E / AndroidRuntime(331):at   android.app.ActivityThread.main(ActivityThread.java:3683)01-14   09:23:49.435:E / AndroidRuntime(331):at   java.lang.reflect.Method.invokeNative(Native Method)01-14   09:23:49.435:E / AndroidRuntime(331):at   java.lang.reflect.Method.invoke(Method.java:507)01-14 09:23:49.435:   E / AndroidRuntime(331):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)   01-14 09:23:49.435:E / AndroidRuntime(331):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)01-14   09:23:49.435:E / AndroidRuntime(331):at   dalvik.system.NativeStart.main(Native Method)01-14 09:23:49.435:   E / AndroidRuntime(331):引起:java.lang.NullPointerException 01-14   09:23:49.435:E / AndroidRuntime(331):at   android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)   01-14 09:23:49.435:E / AndroidRuntime(331):at   android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)   01-14 09:23:49.435:E / AndroidRuntime(331):at   android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)   01-14 09:23:49.435:E / AndroidRuntime(331):at   com.actionbarsherlock.app.SherlockFragmentActivity.onSaveInstanceState(SherlockFragmentActivity.java:126)   01-14 09:23:49.435:E / AndroidRuntime(331):at   android.app.Activity.performSaveInstanceState(Activity.java:1037)   01-14 09:23:49.435:E / AndroidRuntime(331):at   android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1181)   01-14 09:23:49.435:E / AndroidRuntime(331):at   android.app.ActivityThread.performPauseActivity(ActivityThread.java:2336)   01-14 09:23:49.435:E / AndroidRuntime(331):... 12 more

MainActivity.java

package com.example.actiontest;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;

public class MainActivity extends    SherlockFragmentActivity 
                          implements ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "Top Rated", "Games", "Movies" };

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

        // Initilization
        actionBar = getSupportActionBar();
        viewPager = (ViewPager) findViewById(R.id.pager);

        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        //actionBar.setHomeButtonEnabled(false);
        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 transaction) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction transaction) {
        //mSelected.setText("Selected: " + tab.getText());
        viewPager.setCurrentItem(tab.getPosition());
    }

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

TabsPagerAdapter.java

package com.example.actiontest;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
            // Top Rated fragment activity
            return new TopRatedFragment();
        case 1:
             // Top Rated fragment activity
            return new TopRatedFragment();
        case 2:
            // Top Rated fragment activity
            return new TopRatedFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 3;
    }

}

TopRatedFragment.java

package com.example.actiontest;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.actionbarsherlock.app.SherlockFragment;

public class TopRatedFragment extends SherlockFragment {

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

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

        return rootView;
    }
}

2 个答案:

答案 0 :(得分:1)

切换到FragmentStatePagerAdapter不是一个正确的解决方案。 Android docs明确指出

  当号码时,

FragmentStatePagerAdapter 会更有用   页面,更像列表视图。当页面不可见时   用户,他们的整个片段可能会被破坏,只保留   保存了该片段的状态。这允许寻呼机保持很多   与每个访问页面相关联的内存较少    FragmentPagerAdapter ,代价可能是更多开销时   在页面之间切换。

因为使用FragmentPagerAdapter只有三页是正确的。

您的问题很可能源于使用旧版v4支持库。要测试是否属于这种情况,只需将TopRatedFragment#onSaveInstanceState()方法覆盖为

即可
@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    setUserVisibleHint(true);
}

如果这样可以解决您的问题,那么v4支持库版本在FragmentManagerImpl#saveFragmentBasicState()

出现以下错误
if (!f.mUserVisibleHint) {
    /*
    if (result == null) {
        result = new Bundle(); MISSING!
    }*/
    // Only add this if it's not the default value
    result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint);
}

result未初始化时抛出NPE,但仍然会调用putBoolean()

答案 1 :(得分:0)

TabsPagerAdapter延长FragmentStatePagerAdapter代替FragmentPagerAdapter,你就可以了。