Android PreferenceFragment 已加载且没有错误,但屏幕为空白
Android PreferenceFragment loaded with no errors but screen is blank
我正在尝试实现一个从 AppCompatActivity(或常规 Activity)加载的 PreferenceFragment,我似乎已经让它进入偏好片段的 onCreate 方法,但是屏幕显示空白。下面是一张图片:
我感觉片段在布局后面加载,但我不知道如何检查它,因为即使我 运行 它在空 Activity 上我也会得到类似的行为使用单个 FrameLayout 进行布局。我正在尝试使用 onOptionSelected() 从我的 Main 中显示一个设置页面。
我已经坚持了大约一个星期,none 我找到的答案很有帮助,我们将不胜感激!
这是我的 Activity 代码:
public class genericActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(Main.appTAG, "onCreate: settings activity");
setContentView(R.layout.activity_generic);
// IF intent message == settings
final Fragment settingsFragment = new SettingsFragment();
getFragmentManager()
.beginTransaction()
.replace(R.id.fragmentParent, settingsFragment)
.addToBackStack(null)
.commit();
getFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
getFragmentManager()
.beginTransaction()
.remove(settingsFragment)
.commit();
}
});
}
}
Activity的布局,即activity_generic.xml如下
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.thesis.deefunkt.thesispractise.genericActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/fragmentParent"
android:fitsSystemWindows="true">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list">
</ListView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
我真的不需要兼容性功能,因为这个屏幕所做的只是设置,但为了保持一致的外观而保留了它。
我的SettingsFragment代码如下:
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
Log.d(Main.appTAG, "onCreate: SettingsFragment ");
}
}
虽然这是 运行ning,但我有一堆我不知道如何解释的日志条目:
D/com.deefunkt.UsbComms: onCreate: settings activity
D/PhoneWindow: *FMB* installDecor mIsFloating : false
D/PhoneWindow: *FMB* installDecor flags : -2139029248
D/AbsListView: Get MotionRecognitionManager
D/Activity: performCreate Call Injection manager
W/Resources: Converting to string: TypedValue{t=0x10/d=0x2580 a=-1}
W/Resources: Converting to string: TypedValue{t=0x10/d=0x8 a=-1}
W/Resources: Converting to string: TypedValue{t=0x10/d=0x1 a=-1}
W/Resources: Converting to string: TypedValue{t=0x10/d=0x0 a=-1}
D/com.deefunkt.UsbComms: onCreate: SettingsFragment
I/InjectionManager: dispatchPreferences > Target : com.thesis.deefunkt.thesispractise.SettingsFragment
D/AbsListView: Get MotionRecognitionManager
I/InjectionManager: dispatchOnViewCreated > Target : com.thesis.deefunkt.thesispractise.SettingsFragment isFragment :true
I/InjectionManager: dispatchOnViewCreated > Target : com.thesis.deefunkt.thesispractise.genericActivity isFragment :false
D/SecWifiDisplayUtil: Metadata value : SecSettings2
D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
D/SRIB_DCS: log_dcs ThreadedRenderer::initialize entered!
D/mali_winsys: new_window_surface returns 0x3000, [1440x2560]-format:1
D/OpenGLRenderer: endAllStagingAnimators on 0x7f65c06600 (RippleDrawable) with handle 0x7f74cff540
如您所见,调用了片段内的 onCreate 方法,但我不知道如何处理此日志数据。
提前致谢!
好吧,在这上面花了这么多时间,我终于设法修复了这个错误,但我不知道为什么。我最好的猜测是
getFragmentManager()
.beginTransaction()
.replace(R.id.fragmentParent, settingsFragment)
/*Adding to the backStack here and then setting a backStack Listener
*on the next step made it add the fragment then immediately take it
*off, giving my empty activity layout.*/
.addToBackStack(null)
.commit();
getFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
getFragmentManager()
.beginTransaction()
.remove(settingsFragment)
.commit();
}
});
我删除了对 addToBackStack() 的调用并注释掉了 backStack 侦听器,因为我意识到在 activity 加载后我不需要删除该片段并且只真正关心重用 activity 用于多个片段。
我正在尝试实现一个从 AppCompatActivity(或常规 Activity)加载的 PreferenceFragment,我似乎已经让它进入偏好片段的 onCreate 方法,但是屏幕显示空白。下面是一张图片:
我感觉片段在布局后面加载,但我不知道如何检查它,因为即使我 运行 它在空 Activity 上我也会得到类似的行为使用单个 FrameLayout 进行布局。我正在尝试使用 onOptionSelected() 从我的 Main 中显示一个设置页面。
我已经坚持了大约一个星期,none 我找到的答案很有帮助,我们将不胜感激!
这是我的 Activity 代码:
public class genericActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(Main.appTAG, "onCreate: settings activity");
setContentView(R.layout.activity_generic);
// IF intent message == settings
final Fragment settingsFragment = new SettingsFragment();
getFragmentManager()
.beginTransaction()
.replace(R.id.fragmentParent, settingsFragment)
.addToBackStack(null)
.commit();
getFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
getFragmentManager()
.beginTransaction()
.remove(settingsFragment)
.commit();
}
});
}
}
Activity的布局,即activity_generic.xml如下
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.thesis.deefunkt.thesispractise.genericActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/fragmentParent"
android:fitsSystemWindows="true">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list">
</ListView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
我真的不需要兼容性功能,因为这个屏幕所做的只是设置,但为了保持一致的外观而保留了它。
我的SettingsFragment代码如下:
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
Log.d(Main.appTAG, "onCreate: SettingsFragment ");
}
}
虽然这是 运行ning,但我有一堆我不知道如何解释的日志条目:
D/com.deefunkt.UsbComms: onCreate: settings activity
D/PhoneWindow: *FMB* installDecor mIsFloating : false
D/PhoneWindow: *FMB* installDecor flags : -2139029248
D/AbsListView: Get MotionRecognitionManager
D/Activity: performCreate Call Injection manager
W/Resources: Converting to string: TypedValue{t=0x10/d=0x2580 a=-1}
W/Resources: Converting to string: TypedValue{t=0x10/d=0x8 a=-1}
W/Resources: Converting to string: TypedValue{t=0x10/d=0x1 a=-1}
W/Resources: Converting to string: TypedValue{t=0x10/d=0x0 a=-1}
D/com.deefunkt.UsbComms: onCreate: SettingsFragment
I/InjectionManager: dispatchPreferences > Target : com.thesis.deefunkt.thesispractise.SettingsFragment
D/AbsListView: Get MotionRecognitionManager
I/InjectionManager: dispatchOnViewCreated > Target : com.thesis.deefunkt.thesispractise.SettingsFragment isFragment :true
I/InjectionManager: dispatchOnViewCreated > Target : com.thesis.deefunkt.thesispractise.genericActivity isFragment :false
D/SecWifiDisplayUtil: Metadata value : SecSettings2
D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
D/SRIB_DCS: log_dcs ThreadedRenderer::initialize entered!
D/mali_winsys: new_window_surface returns 0x3000, [1440x2560]-format:1
D/OpenGLRenderer: endAllStagingAnimators on 0x7f65c06600 (RippleDrawable) with handle 0x7f74cff540
如您所见,调用了片段内的 onCreate 方法,但我不知道如何处理此日志数据。
提前致谢!
好吧,在这上面花了这么多时间,我终于设法修复了这个错误,但我不知道为什么。我最好的猜测是
getFragmentManager()
.beginTransaction()
.replace(R.id.fragmentParent, settingsFragment)
/*Adding to the backStack here and then setting a backStack Listener
*on the next step made it add the fragment then immediately take it
*off, giving my empty activity layout.*/
.addToBackStack(null)
.commit();
getFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
getFragmentManager()
.beginTransaction()
.remove(settingsFragment)
.commit();
}
});
我删除了对 addToBackStack() 的调用并注释掉了 backStack 侦听器,因为我意识到在 activity 加载后我不需要删除该片段并且只真正关心重用 activity 用于多个片段。