应用程序适用于API 10,但在API 11上启动时崩溃

时间:2013-11-15 17:07:28

标签: java android

我是Android开发的新手,我还在学习。我正在尝试创建一个基本的应用程序来显示到达的公共汽车路线时间(带有公交路线的导航抽屉的简单文本)。

我正在使用Android Studio导航抽屉模板。

适用于我的HTC Desire(Bravo)API 10,但拒绝使用华为Media Pad 7 Lite(API 11)。

这是logcat

> 11-15 17:59:48.721    4343-4343/com.prospectsoftware.samoborcek
> D/dalvikvm﹕ Late-enabling CheckJNI 11-15 17:59:48.771   
> 4343-4343/com.prospectsoftware.samoborcek I/SystemProperties﹕ get
> key=ro.kernel.android.tracing 11-15 17:59:49.001   
> 4343-4343/com.prospectsoftware.samoborcek D/AndroidRuntime﹕ Shutting
> down VM 11-15 17:59:49.001   
> 4343-4343/com.prospectsoftware.samoborcek W/dalvikvm﹕ threadid=1:
> thread exiting with uncaught exception (group=0x41010208) 11-15
> 17:59:49.021    4343-4343/com.prospectsoftware.samoborcek
> E/AndroidRuntime﹕ FATAL EXCEPTION: main
>     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.prospectsoftware.samoborcek/com.prospectsoftware.samoborcek.MainActivity}:
> android.view.InflateException: Binary XML file line #24: Error
> inflating class fragment
>             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
>             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
>             at android.app.ActivityThread.access$600(ActivityThread.java:123)
>             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
>             at android.os.Handler.dispatchMessage(Handler.java:99)
>             at android.os.Looper.loop(Looper.java:137)
>             at android.app.ActivityThread.main(ActivityThread.java:4424)
>             at java.lang.reflect.Method.invokeNative(Native Method)
>             at java.lang.reflect.Method.invoke(Method.java:511)
>             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
>             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
>             at dalvik.system.NativeStart.main(Native Method)
>      Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
>             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
>             at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
>             at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
>             at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
>             at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
>             at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
>             at android.app.Activity.setContentView(Activity.java:1835)
>             at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:208)
>             at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
>             at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
>             at com.prospectsoftware.samoborcek.MainActivity.onCreate(MainActivity.java:35)
>             at android.app.Activity.performCreate(Activity.java:4465)
>             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
>             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
>             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
>             at android.app.ActivityThread.access$600(ActivityThread.java:123)
>             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
>             at android.os.Handler.dispatchMessage(Handler.java:99)
>             at android.os.Looper.loop(Looper.java:137)
>             at android.app.ActivityThread.main(ActivityThread.java:4424)
>             at java.lang.reflect.Method.invokeNative(Native Method)
>             at java.lang.reflect.Method.invoke(Method.java:511)
>             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
>             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
>             at dalvik.system.NativeStart.main(Native Method)
>      Caused by: java.lang.NullPointerException
>             at com.prospectsoftware.samoborcek.MainActivity.onCreateOptionsMenu(MainActivity.java:172)
>             at android.app.Activity.onCreatePanelMenu(Activity.java:2444)
>             at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
>             at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:224)
>             at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:141)
>             at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
>             at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:280)
>             at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:391)
>             at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:742)
>             at android.app.Activity.invalidateOptionsMenu(Activity.java:2552)
>             at android.support.v4.app.ActivityCompatHoneycomb.invalidateOptionsMenu(ActivityCompatHoneycomb.java:29)
>             at android.support.v4.app.FragmentActivity.supportInvalidateOptionsMenu(FragmentActivity.java:633)
>             at android.support.v7.app.ActionBarActivity.supportInvalidateOptionsMenu(ActionBarActivity.java:170)
>             at android.support.v4.app.Fragment.setHasOptionsMenu(Fragment.java:781)
>             at com.prospectsoftware.samoborcek.NavigationDrawerFragment.onCreate(NavigationDrawerFragment.java:83)
>             at android.support.v4.app.Fragment.performCreate(Fragment.java:1455)
>             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
>             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
>             at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
>             at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java

我不知道如何正确检测错误在哪一行,我在几个地方读到了我应该寻找“引起”的行,但它并没有给我提供很多信息或者我不知道如何正确识别它们。

任何帮助都会受到赞赏,我尝试在所有地方搜索,但我找不到任何与我的问题有关的内容,也没有解释为什么会发生这种情况。

在HTC Desire(API 10)上,logcat没有显示错误。

如果您需要任何源代码,请告诉我,我不知道要粘贴什么,我仍然想要了解所有内容并了解实际情况。

    MainActivity.java (170-180)

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            if (!mNavigationDrawerFragment.isDrawerOpen()) { << Line: 172
                // Only show items in the action bar relevant to this screen
                // if the drawer is not showing. Otherwise, let the drawer
                // decide what to show in the action bar.
                getMenuInflater().inflate(R.menu.main, menu);
                restoreActionBar();
                return true;
            }
            return super.onCreateOptionsMenu(menu);
        }

    ActivityMain XML File

    <!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
    <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=".MainActivity">

        <!-- As the main content view, the view below consumes the entire
             space available using match_parent in both dimensions. -->
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <!-- android:layout_gravity="start" tells DrawerLayout to treat
             this as a sliding drawer on the left side for left-to-right
             languages and on the right side for right-to-left languages.
             If you're not building against API 17 or higher, use
             android:layout_gravity="left" instead. -->
        <!-- The drawer is given a fixed width in dp and extends the full height of
             the container. -->
        <fragment android:id="@+id/navigation_drawer" << Line: 24
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:name="com.prospectsoftware.samoborcek.NavigationDrawerFragment"
            tools:layout="@layout/simple_list_item_1" />

    </android.support.v4.widget.DrawerLayout>

mDrawerFragment ---



@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(MainActivity.this, "Aplikacija pokrenuta", Toast.LENGTH_SHORT).show();

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));
    }

编辑:评论以下部分使其适用于API 11+,确认这是问题所在。

/*if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.main, menu);
            restoreActionBar();
            return true;
        }*/

0 个答案:

没有答案