设备旋转时片段未加载横向布局,还原为先前的片段

时间:2015-03-08 20:42:12

标签: android android-layout android-fragments fragment

我遇到了一个没有调用自身景观布局的片段的问题。当您旋转设备时,它会恢复到之前的片段并将其放入logcat:

细分:

我在片段A中,按下一个按钮并被带到片段B.当我旋转设备时,片段A以横向视图显示。

  

requestLayout()由android.widget.ListView {43a67d00 VFED.VC.不正确地调用。 ...... ID 0,0-720,1845}布局期间:运行第二次布局传递

     

E / ViewRootImpl:sendUserActionEvent()mView == null

如果设备已经旋转并且然后选择了片段,那么横向布局会很好地加载,就在你进入片段本身的时候。

这是片段的代码,希望你能帮忙!

public class nandos_Fragment extends Fragment implements View.OnClickListener {
    View rootview;
    MapView mMapView;
    private GoogleMap googleMap;
    public static Button scanBtn;
    public static int retrievedResult = 0;
    private static final String TAG = "MyActivity";
    @Nullable
    @Override

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

    rootview = inflater.inflate(R.layout.nandos_layout, container, false);
    scanBtn = (Button) rootview.findViewById(R.id.scan_button);
    scanBtn.setOnClickListener(this);
    mMapView = (MapView) rootview.findViewById(R.id.mapView);
    mMapView.onCreate(savedInstanceState);
    mMapView.onResume();// needed to get the map to display immediately
    try {
        MapsInitializer.initialize(getActivity().getApplicationContext());
    } catch (Exception e) {
        e.printStackTrace();
    }
    googleMap = mMapView.getMap();
    // latitude and longitude
    double latitude = 52.955491;
    double longitude = -1.149885;

    // create marker
    MarkerOptions marker = new MarkerOptions().position(
            new LatLng(latitude, longitude)).title("Nandos Nottingham!");

    // Changing marker icon
    marker.icon(BitmapDescriptorFactory
            .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

    // adding marker
    googleMap.addMarker(marker);
    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(new LatLng(52.955491, -1.149885)).zoom(15).build();
    googleMap.animateCamera(CameraUpdateFactory
            .newCameraPosition(cameraPosition));
    return rootview;
}

public void onClick(View v) {
    //respond to clicks
    if (v.getId() == R.id.scan_button) {
        //scan
        if (retrievedResult == 0) {
            IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity());
            scanIntegrator.initiateScan();
        }else{}

    }
}
@Override
public void onResume() {
    super.onResume();
    mMapView.onResume();
    Random r = new Random();
    int i1 = r.nextInt(80 - 65) + 65;
    if (result != null) {
        scanBtn.setText("Code: " + result + i1);
        retrievedResult = 1;
    }

}

@Override
public void onPause() {
    super.onPause();
    mMapView.onPause();
    scanBtn.setText("Get Offer!");
    retrievedResult = 0;
    result = null;
}

@Override
public void onDestroy() {
    Log.v(TAG, "ON DESTROY");
    mMapView.onDestroy();
    super.onDestroy();
}

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.ntuio" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="com.ram.googlemapsv2.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/ntsu"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".HomeActivity"
        android:label="@string/title_activity_nav_bar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="****"/>

</application>

</manifest>

1 个答案:

答案 0 :(得分:-1)

android:configChanges="keyboardHidden|orientation|screenSize"之后添加
android:label="@string/title_activity_nav_bar"


这样可以防止旋转设备后刷新。