Android Studio(导航抽屉)switch-case“0”导致“错误膨胀类片段”

时间:2015-03-17 13:04:06

标签: java android android-fragments android-studio

我是Android开发的新手,我最近开始编写应用程序,只是为了好玩。我使用了"导航抽屉模板"。

在我的导航抽屉中有8个元素,选择元素2-8工作正常。但是,当我将一个动作分配给元素1时,我得到了#二十二分之二行xml第22行:错误膨胀类片段"。

我知道我编程的方式可能很奇怪,但我之前从未编写过Java,只有C ++ ......

这很好用:

private void selectItem(int position) {
    mCurrentSelectedPosition = position;
    switch (position){
       /* case 0:
            activity_main.webview.loadUrl("http://www.random.org/");
            break;*/
        case 1:
            activity_main.webview.loadUrl("http://www.google.com/");
            break;
        case 2:
            activity_main.webview.loadUrl("http://facebook.com/");
            break;
        case 3:
            activity_main.webview.loadUrl("http://stackoverflow.com/");
            break;
        case 4:
            activity_main.webview.loadUrl("http://plus.google.com/");
            break;
        case 5:
            activity_main.webview.loadUrl("http://flickr.com/");
            break;
        case 6:
            activity_main.webview.loadUrl("http://9gag.com/");
            break;
        case 7:
            activity_main.webview.loadUrl("http://reddit.com");
            break;
    }
    if (mDrawerListView != null) {
        mDrawerListView.setItemChecked(position, true);
    }
    if (mDrawerLayout != null) {
        mDrawerLayout.closeDrawer(mFragmentContainerView);
    }
    if (mCallbacks != null) {
        mCallbacks.onNavigationDrawerItemSelected(position);
    }
}

当我取消注释"案例0"但是,我收到以下错误:

03-17 13:37:58.900    4265-4265/com.mycompany.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mycompany.app, PID: 4265
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.app/com.mycompany.app.activity_main}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
            at android.app.ActivityThread.access$900(ActivityThread.java:147)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5256)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
     Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:240)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at com.mycompany.app.activity_main.onCreate(activity_main.java:41)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
            at android.app.ActivityThread.access$900(ActivityThread.java:147)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5256)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference
            at com.mycompany.app.NavigationDrawerFragment.selectItem(NavigationDrawerFragment.java:201)
            at com.mycompany.app.NavigationDrawerFragment.onCreate(NavigationDrawerFragment.java:80)
            at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1114)
            at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1216)
            at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2169)
            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
            at android.support.v7.app.ActionBarActivity.onCreateView(ActionBarActivity.java:543)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:240)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at com.mycompany.app.activity_main.onCreate(activity_main.java:41)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
            at android.app.ActivityThread.access$900(ActivityThread.java:147)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5256)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

我看到它会导致nullpointer异常,但我无法理解为什么......所以也许你可以看看,如果你需要更多代码,请告诉我们。)

提前致谢, fl0_at

--- --- EDIT

这是您要求的xml:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
    android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".NMW">
    <FrameLayout android:id="@+id/container" android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
        android:layout_gravity="start"
        class="com.mycompany.app.NavigationDrawerFragment"
        tools:layout="@layout/fragment_navigation_drawer"
        android:background="@drawable/activated_background"/>
</android.support.v4.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:1)

感谢Ivo Beckers和Selvin,我找到了解决问题的方法。在我的 NavigationDrawerFragment.java 中,我编辑了 onCreate

if(activity_main.webview != null){
    selectItem(0);
}

所以 onCreate 的最终版本是:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
    mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);

    if (savedInstanceState != null) {
        mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
        mFromSavedInstanceState = true;
    }

    // Select either the default item (0) or the last selected item, i.e. selectItem(mCurrentSelectedPosition);
    if (activity_main.webview != null) {
        selectItem(0);
    }
}

干杯!

fl0_at