如何在Android中的Activity中使用getSupportFragment

时间:2017-01-08 19:16:05

标签: android android-fragments android-preferences

我想在public static Bitmap loadBitmapFromView(ScrollView v) { Bitmap b = Bitmap.createBitmap(v.getWidth() , v.getChildAt(0).getHeight(), Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); v.draw(c); return b; } 中使用Fragment,对于此问题,我想使用activity
我在getSupportFragment中编写了以下代码,用于显示activity

fragment

我的SettingFragment代码:

android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
SettingsFragment settingsFragment = new SettingsFragment();
fragmentTransaction.add(R.id.frame_containerone, settingsFragment, "Setting");
fragmentTransaction.commit();

但是当使用import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import android.util.Log; import in.nouri.cameraalways.R; import in.nouri.cameraalways.Service.OverlayService; public class SettingsFragment extends PreferenceFragment { SharedPreferences.OnSharedPreferenceChangeListener listener = null; SharedPreferences defaultSharedPreference; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { defaultSharedPreference = PreferenceManager.getDefaultSharedPreferences(getContext()); } else { defaultSharedPreference = PreferenceManager.getDefaultSharedPreferences(getActivity()); } Log.d("defaultpPref", defaultSharedPreference.getAll().toString()); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onAttach(Activity activity) { super.onAttach(activity); listener = (SharedPreferences.OnSharedPreferenceChangeListener) activity; } @TargetApi(Build.VERSION_CODES.M) @Override public void onAttach(Context context) { super.onAttach(context); listener = (SharedPreferences.OnSharedPreferenceChangeListener) context; } @Override public void onDetach() { super.onDetach(); listener = null; } @Override public void onResume() { Log.d(getClass().getSimpleName(), "onResume"); super.onResume(); getPreferenceScreen().getSharedPreferences() .registerOnSharedPreferenceChangeListener(listener); CheckBoxPreference preference_enabled = (CheckBoxPreference) this.findPreference(getString(R.string.key_pref_enabled)); preference_enabled.setChecked(OverlayService.isRunning()); } @Override public void onPause() { super.onPause(); getPreferenceScreen().getSharedPreferences() .unregisterOnSharedPreferenceChangeListener(listener); } } 中的上述代码(用于活动中的片段)时,它会向我显示以下消息:

Please see image

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您的SettingsFragment是android.app.Fragment类的子类,而不是android.support.v4.app.Fragment。更改片段中的import语句,你就可以了。

<强>更新 而不是import android.preference.PreferenceFragment; 在“设置”片段中使用import android.support.v7.preference.PreferenceFragmentCompat;。 请注意,它需要compile 'com.android.support:preference-v7:24.2.1' 在你的app.gradle文件中

答案 1 :(得分:0)

在SettingsFragment中创建SettingsFragment的实例:

initialize

将其称为您的活动,如下所示: public static SettingsFragment getInstance() { return new SettingsFragment (); }