我已经创建了一个Android Studio项目,我已经通过(Mike Penz)
基于Github项目添加了一个导航抽屉抽屉工作正常,我可以很好地导航到菜单片段。
我只有一个需要帮助的问题。
我的( SettingsFragment )扩展( PreferenceFragment ),但是从我的dawer我无法导航设置片段。它只是崩溃了应用程序。
SettingsFragment.Java
public class SettingsFragment extends PreferenceFragment {
public SettingsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
以下是导航到不同片段的代码
MainActivity.Java
...
public void selectDrawerItem(int ItemId,IDrawerItem drawerItem) {
// Create a new fragment and specify the fragment to show based on nav item clicked
Fragment fragment = null;
Class fragmentClass;
switch(ItemId) {
case 1:
fragmentClass = OneFragment.class;
break;
case 2:
fragmentClass = TwoFragment.class;
break;
case 3:
fragmentClass = ThreeFragment.class;
break;
case 4:
fragmentClass = FourFragment.class;
break;
case 5:
fragmentClass = SettingsFragment.class;
break;
default:
fragmentClass = OneFragment.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
result.closeDrawer();
}
...
LogCat输出
12-19 20:32:06.619 10651-10651/com.company.app W/System.err: java.lang.ClassCastException: com.company.app.fragments.SettingsFragment cannot be cast to android.support.v4.app.Fragment
12-19 20:32:06.619 10651-10651/com.company.app W/System.err: at com.company.app.activities.MainActivity.selectDrawerItem(MainActivity.java:259)
12-19 20:32:06.620 10651-10651/com.company.app W/System.err: at com.company.app.activities.MainActivity$3.onItemClick(MainActivity.java:225)
12-19 20:32:06.620 10651-10651/com.company.app W/System.err: at com.mikepenz.materialdrawer.DrawerBuilder$6.onClick(DrawerBuilder.java:1768)
12-19 20:32:06.620 10651-10651/com.company.app W/System.err: at com.mikepenz.materialdrawer.DrawerBuilder$6.onClick(DrawerBuilder.java:1742)
12-19 20:32:06.620 10651-10651/com.company.app W/System.err: at com.mikepenz.fastadapter.FastAdapter$1.onClick(FastAdapter.java:580)
12-19 20:32:06.621 10651-10651/com.company.app W/System.err: at com.mikepenz.fastadapter.utils.EventHookUtil$1.onClick(EventHookUtil.java:72)
12-19 20:32:06.621 10651-10651/com.company.app W/System.err: at android.view.View.performClick(View.java:6256)
12-19 20:32:06.621 10651-10651/com.company.app W/System.err: at android.view.View$PerformClick.run(View.java:24701)
12-19 20:32:06.621 10651-10651/com.company.app W/System.err: at android.os.Handler.handleCallback(Handler.java:789)
12-19 20:32:06.621 10651-10651/com.company.app W/System.err: at android.os.Handler.dispatchMessage(Handler.java:98)
12-19 20:32:06.621 10651-10651/com.company.app W/System.err: at android.os.Looper.loop(Looper.java:164)
12-19 20:32:06.622 10651-10651/com.company.app W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6541)
12-19 20:32:06.622 10651-10651/com.company.app W/System.err: at java.lang.reflect.Method.invoke(Native Method)
12-19 20:32:06.622 10651-10651/com.company.app W/System.err: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
12-19 20:32:06.622 10651-10651/com.company.app W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
12-19 20:32:06.628 10651-10651/com.company.app D/AndroidRuntime: Shutting down VM
12-19 20:32:06.630 10651-10651/com.company.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.company.app, PID: 10651
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:441)
at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:432)
at com.company.app.activities.MainActivity.selectDrawerItem(MainActivity.java:265)
at com.company.app.activities.MainActivity$3.onItemClick(MainActivity.java:225)
at com.mikepenz.materialdrawer.DrawerBuilder$6.onClick(DrawerBuilder.java:1768)
at com.mikepenz.materialdrawer.DrawerBuilder$6.onClick(DrawerBuilder.java:1742)
at com.mikepenz.fastadapter.FastAdapter$1.onClick(FastAdapter.java:580)
at com.mikepenz.fastadapter.utils.EventHookUtil$1.onClick(EventHookUtil.java:72)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
答案 0 :(得分:0)
您SettingsFragment
正在扩展无法投放到PreferenceFragment
的{{1}}
解决方法是将Fragment
单独加载到新创建的PreferenceFragment
类中:
<强>代码强>
Fragment
同样适用于 public class SettingsFragment extends Fragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getActivity().getFragmentManager().beginTransaction()
.replace(android.R.id.content, new MyPreferenceFragment())
.commit();
}
public static class MyPreferenceFragment extends PreferenceFragment {
public MyPreferenceFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
}
}
根布局:添加
Fragment
这将解决android:background="?android:windowBackground"
android:clickable = "true"
导航中的重叠问题。