我正在解决一个问题,即在屏幕轮换时修复活动重启,我正在实施the 2nd part of this answer,它在setContentView(R.layout.fragment_introduction)
中给出了错误。
方法setContentView(int)未定义类型IntroductionFragment。
public class IntroductionFragment extends Fragment {
public IntroductionFragment(){}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_introduction,container,false);
return rootView;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.fragment_introduction);
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.indianconstitution"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:configChanges="keyboardHidden|orientation|screenSize"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.indianconstitution.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
如何解决此错误?
提前致谢
答案 0 :(得分:1)
setContentView
是Activity
的一种方法。你在Fragment
里面。片段的View层次结构基于onCreateView
答案 1 :(得分:1)
您的onConfigurationChanged()
方法应如下所示:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.fragment_introduction, null);
ViewGroup rootView = (ViewGroup) getView();
rootView.removeAllViews();
rootView.addView(view);
}