FragmentActivity与ViewPager:崩溃的方向变化方向、FragmentActivity、ViewPager

2023-09-06 11:19:53 作者:山色风月倦

大家好我在FragmentActivity使用ViewPager。我得到一个崩溃时的方向变化。

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_list);
    上下文=这一点;
    loadInitialData();
    setActionBar();
    initializeUiComponents();
}

私人无效initializeUiComponents(){
    //设置的ViewPager与部分适配器。
    mViewPager =(ViewPager)findViewById(R.id.pager);
    }
 

我的数据是动态地从数据库里,我必须调用Sysnctask这是从数据库中获取datat在onResume()。这里是AsyncTask的的onPostExecute()。

 保护无效onPostExecute(无效的结果){

            mSectionsPagerAdapter =新SectionsPagerAdapter(getSupportFragmentManager());
            mViewPager.setAdapter(mSectionsPagerAdapter);
        }
        mSectionsPagerAdapter.notifyDataSetChanged();
        }
}
 

添加FragmentAdapter code:

 公共类SectionsPagerAdapter扩展FragmentPagerAdapter {
    公共SectionsPagerAdapter(FragmentManager FM){
        超(FM);
    }

    @覆盖
    公共片段的getItem(INT位置){
        //的getItem被称为实例化片段给定的页面。
        //返回一个DummySectionFragment(定义为静态内部类
        下图)与页面数量作为其唯一的参数//。
        片段片段=新PayeeListSectionFragment();
        捆绑的args =新包();
        args.putInt(PayeeListSectionFragment.ARG_SECTION_NUMBER,位置+ 1);
        fragment.setArguments(参数);
        返回片段;
    }

    @覆盖
    公众诠释getCount将(){
        返回2;
    }

    @覆盖
    公共CharSequence的getPageTitle(INT位置){

        返回的字符串;
    }
}
 
Android 4 Fragment,ViewPager

添加片段code:

 公共类PayeeListSectionFragment扩展片段{
    / **
     *片段参数重新presenting这个片段的部分号码。
     * /
    公共静态最后弦乐ARG_SECTION_NUMBER =section_number标;

    公共PayeeListSectionFragment(){
    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        查看rootView = NULL;
        \\ rootView膨胀
        返回rootView;
    }
}
 

是所使用的XML是:

 < android.support.v4.view.ViewPager
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID /寻呼机
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:上下文=。Paye​​eListActivity>

    <! - 
这个标题条会显示当前可见的页面标题,以及页面
标题为相邻的页面。

     - >

    < android.support.v4.view.PagerTitleStrip
        机器人:ID =@ + ID / pager_title_strip
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =顶
        机器人:后台=#33b5e5
        机器人:paddingBottom会=4DP
        机器人:paddingTop =4DP
        机器人:文字颜色=#FFF/>
< /android.support.v4.view.ViewPager>
 

大家好!请帮忙!我正在以下崩溃的方向变化:

 十一月六日至7日:01:57.834:E / AndroidRuntime(766):致命异常:主要
11月6号至7号:01:57.834:E / AndroidRuntime(766):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.walletv2.activity / com.walletv2.activity.PayeeListActivity}:android.support.v4.app .Fragment $ InstantiationException:无法实例片段com.walletv2.activity.PayeeListActivity $ PayeeListSectionFragment:确保类名称存在,是公开的,并且有一个空的构造是公
11月6号至7号:01:57.834:E / AndroidRuntime(766):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在android.app.ActivityThread.access $ 700(ActivityThread.java:141)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1240)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在android.os.Handler.dispatchMessage(Handler.java:99)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在android.os.Looper.loop(Looper.java:137)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在android.app.ActivityThread.main(ActivityThread.java:5041)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在java.lang.reflect.Method.invokeNative(本机方法)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在java.lang.reflect.Method.invoke(Method.java:511)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在dalvik.system.NativeStart.main(本机方法)
11月6号至7号:01:57.834:E / AndroidRuntime(766):android.support.v4.app.Fragment $ InstantiationException:产生的原因无法实例片段com.walletv2.activity.PayeeListActivity $ PayeeListSectionFragment:确保类名称存在,是公开的,并且有一个空的构造是公
11月6号至7号:01:57.834:E / AndroidRuntime(766):在android.support.v4.app.Fragment.instantiate(Fragment.java:405)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1767)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:208)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在com.walletv2.activity.PayeeListActivity.onCreate(PayeeListActivity.java:78)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在android.app.Activity.performCreate(Activity.java:5104)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11月6号至7号:01:57.834:E / AndroidRuntime(766):12 ...更多
11月6号至7号:01:57.834:E / AndroidRuntime(766):java.lang.InstantiationException:产生的原因无法实例类com.walletv2.activity.PayeeListActivity $ PayeeListSectionFragment;没有空的构造
11月6号至7号:01:57.834:E / AndroidRuntime(766):在java.lang.Class.newInstanceImpl(本机方法)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在java.lang.Class.newInstance(Class.java:1319)
11月6号至7号:01:57.834:E / AndroidRuntime(766):在android.support.v4.app.Fragment.instantiate(Fragment.java:394)
11月6号至7号:01:57.834:E / AndroidRuntime(766):19 ...更多
 

