使用ViewPager / Fragments / ViewPagerExtensions强制关闭方向更改

时间:2013-01-23 22:00:06

标签: android android-viewpager fragment fragmentpageradapter

我遇到了一些我似乎无法解决的问题(我之前没有以这种方式处理过碎片)

我有一个主要的FragmentActivity如下:

package com.my.app;

import android.app.AlertDialog;
import android.content.DialogInterface; 
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

import com.astuetz.viewpager.extensions.ScrollingTabsView;
import com.astuetz.viewpager.extensions.TabsAdapter;

public class MyTabsActivity extends FragmentActivity {

private ViewPager mPager;
private ScrollingTabsView mScrollingTabs;

private TabsAdapter mScrollingTabsAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_tabs);

    mPager = (ViewPager) findViewById(R.id.pager);
    mScrollingTabs = (ScrollingTabsView) findViewById(R.id.my_tabs);
    mScrollingTabsAdapter = new MyTabsAdapter(this, mPager);
    addTabs();
    mScrollingTabs.setAdapter(mScrollingTabsAdapter);
    mScrollingTabs.setViewPager(mPager);
    mPager.setCurrentItem(1);
    mPager.setPageMargin(1);
}

private void addTabs() {
    mScrollingTabsAdapter.addTab("FirstFragment", "First");
    mScrollingTabsAdapter.addTab("SecondFragment", "Second");
}
}

XML如下(使用Viewpager和ScrollingTabsView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.my.package"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<com.astuetz.viewpager.extensions.ScrollingTabsView
    android:id="@+id/ztc_tabs"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/tab_unselected_holo"
    app:dividerDrawable="@drawable/divider"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/ztc_tabs" />

</RelativeLayout>

然后在MyTabsAdapter中我有这个:

package com.my.app;

import com.my.app.R;

import java.util.ArrayList;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import com.astuetz.viewpager.extensions.TabsAdapter;

public class MyTabsAdapter extends FragmentPagerAdapter implements TabsAdapter {
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private final ViewPager mPager;

private Activity mContext;

static final class TabInfo {
    private final String fragName;
    private final String title;

    TabInfo(String fragName, String title) {
        this.fragName = fragName;
        this.title = title;
    }
}

public MyTabsAdapter(FragmentActivity ctx, ViewPager pager) {
    super(ctx.getSupportFragmentManager());
    this.mContext = ctx;
    this.mPager = pager;
    this.mPager.setAdapter(this);
}

public View getView(int position) {
    Button tab;

    LayoutInflater inflater = mContext.getLayoutInflater();
    tab = (Button) inflater.inflate(R.layout.tab_scrolling, null);

    tab.setText(this.mTabs.get(position).title.toUpperCase());

    return tab;
}

public void addTab(String fragName, String title) {
    TabInfo info = new TabInfo(fragName, title);
    mTabs.add(info);
}

@Override
public Fragment getItem(int position) {
    TabInfo info = mTabs.get(position);
    if (info.fragName == "FirstFragment") {
        return FirstFragment.newInstance();
    } else if (info.fragName == "SecondFragment") {
        return SecondFragment.newInstance(t);
    }
    return null;
}

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

}

FirstFragment / SecondFragment有自己的基本布局,几乎完全相同,这是片段

package com.my.app;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FirstFragment extends Fragment {

public static Fragment newInstance() {
    FirstFragment frag = new FirstFragment();;
    return frag;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
    if (container == null)
        return null;
    return inflater.inflate(R.layout.fragment_first, container, false);
}

}

这里的一切都完全符合我的要求。选项卡正常工作,ViewPager页面正常,等等。

问题是,当我改变方向时,我得到这个痕迹并强制关闭,我不知道为什么。

E/AndroidRuntime(32431): FATAL EXCEPTION: main
E/AndroidRuntime(32431): java.lang.NullPointerException
E/AndroidRuntime(32431):    at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
E/AndroidRuntime(32431):    at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
E/AndroidRuntime(32431):    at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
E/AndroidRuntime(32431):    at android.app.Activity.performSaveInstanceState(Activity.java:1147)
E/AndroidRuntime(32431):    at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1216)
E/AndroidRuntime(32431):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3666)
E/AndroidRuntime(32431):    at android.app.ActivityThread.access$700(ActivityThread.java:141)
E/AndroidRuntime(32431):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
E/AndroidRuntime(32431):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(32431):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(32431):    at android.app.ActivityThread.main(ActivityThread.java:5039)
E/AndroidRuntime(32431):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(32431):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(32431):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(32431):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(32431):    at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(  418):   Force finishing activity com.ada.ztc/.ZTCTabs

所以我需要帮助的是弄清楚为什么我会在方向改变时得到错误。我做了一些研究,似乎没有找到任何有类似设置的人。

任何帮助将不胜感激,如果需要,我很乐意提供更多信息。

2 个答案:

答案 0 :(得分:2)

我似乎通过添加

解决了这个问题
android:configChanges="orientation|screenSize"

在FragmentActivity的清单中

答案 1 :(得分:2)

@Override
        public void onSaveInstanceState(Bundle outState) {
            outState.putString("BUGFIX", "BUGFIX");
            super.onSaveInstanceState(outState);
        }

试试这个。捆绑永远不会是空的