1

I'm new to Android. I'm creating an application in which I have to use a map in my two activities. My application is running good but when I tap on my 'TagYourself' activity (one of the map activities) my application crashes. Log cat shows the null pointer exception on line 46 which is this.setContentView(R.layout.activity_tag_yourself);. Log cat is also showing. android.view.InflateException on Binary XML file line # 17 which is the fragment for map.

I couldn't understand why it is giving such exceptions. Please help me. Here is my log cat for the acitivity.

04-06 23:20:02.851: I/InputMethodManager(31122): handleMessage: MSG_SET_ACTIVE false, was true
04-06 23:20:02.867: I/InputMethodManager(31122): handleMessage: MSG_UNBIND 985
04-06 23:20:02.911: D/OpenGLRenderer(31122): Flushing caches (mode 0)
04-06 23:20:03.276: D/OpenGLRenderer(31122): Flushing caches (mode 0)
04-06 23:20:08.341: V/PhoneWindow(31122): DecorView setVisiblity: visibility = 0
04-06 23:20:08.386: V/InputMethodManager(31122): onWindowFocus: null softInputMode=32 first=true flags=#1810100
04-06 23:20:08.386: V/InputMethodManager(31122): START INPUT: com.android.internal.policy.impl.PhoneWindow$DecorView{41394140 V.E..... R.....ID 0,0-480,854} ic=null tba=android.view.inputmethod.EditorInfo@412aebc0 controlFlags=#104
04-06 23:20:08.390: V/InputMethodManager(31122): Starting input: Bind result=InputBindResult{com.android.internal.view.IInputMethodSession$Stub$Proxy@412635d8 com.android.inputmethod.latin/.LatinIME #989}
04-06 23:20:08.420: I/InputMethodManager(31122): handleMessage: MSG_SET_ACTIVE true, was false
04-06 23:20:08.427: I/SurfaceTextureClient(31122): [STC::queueBuffer] (this:0x52cb22e8) fps:0.15, dur:6849.47, max:6849.47, min:6849.47
04-06 23:20:08.427: I/SurfaceTextureClient(31122): [STC::queueBuffer] this:0x52cb22e8, api:1, last queue time elapsed:6849.47
04-06 23:20:09.363: V/Provider/Settings(31122): invalidate [system]: current 182 != cached 0
04-06 23:20:09.367: D/ActivityThread(31122): installProvider: context=android.app.ContextImpl@41393588holder=android.app.IActivityManager$ContentProviderHolder@41271e08noisy=truenoReleaseNeeded=truestable=true
04-06 23:20:09.368: V/Provider/Settings(31122): from db cache, name = sound_effects_enabled , value = 0
04-06 23:20:09.393: D/mylog(31122): Wifi State true
04-06 23:20:09.514: I/SurfaceTextureClient(31122): [STC::queueBuffer] (this:0x52cb22e8) fps:1.84, dur:1087.15, max:843.11, min:244.04
04-06 23:20:09.562: D/AndroidRuntime(31122): Shutting down VM
04-06 23:20:09.562: W/dalvikvm(31122): threadid=1: thread exiting with uncaught exception (group=0x40cab9a8)
04-06 23:20:09.584: E/AndroidRuntime(31122): FATAL EXCEPTION: main
04-06 23:20:09.584: E/AndroidRuntime(31122): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jamaat_times/com.example.jamaattiming.TagYourself}: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.os.Handler.dispatchMessage(Handler.java:107)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.os.Looper.loop(Looper.java:194)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.app.ActivityThread.main(ActivityThread.java:5371)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at java.lang.reflect.Method.invokeNative(Native Method)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at java.lang.reflect.Method.invoke(Method.java:525)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at dalvik.system.NativeStart.main(Native Method)
04-06 23:20:09.584: E/AndroidRuntime(31122): Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:281)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.app.Activity.setContentView(Activity.java:1881)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at com.example.jamaattiming.TagYourself.onCreate(TagYourself.java:46)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.app.Activity.performCreate(Activity.java:5122)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
04-06 23:20:09.584: E/AndroidRuntime(31122):    ... 11 more
04-06 23:20:09.584: E/AndroidRuntime(31122): Caused by: java.lang.NullPointerException: name == null
04-06 23:20:09.584: E/AndroidRuntime(31122):    at java.lang.VMClassLoader.findLoadedClass(Native Method)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:354)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at java.lang.ClassLoader.loadClass(ClassLoader.java:491)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:436)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:255)
04-06 23:20:09.584: E/AndroidRuntime(31122):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
04-06 23:20:09.584: E/AndroidRuntime(31122):    ... 21 more

