Hick
Hick

Reputation: 36404

Java null pointer exception while using google maps in Android

package com.crumbin.tabs;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.client.HttpClient;

import android.content.Context;
import android.database.Cursor;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.webkit.WebView;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;



public class ExploreActivity extends MapActivity {





  public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(com.app.main.R.layout.user_main_tab_explore);




        MapView mv = (MapView)findViewById(com.app.main.R.id.myMapView);
        mv.setBuiltInZoomControls(true);






      }

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <com.google.android.maps.MapView
  android:id="@+id/myMapView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:enabled="true"
  android:clickable="true"
  android:apiKey="Mykey"/>
  </LinearLayout>

This code gives me a java null pointer exception. I debugged it and found out that Mapview mv is null.

Shouldn't this code just display the map and no overlay/data on it? Or am I missing something here?

Upvotes: 0

Views: 269

Answers (1)

ikromm
ikromm

Reputation: 523

This line MapView mv = (MapView)findViewById(com.app.main.R.id.myMapView); needs to be MapView mv = (MapView)findViewById(R.id.myMapView);

To explain more, you are requesting an ID of a View that is on the R class in the package com.app.main, when you need to request it from the R class in your own package, which can be omitted.

Upvotes: 3

Related Questions