0

I have been following Googles guide to making an app that allows you to view an app on an Android phone. I have followed many tutorials, but always end up with the same error! I have also searched around the web and figured my error is something to do with the "fragment". Here are my files:

MainActivity.java

package com.example.theapp;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.theapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<permission
    android:name="com.example.theapp.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>

<uses-permission android:name="com.example.theapp.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>     

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="--REMOVED--"/>


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.theapp.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

</application>

I am using Android 2.3. Here is the log cat messages:

04-02 22:43:45.890: D/AndroidRuntime(23773): Shutting down VM
04-02 22:43:45.890: W/dalvikvm(23773): threadid=1: thread exiting with uncaught exception    (group=0x40018578)
04-02 22:43:45.898: E/AndroidRuntime(23773): FATAL EXCEPTION: main
04-02 22:43:45.898: E/AndroidRuntime(23773): java.lang.RuntimeException: Unable to start   activity ComponentInfo{com.example.theapp/com.example.theapp.MainActivity}:   android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.os.Looper.loop(Looper.java:130)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.app.ActivityThread.main(ActivityThread.java:3687)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at java.lang.reflect.Method.invokeNative(Native Method)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at java.lang.reflect.Method.invoke(Method.java:507)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at dalvik.system.NativeStart.main(Native Method)
04-02 22:43:45.898: E/AndroidRuntime(23773): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:209)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.app.Activity.setContentView(Activity.java:1657)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at com.example.theapp.MainActivity.onCreate(MainActivity.java:11)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-02 22:43:45.898: E/AndroidRuntime(23773):    ... 11 more
04-02 22:43:45.898: E/AndroidRuntime(23773): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.theapp-1.apk]
04-02 22:43:45.898: E/AndroidRuntime(23773):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.view.LayoutInflater.createView(LayoutInflater.java:471)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
04-02 22:43:45.898: E/AndroidRuntime(23773):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
04-02 22:43:45.898: E/AndroidRuntime(23773):    ... 19 more
04-02 22:55:53.289: D/AndroidRuntime(23942): Shutting down VM
04-02 22:55:53.289: W/dalvikvm(23942): threadid=1: thread exiting with uncaught exception (group=0x40018578)
04-02 22:55:53.304: E/AndroidRuntime(23942): FATAL EXCEPTION: main
04-02 22:55:53.304: E/AndroidRuntime(23942): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.example.theapp/com.example.theapp.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.os.Looper.loop(Looper.java:130)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.app.ActivityThread.main(ActivityThread.java:3687)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at java.lang.reflect.Method.invokeNative(Native Method)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at java.lang.reflect.Method.invoke(Method.java:507)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at dalvik.system.NativeStart.main(Native Method)
04-02 22:55:53.304: E/AndroidRuntime(23942): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:209)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.app.Activity.setContentView(Activity.java:1657)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at com.example.theapp.MainActivity.onCreate(MainActivity.java:11)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-02 22:55:53.304: E/AndroidRuntime(23942):    ... 11 more
04-02 22:55:53.304: E/AndroidRuntime(23942): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.theapp-2.apk]
04-02 22:55:53.304: E/AndroidRuntime(23942):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.view.LayoutInflater.createView(LayoutInflater.java:471)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
04-02 22:55:53.304: E/AndroidRuntime(23942):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
04-02 22:55:53.304: E/AndroidRuntime(23942):    ... 19 more

Any help would be appreciated. Thank you in advance.

3 Answers 3

1
     package com.example.theapp;

      import android.app.Activity;
              import android.os.Bundle;

             public class MainActivity extends FragmentActivity {

                  @Override
             protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_main);
                   }
                }

and in manifest add one more meta tag below previous one.

        <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="Api key" />
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for getting back to me. Your code worked. However I am having some more problems, I am trying to achieve buttons that allow you to change its location such as: "youtube.com/watch?v=awX5T-EwLPc", however he does not use "extend FragmentActivity", how may I work around this? I currently have a map on one page, I cannot add a button above the map. It just shows the map, else its on top of the map. I have created a separate question. Thank you. Heres the link: stackoverflow.com/questions/22842732/…
0

As far as I remember, You should include android-support-v4.jar file from Android SDK folder "sdk/extras/android/support/v4/android-support-v4.jar". If You dont have that file, in Your SDK Manager install Android Support Library, which is found under Extras.

1 Comment

Thanks for getting back to me. I have already included this file, however now I have another problem. I would be grateful if you could take a look. Here is the link: stackoverflow.com/questions/22842732/…
0

Please check the following steps:

  1. In android manifest you have the NETWORK STATE permission like this

    < uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

  2. In android manifest make sure you have the following code as well-

      <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value=" YOUR KEY HERE" />
    
     <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />  
    
  3. Lastly if you are testing your application on android API 5.x then make sure you modify your map code like this-

Instead of this

 map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

write this

map = getMapFragment().getMap();

and add a small method like this

private MapFragment getMapFragment() {
        FragmentManager fm = null;

        Log.d("", "sdk: " + Build.VERSION.SDK_INT);
        Log.d("", "release: " + Build.VERSION.RELEASE);

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            Log.d("", "using getFragmentManager");
            fm = getFragmentManager();
        } else {
            Log.d("", "using getChildFragmentManager");
            fm = getChildFragmentManager();
        }

        return (MapFragment) fm.findFragmentById(R.id.map);
    }

And make sure for step 3 your min sdk version in your manifest file is 17 like this

<uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="21" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.