0

I'm new to Android development and I am still learning. I'm trying to create a basic application to show bus route time of arrivals (simple text with a navigation drawer with bus routes).

I'm using Android Studio Navigation Drawer template for this.

It works on my HTC Desire (Bravo) API 10, but refuses to work on Huawei Media Pad 7 Lite (API 11).

This is the 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

I have no idea how to properly detect on which line the error is, I read on a couple of places that I should look for "Caused" line, but it doesn't gives me a lot of information to work with or I don't know how to recognize them properly.

Any help would be appreciated, I tried searching all over the place but I can't find anything specific to my problem nor an explanation why it's happening.

On HTC Desire (API 10), logcat shows no errors.

If you need any source code please tell me to, I have no idea what to paste really, I'm still trying to catch up with it all and understand what everything is actually.

    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));
    }

EDIT: Commenting out the following part made it work on API 11+ confirming this was the issue.

/*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;
        }*/
Petar-Krešimir
  • 444
  • 8
  • 22
  • Please post your XML file – Apoorv Nov 15 '13 at 17:08
  • Show MainActivity.java (around line 172) – Tobias Nov 15 '13 at 17:09
  • Line 24 of your XML has an issue. Do you have different layouts for different screen sizes/shapes? Maybe the issue is in one of your XML files, and it is not the API of the device that affects the issue (but rather the screen resolution). – Tenfour04 Nov 15 '13 at 17:11
  • The real problem is a NPE in `com.prospectsoftware.samoborcek.MainActivity.onCreateOptionsMenu(MainActivity.java:172)`. I guess `mNavigationDrawerFragment` is `null`. – Tobias Nov 15 '13 at 17:14
  • I have added the lines from the logcat. Don't be too harsh on me, I just started and learning in my spare time. I used Android Studio Navigation Drawer project template and haven't modified it. – Petar-Krešimir Nov 15 '13 at 17:15
  • see [this][1] maybe you can find something helps you [1]: http://stackoverflow.com/questions/18152779/app-crashes-on-startup-on-android-api-10-but-not-on-api-16-because-of-resources – bourax webmaster Nov 15 '13 at 17:28
  • So how can I resolve the NPE? I checked the code but can't seem to find what may be causing it @still_learning – Petar-Krešimir Nov 15 '13 at 18:06
  • When do you initialize `mNavigationDrawerFragment`? – Tobias Nov 15 '13 at 18:09
  • I have added it to the end of the question. There are only three parts of the code where the mNavigationDrawerFragment is showing, on the beginning where it's declared, on onCreate and on the line 172 where the NPE occurs. I'm still all new to this and I still don't understand the whole concept, sorry if my question sounds a bit dumb. – Petar-Krešimir Nov 15 '13 at 18:19

0 Answers0