更改方向时片段膨胀错误

时间:2012-08-03 16:22:20

标签: android crash orientation inflate portrait

好的,这是问题所在,我有一个片段活动,里面有两个片段。一个是ListFragment,另一个是Fragment。所以,我想根据选择的项目显示片段。这是问题所在。如果我在横向模式或纵向模式下启动此片段活动,它可以正常工作。无论如何,当我改变到另一个方向时,它总是崩溃。错误是关于充气器,类似onCreateView,但我查看了其他帖子,其他解决方案,如onConfigurationChanges,onSaveInstances,没有从名称更改为类。所有都有相同的错误。所以,我希望有人帮助我。我确实有布局 - 土地和布局,但仍然不起作用。

这是我的代码。

activity_pontix(陆地和肖像模式)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#CCFF99"
    android:orientation="vertical" >

    <TextView android:id="@+id/Pontix"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_horizontal"
        android:text="@string/pontix"
        android:background="#ffffff"
        android:textColor="#000000"
        android:textSize="40dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <fragment
            android:id="@+id/sideMenu"
            android:name="com.example.pontix.SideMenu"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3" />

        <fragment
            android:id="@+id/content"
            android:name="com.example.pontix.ContentPreview"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

SideMenu.java

public class SideMenu extends ListFragment
{
    OnMenuSelectedListener setSelected;

    public interface OnMenuSelectedListener 
    {
        public void onItemSelected(int position);
    }

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

        int layout = R.layout.side_menu_item;

        setListAdapter(new ArrayAdapter<String>(getActivity(), layout, MenuItem.Items));
    }

    @Override
    public void onStart() 
    {
        super.onStart();

         if (getFragmentManager().findFragmentById(R.id.sideMenu) != null) {
                getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            }
    }

    @Override
    public void onAttach(Activity activity) 
    {
        super.onAttach(activity);

        try 
        {
            setSelected = (OnMenuSelectedListener) activity;
        } 
        catch (ClassCastException e) 
        {
            throw new ClassCastException(activity.toString() + " must implement OnMenuSelectedListener");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) 
    {
        setSelected.onItemSelected(position);

         // Set the item as checked to be highlighted when in two-pane layout
        getListView().setItemChecked(position, true);
    }
}

ContentPreview.java

public class ContentPreview extends Fragment
{
    final static String ARG_POSITION = "position";
    int mCurrentPosition = -1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        if (savedInstanceState != null) 
        {
            mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
        }

        return inflater.inflate(R.layout.content_view, container, false);
    }

    @Override
    public void onStart() 
    {
        super.onStart();

        Bundle args = getArguments();
        if (args != null) 
        {
            updateContentPreview(args.getInt(ARG_POSITION));
        } 
        else if (mCurrentPosition != -1) 
        {
            updateContentPreview(mCurrentPosition);
        }
    }

    public void updateContentPreview(int position) 
    {
        switch(position)
        {
            case 1:
            {
                TextView article = (TextView) getActivity().findViewById(R.id.contentView);
                article.setText("Voce estava na aba Gincana");
                mCurrentPosition = position;
                break;
            }
            case 2:
            {
                TextView article = (TextView) getActivity().findViewById(R.id.contentView);
                article.setText("Voce estava na aba Tarefas");
                mCurrentPosition = position;
                break;
            }
            case 3:
            {
                TextView article = (TextView) getActivity().findViewById(R.id.contentView);
                article.setText("Voce estava na aba Ranking");
                mCurrentPosition = position;
                break;
            }
            case 4:
            {
                TextView article = (TextView) getActivity().findViewById(R.id.contentView);
                article.setText("Voce estava na aba Amigos");
                mCurrentPosition = position;
                break;
            }
            case 5:
            {
                TextView article = (TextView) getActivity().findViewById(R.id.contentView);
                article.setText("Voce estava na aba Perfil");
                mCurrentPosition = position;
                break;
            }
            case 6:
            {
                TextView article = (TextView) getActivity().findViewById(R.id.contentView);
                article.setText("Voce estava na aba Sobre");
                mCurrentPosition = position;
                break;
            }
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) 
    {
        super.onSaveInstanceState(outState);

        outState.putInt(ARG_POSITION, mCurrentPosition);
    }
}

PontixActivity.java

