使用片段在横向和纵向模式之间切换

时间:2014-09-22 06:36:16

标签: android android-fragments android-orientation

我有一个非常简单的应用程序,可以在横向模式和纵向模式之间切换。在纵向模式下,我有一个片段,以列表和横向模式显示数据我有另一个片段,在网格中显示我的数据。只是为了重新迭代,有两个不同的片段,一个以纵向模式呈现数据的ListFragment和一个' GridFragment'以横向模式显示数据。只要设备方向未更改,各个片段就会正确呈现。然而,当设备从纵向翻转到横向时,应用程序崩溃,反之亦然。

首先是我的错误日志。当设备从纵向模式翻转为横向时的错误日志:

  

09-22 11:48:38.990:E / AndroidRuntime(29737):致命异乎寻常:主要   09-22 11:48:38.990:E / AndroidRuntime(29737):java.lang.RuntimeException:无法启动活动ComponentInfo {com.funapp.ssnd / com.funapp.ssnd.MainActivity}:java.lang.RuntimeException:你的content必须有一个ListView,其id属性为' android.R.id.list'   09-22 11:48:38.990:E / AndroidRuntime(29737):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)   09-22 11:48:38.990:E / AndroidRuntime(29737):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)   09-22 11:48:38.990:E / AndroidRuntime(29737):在android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3677)   09-22 11:48:38.990:E / AndroidRuntime(29737):在android.app.ActivityThread.access $ 700(ActivityThread.java:149)   09-22 11:48:38.990:E / AndroidRuntime(29737):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1306)   09-22 11:48:38.990:E / AndroidRuntime(29737):在android.os.Handler.dispatchMessage(Handler.java:99)   09-22 11:48:38.990:E / AndroidRuntime(29737):在android.os.Looper.loop(Looper.java:153)   09-22 11:48:38.990:E / AndroidRuntime(29737):在android.app.ActivityThread.main(ActivityThread.java:4987)   09-22 11:48:38.990:E / AndroidRuntime(29737):at java.lang.reflect.Method.invokeNative(Native Method)   09-22 11:48:38.990:E / AndroidRuntime(29737):at java.lang.reflect.Method.invoke(Method.java:511)   09-22 11:48:38.990:E / AndroidRuntime(29737):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:821)   09-22 11:48:38.990:E / AndroidRuntime(29737):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)   09-22 11:48:38.990:E / AndroidRuntime(29737):at dalvik.system.NativeStart.main(Native Method)   09-22 11:48:38.990:E / AndroidRuntime(29737):引起:java.lang.RuntimeException:您的内容必须有一个ListView,其id属性为' android.R.id.list'   09-22 11:48:38.990:E / AndroidRuntime(29737):在android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)   09-22 11:48:38.990:E / AndroidRuntime(29737):在android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)   09-22 11:48:38.990:E / AndroidRuntime(29737):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:956)   09-22 11:48:38.990:E / AndroidRuntime(29737):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)   09-22 11:48:38.990:E / AndroidRuntime(29737):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1103)   09-22 11:48:38.990:E / AndroidRuntime(29737):在android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1901)   09-22 11:48:38.990:E / AndroidRuntime(29737):在android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:567)   09-22 11:48:38.990:E / AndroidRuntime(29737):在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1166)   09-22 11:48:38.990:E / AndroidRuntime(29737):在android.app.Activity.performStart(Activity.java:5030)   09-22 11:48:38.990:E / AndroidRuntime(29737):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)   09-22 11:48:38.990:E / AndroidRuntime(29737):... 12更多

设备从横向更改为纵向时的错误日志:

  