解决方案

有以下行一对夫妇的提示:

  

产生的原因:android.support.v4.app.Fragment $ InstantiationException:无法实例片段com.walletv2.activity.PayeeListActivity $ PayeeListSectionFragment:确保类名称存在,是公开的,并且有一个空的构造是公共

  

11月六日至七日:01:57.834:E / AndroidRuntime(766):java.lang.InstantiationException:产生的原因无法实例类com.walletv2.activity.PayeeListActivity $ PayeeListSectionFragment; 没有空的构造

这意味着你已经添加了一个参数的构造你的 PayeeListSectionFragment 内部类。不幸的是,这是行不通的,因为Android的依赖调用使用反射非参数的构造函数,以恢复对配置更改的片段的状态(除其他事项外)。

如果您想提供一个或多个参数的片段,你必须将它们插入一个捆绑,并将其设置为参数,使用setArguments(Bundle).看一看在 DetailsFragment CountingFragment 中有关如何做到这一点的例子的文档。

也可能出现另一种情况,这可能是对那些不太熟悉Java不太明显此问题。如果你的片段是一个嵌套类说出一些活动中,一定要声明的内部类静态。例如,在上面的问题的情况下,它应该是一个公共静态类PayeeListSectionFragment (重点放在了静态修改)。这样,内部类不会让一个引用外部类,可以有自己的生命周期,而对外部类的任何相关性。如果没有static修饰符,嵌套类不能没有实例化外部类,这意味着Android将尝试重新实例到您的片段类时遇到问题实例。

这是另一种解决方案是内部类移动到它自己的的.java 文件。这样,任何外部类的依赖关系被自动删除。

Hi guys I have used ViewPager in FragmentActivity. I am getting a crash when orientation changes.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    context = this;
    loadInitialData();
    setActionBar();
    initializeUiComponents();
}

private void initializeUiComponents() {
    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    }

My data is coming dynamically from the database, and I have to call the Sysnctask which is getting the datat from database in onResume(). Here is onPostExecute() of the Asynctask.

protected void onPostExecute(Void result) {

            mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
            mViewPager.setAdapter(mSectionsPagerAdapter);
        }
        mSectionsPagerAdapter.notifyDataSetChanged();
        }
}

Adding FragmentAdapterCode:

public class SectionsPagerAdapter extends FragmentPagerAdapter {
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = new PayeeListSectionFragment();
        Bundle args = new Bundle();
        args.putInt(PayeeListSectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {

        return string;
    }
}

Adding Fragment Code:

public class PayeeListSectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public PayeeListSectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = null;
        \\ rootView inflated
        return rootView;
    }
}

The xml is used is:

 <android.support.v4.view.ViewPager
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PayeeListActivity" >

    <!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.

    -->

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" />
</android.support.v4.view.ViewPager>

Guys! Please help! I am getting the following crash on orientation change:

06-07 11:01:57.834: E/AndroidRuntime(766): FATAL EXCEPTION: main  
06-07 11:01:57.834: E/AndroidRuntime(766): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.walletv2.activity/com.walletv2.activity.PayeeListActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.walletv2.activity.PayeeListActivity$PayeeListSectionFragment: make sure class name exists, is public, and has an empty constructor that is public  
06-07 11:01:57.834: E/AndroidRuntime(766):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-07 11:01:57.834: E/AndroidRuntime(766):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-07 11:01:57.834: E/AndroidRuntime(766):  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
06-07 11:01:57.834: E/AndroidRuntime(766):  at android.app.ActivityThread.access$700(ActivityThread.java:141)
06-07 11:01:57.834: E/AndroidRuntime(766):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
06-07 11:01:57.834: E/AndroidRuntime(766):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 11:01:57.834: E/AndroidRuntime(766):  at android.os.Looper.loop(Looper.java:137)
06-07 11:01:57.834: E/AndroidRuntime(766):  at android.app.ActivityThread.main(ActivityThread.java:5041)
06-07 11:01:57.834: E/AndroidRuntime(766):  at java.lang.reflect.Method.invokeNative(Native Method)
06-07 11:01:57.834: E/AndroidRuntime(766):  at java.lang.reflect.Method.invoke(Method.java:511)
06-07 11:01:57.834: E/AndroidRuntime(766):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-07 11:01:57.834: E/AndroidRuntime(766):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-07 11:01:57.834: E/AndroidRuntime(766):  at dalvik.system.NativeStart.main(Native Method)
06-07 11:01:57.834: E/AndroidRuntime(766): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.walletv2.activity.PayeeListActivity$PayeeListSectionFragment: make sure class name exists, is public, and has an empty constructor that is public
06-07 11:01:57.834: E/AndroidRuntime(766):  at android.support.v4.app.Fragment.instantiate(Fragment.java:405)
06-07 11:01:57.834: E/AndroidRuntime(766):  at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
06-07 11:01:57.834: E/AndroidRuntime(766):  at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1767)
06-07 11:01:57.834: E/AndroidRuntime(766):  at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:208)
06-07 11:01:57.834: E/AndroidRuntime(766):  at com.walletv2.activity.PayeeListActivity.onCreate(PayeeListActivity.java:78)
06-07 11:01:57.834: E/AndroidRuntime(766):  at android.app.Activity.performCreate(Activity.java:5104)
06-07 11:01:57.834: E/AndroidRuntime(766):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-07 11:01:57.834: E/AndroidRuntime(766):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-07 11:01:57.834: E/AndroidRuntime(766):  ... 12 more
06-07 11:01:57.834: E/AndroidRuntime(766): Caused by: java.lang.InstantiationException: can't instantiate class com.walletv2.activity.PayeeListActivity$PayeeListSectionFragment; no empty constructor
06-07 11:01:57.834: E/AndroidRuntime(766):  at java.lang.Class.newInstanceImpl(Native Method)
06-07 11:01:57.834: E/AndroidRuntime(766):  at java.lang.Class.newInstance(Class.java:1319)
06-07 11:01:57.834: E/AndroidRuntime(766):  at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
06-07 11:01:57.834: E/AndroidRuntime(766):  ... 19 more

解决方案

There's a couple of hints on the following lines:

Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.walletv2.activity.PayeeListActivity$PayeeListSectionFragment: make sure class name exists, is public, and has an empty constructor that is public

And:

06-07 11:01:57.834: E/AndroidRuntime(766): Caused by: java.lang.InstantiationException: can't instantiate class com.walletv2.activity.PayeeListActivity$PayeeListSectionFragment; no empty constructor

That suggests that you've added a parameterized constructor to your PayeeListSectionFragment inner class. Unfortunately, that won't work, as Android relies on invoking a non-parameterized constructor using reflection in order to restore a fragment's state on configuration changes (amongst other things).

If you want to supply one or more parameters to a fragment, you'll have to plug them into a Bundle and set it as argument, using setArguments(Bundle). Have a look at the DetailsFragment and CountingFragment in the documentation for an example on how to do that.

This problem can also arise in another situation, which may be less obvious for those not too familiar with Java. If your fragment is a nested class of say some Activity, make sure you declare the inner class static. For example, in the context of above question, it should be a public static class PayeeListSectionFragment (with an emphasis on the static modifier). That way the inner class won't keep a reference to the outer class and can have its own life cycle, without any dependencies on the outer class. Without the static modifier, the nested class can't be instantiated without instantiating the outer class, which means Android will run into issues when attempting to reinstantiate your fragment class.

An alternative solution would be to move the inner class to its own .java file. That way any outer class dependencies are automatically removed.