public class PontixActivity extends FragmentActivity implements SideMenu.OnMenuSelectedListener 
{   
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pontix);

        Intent intent = getIntent();
        int code = intent.getIntExtra(GincanaActivity.EXTRA_CONTENT,0);

        SideMenu sideMenu = new SideMenu();

        sideMenu.setArguments(getIntent().getExtras());

        getSupportFragmentManager().beginTransaction().add(R.id.sideMenu, sideMenu).commit();

        ContentPreview contentFragment = (ContentPreview) getSupportFragmentManager().findFragmentById(R.id.content);

        if (contentFragment != null) 
        {
            contentFragment.updateContentPreview(code);

        } 
    }

    public void onItemSelected(int position) 
    {
        switch(position)
        {
            case 0:
            {
                Intent contentView = new Intent(this, GincanaActivity.class);
                startActivity(contentView);
                break;
            }
            case 1:
            {
                Intent contentView = new Intent(this, TarefaActivity.class);
                startActivity(contentView);
                break;
            }
            case 2:
            {
                Intent contentView = new Intent(this, RankingActivity.class);
                startActivity(contentView);
                break;
            }
            case 3:
            {
                Intent contentView = new Intent(this, MainActivity.class);
                startActivity(contentView);
                break;
            }
            case 4:
            {
                Intent contentView = new Intent(this, MainActivity.class);
                startActivity(contentView);
                break;
            }
            case 5:
            {
                Intent contentView = new Intent(this, MainActivity.class);
                startActivity(contentView);
                break;
            }
        }
    }
}

这是错误日志。

08-03 13:13:58.745: W/IInputConnectionWrapper(13847): showStatusIcon on inactive InputConnection
08-03 13:14:09.065: D/AbsListView(13847): Get MotionRecognitionManager
08-03 13:14:09.065: D/AbsListView(13847): Get MotionRecognitionManager
08-03 13:14:12.310: D/AndroidRuntime(13847): Shutting down VM
08-03 13:14:12.310: W/dalvikvm(13847): threadid=1: thread exiting with uncaught exception (group=0x40c661f8)
08-03 13:14:12.340: E/AndroidRuntime(13847): FATAL EXCEPTION: main
08-03 13:14:12.340: E/AndroidRuntime(13847): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pontix/com.example.pontix.PontixActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class fragment
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3365)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.ActivityThread.access$700(ActivityThread.java:128)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1165)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.os.Looper.loop(Looper.java:137)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.ActivityThread.main(ActivityThread.java:4514)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at java.lang.reflect.Method.invokeNative(Native Method)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at java.lang.reflect.Method.invoke(Method.java:511)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at dalvik.system.NativeStart.main(Native Method)
08-03 13:14:12.340: E/AndroidRuntime(13847): Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class fragment
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:280)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.Activity.setContentView(Activity.java:1892)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at com.example.pontix.PontixActivity.onCreate(PontixActivity.java:13)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.Activity.performCreate(Activity.java:4562)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
08-03 13:14:12.340: E/AndroidRuntime(13847):    ... 12 more
08-03 13:14:12.340: E/AndroidRuntime(13847): Caused by: java.lang.IllegalStateException: Fragment com.example.pontix.SideMenu did not create a view.
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:295)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
08-03 13:14:12.340: E/AndroidRuntime(13847):    ... 23 more
08-03 13:14:12.355: D/dalvikvm(13847): GC_CONCURRENT freed 152K, 3% free 13194K/13575K, paused 1ms+3ms
08-03 13:14:12.355: D/AbsListView(13847): [unregisterDoubleTapMotionListener]
08-03 13:14:12.355: I/MotionRecognitionManager(13847):   .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@419278a8
08-03 13:14:12.355: D/AbsListView(13847): [unregisterDoubleTapMotionListener]
08-03 13:14:12.355: I/MotionRecognitionManager(13847):   .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@4192d5e0
08-03 13:14:12.360: D/AbsListView(13847): [unregisterDoubleTapMotionListener]
08-03 13:14:12.360: I/MotionRecognitionManager(13847):   .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@41963500

1 个答案:

答案 0 :(得分:2)

你的两个片段都需要公共的空构造函数:

public class SideMenu extends ListFragment
{
   public SideMenu(){}
   ...

public class ContentPreview extends Fragment
{
   public ContentPreview(){}
   ...