09-22 11:52:20.906:E / AndroidRuntime(29943):致命异常:主要   09-22 11:52:20.906:E / AndroidRuntime(29943):java.lang.RuntimeException:无法启动活动ComponentInfo {com.funapp.ssnd / com.funapp.ssnd.MainActivity}:java.lang.NullPointerException   09-22 11:52:20.906:E / AndroidRuntime(29943):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)   09-22 11:52:20.906:E / AndroidRuntime(29943):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)   09-22 11:52:20.906:E / AndroidRuntime(29943):在android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3677)   09-22 11:52:20.906:E / AndroidRuntime(29943):在android.app.ActivityThread.access $ 700(ActivityThread.java:149)   09-22 11:52:20.906:E / AndroidRuntime(29943):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1306)   09-22 11:52:20.906:E / AndroidRuntime(29943):在android.os.Handler.dispatchMessage(Handler.java:99)   09-22 11:52:20.906:E / AndroidRuntime(29943):在android.os.Looper.loop(Looper.java:153)   09-22 11:52:20.906:E / AndroidRuntime(29943):在android.app.ActivityThread.main(ActivityThread.java:4987)   09-22 11:52:20.906:E / AndroidRuntime(29943):at java.lang.reflect.Method.invokeNative(Native Method)   09-22 11:52:20.906:E / AndroidRuntime(29943):at java.lang.reflect.Method.invoke(Method.java:511)   09-22 11:52:20.906:E / AndroidRuntime(29943):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:821)   09-22 11:52:20.906:E / AndroidRuntime(29943):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)   09-22 11:52:20.906:E / AndroidRuntime(29943):at dalvik.system.NativeStart.main(Native Method)   09-22 11:52:20.906:E / AndroidRuntime(29943):引起:java.lang.NullPointerException   09-22 11:52:20.906:E / AndroidRuntime(29943):at com.funapp.ssnd.DetailsFragment.onActivityCreated(DetailsFragment.java:59)   09-22 11:52:20.906:E / AndroidRuntime(29943):在android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1512)   09-22 11:52:20.906:E / AndroidRuntime(29943):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:962)   09-22 11:52:20.906:E / AndroidRuntime(29943):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)   09-22 11:52:20.906:E / AndroidRuntime(29943):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1103)   09-22 11:52:20.906:E / AndroidRuntime(29943):在android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1901)   09-22 11:52:20.906:E / AndroidRuntime(29943):在android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:567)   09-22 11:52:20.906:E / AndroidRuntime(29943):在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1166)   09-22 11:52:20.906:E / AndroidRuntime(29943):在android.app.Activity.performStart(Activity.java:5030)   09-22 11:52:20.906:E / AndroidRuntime(29943):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)   09-22 11:52:20.906:E / AndroidRuntime(29943):... 12更多

包含碎片的活动:

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends FragmentActivity {

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

     Configuration config = getResources().getConfiguration();

     if(config.orientation == Configuration.ORIENTATION_PORTRAIT){

         setContentView(R.layout.activity_main);
         FragmentManager manager = getSupportFragmentManager();
         FragmentTransaction transaction = manager.beginTransaction();
         MemberListFragment fragment1 = new MemberListFragment();
         transaction.add(R.id.fragment_container,fragment1);
         transaction.commit();
     }

     if(config.orientation == Configuration.ORIENTATION_LANDSCAPE){

         setContentView(R.layout.activity_main);
         FragmentManager manager = getSupportFragmentManager();
         FragmentTransaction transaction = manager.beginTransaction();
         DetailsFragment fragment2 = new DetailsFragment();
         transaction.add(R.id.fragment_container,fragment2);
         transaction.commit();
     }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

还有布局文件。对于肖像模式:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView android:id="@id/android:list"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:drawSelectorOnTop="false"/>



</LinearLayout>

对于横向模式:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"

/>

碎片。对于肖像模式:

import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MemberListFragment extends ListFragment {

String[] membersList;
Integer[] imageList;
ListView listView;
List<RowItem> rowItems;

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

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

}


@Override
public void onActivityCreated(Bundle savedInstanceState){

    super.onActivityCreated(savedInstanceState);


    rowItems = new ArrayList<RowItem>();

    membersList = (getResources().getStringArray(R.array.snsd_members));
    imageList = new Integer[membersList.length];

    for(int i=0; i<membersList.length; i++){

        imageList[i] = R.drawable.home;
    }

    for(int i=0; i<membersList.length; i++){

        RowItem item = new RowItem(imageList[i], membersList[i]);
        rowItems.add(item);
    }

    listView = (ListView) getListView();
    SNSDAdapter adapter = new SNSDAdapter(getActivity().getApplicationContext(),
            R.layout.list_element, rowItems);
    listView.setAdapter(adapter);

}



}

横向模式:

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

public class DetailsFragment extends Fragment {

String[] membersList;
Integer[] imageList;
GridView gridView;
List<RowItem> rowItems;

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

    View view = inflater.inflate(R.layout.fragment_layout, container,false);

    gridView = (GridView)view.findViewById(R.id.grid_view);

    return view;

}  

@Override
public void onActivityCreated(Bundle savedInstanceState){

    super.onActivityCreated(savedInstanceState);


    rowItems = new ArrayList<RowItem>();

    membersList = (getResources().getStringArray(R.array.snsd_members));
    imageList = new Integer[membersList.length];

    for(int i=0; i<membersList.length; i++){

        imageList[i] = R.drawable.home;
    }

    for(int i=0; i<membersList.length; i++){

        RowItem item = new RowItem(imageList[i], membersList[i]);
        rowItems.add(item);
    }

    Log.v("ROW ITEMS",rowItems.toString());
    GridAdapter adapter = new GridAdapter(getActivity().getApplicationContext(),rowItems);
    gridView.setAdapter(adapter);

}




}

我花了很多时间试图解决这个问题而没有找到解决方案。这可能是因为我的碎片并没有被真正“摧毁”。屏幕旋转时防止新片段取代它?任何解决方案或某些方向将非常感激。

1 个答案:

答案 0 :(得分:0)

检查XML文件中的ListView ID ... 它应该是android:id="@android:id/list"