here is xml code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="#808080"
    tools:context=".TagYourself" 
    android:id="@+id/activity_tag_yourself">

<TextView
    android:id="@+id/tv_distance_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:text="@string/hello_world" />

<fragment
    android:id="@+id/map"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/tv_distance_time" />

</RelativeLayout>

here is Java Code for TagYourself Class:

    public class TagYourself extends FragmentActivity
{
    GoogleMap map;
    ArrayList<LatLng> markerPoints;
    TextView tvDistanceDuration;

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

        this.tvDistanceDuration = (TextView) this.findViewById(R.id.tv_distance_time);
        // Initializing
        this.markerPoints = new ArrayList<LatLng>();


        SupportMapFragment fm = (SupportMapFragment) this.getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting Map for the SupportMapFragment
        this.map = fm.getMap();

        // Enable MyLocation Button in the Map
        this.map.setMyLocationEnabled(true);

        // Setting onclick event listener for the map
        this.map.setOnMapClickListener(new OnMapClickListener()
        {
            @Override
            public void onMapClick(LatLng point)
            {
                // Already two locations
                if (TagYourself.this.markerPoints.size() > 1)
                {
                    TagYourself.this.markerPoints.clear();
                    TagYourself.this.map.clear();
                }

                // Adding new item to the ArrayList
                TagYourself.this.markerPoints.add(point);

                // Creating MarkerOptions
                MarkerOptions options = new MarkerOptions();

                // Setting the position of the marker
                options.position(point);

                /**
                 * For the start location, the color of marker is GREEN and
                 * for the end location, the color of marker is RED.
                 */
                if (TagYourself.this.markerPoints.size() == 1)
                {
                    options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
                } else if (TagYourself.this.markerPoints.size() == 2)
                {
                    options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
                }

                // Add new marker to the Google Map Android API V2
                TagYourself.this.map.addMarker(options);

                // Checks, whether start and end locations are captured
                if (TagYourself.this.markerPoints.size() >= 2)
                {
                    LatLng origin = TagYourself.this.markerPoints.get(0);
                    LatLng dest = TagYourself.this.markerPoints.get(1);

                    // Getting URL to the Google Directions API
                    String url = TagYourself.this.getDirectionsUrl(origin, dest);

                    DownloadTask downloadTask = new DownloadTask();

                    // Start downloading json data from Google Directions API
                    downloadTask.execute(url);
                }
            }
        });
    }

    private String getDirectionsUrl(LatLng origin, LatLng dest)
    {
        // Origin of route
        String str_origin = "origin=" + origin.latitude + "," + origin.longitude;

        // Destination of route
        String str_dest = "destination=" + dest.latitude + "," + dest.longitude;

        // Sensor enabled
        String sensor = "sensor=false";

        // Building the parameters to the web service
        String parameters = str_origin + "&" + str_dest + "&" + sensor;

        // Output format
        String output = "json";

        // Building the url to the web service
        String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;

        return url;
    }

    /** A method to download json data from url */
    private String downloadUrl(String strUrl) throws IOException
    {
        String data = "";
        InputStream iStream = null;
        HttpURLConnection urlConnection = null;
        try
        {
            URL url = new URL(strUrl);

            // Creating an http connection to communicate with url
            urlConnection = (HttpURLConnection) url.openConnection();

            // Connecting to url
            urlConnection.connect();

            // Reading data from url
            iStream = urlConnection.getInputStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

            StringBuffer sb = new StringBuffer();

            String line = "";
            while ((line = br.readLine()) != null)
            {
                sb.append(line);
            }

            data = sb.toString();

            br.close();

        } catch (Exception e)
        {
            Log.d("Exception while downloading url", e.toString());
        } finally
        {
            iStream.close();
            urlConnection.disconnect();
        }
        return data;
    }

I've uses the following permissions:

 <uses-sdk
  android:minSdkVersion="11"
  android:targetSdkVersion="16" />

<uses-permission android:name="com.example.jamaat_times.permission.MAPS_RECEIVE" />

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

P.S: There is no problem in my key.

2 Answers 2

1

Caused by: java.lang.NullPointerException: name == null

You are missing android:name attribute for the fragment

<fragment
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_distance_time" 
android:name="com.google.android.gms.maps.SupportMapFragment"/>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot for the answer sir. My Problem Has been resolved.
0

use this code in your xml

  <fragment
    android:id="@+id/map"
    android:layout_width="wrap_content"
    android:layout_height="wrap_contet" 
    android:layout_below="@id/tv_distance_time"
    android:name="com.google.android.gms.maps.SupportMapFragment" />

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.