Question

I want to clickable markers so that i did what does link says below. clickable marker 1

clickable marker 2

After extends android.support.v4.app.FragmentActivity i get null pointer exception on

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

My all code too long so i give some parts:

 public class GpsMapActivity extends android.support.v4.app.FragmentActivity implements OnMarkerClickListener, LocationListener {
    private GoogleMap mapView;

        public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
        case R.id.myAccount:
            Intent intent = new Intent(GpsMapActivity.this, MyAccountActivity.class);
            startActivity(intent);
            return true;
        case R.id.announcements:
            Intent intent2 = new Intent(GpsMapActivity.this, AnnouncementsActivity.class);
            startActivity(intent2);
            return true;
        }
        return false;
    }
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gps_map_activity);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 

        mapView = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        mapView.setOnMarkerClickListener((OnMarkerClickListener)this);

gps_map_activity.xml

 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="300dp"
    android:orientation="vertical"
    android:id="@+id/layone"
    >
   <fragment
  android:id="@+id/map"        
  android:layout_width="fill_parent"        
  android:layout_height="fill_parent"        
  class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
  <TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
     >
      <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">
        <Button
        android:text="Insert Event"
       android:id="@+id/insertevent"  />
         <Button
        android:text="Back to View"
       android:id="@+id/backtoview"  />
         </TableRow>
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">
        <Button
        android:text="to Up"
       android:id="@+id/toUp"  />
         </TableRow>
          <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">
        <Button
        android:text="to Left"
       android:id="@+id/toLeft"  />

    <Button
        android:text="to Right"
       android:id="@+id/toRight"  />
         </TableRow>

         <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">
        <Button
        android:text="to Down"
        android:id="@+id/toDown" />
         </TableRow>
         </TableLayout>
</LinearLayout>

why i get null pointer exception ? any ideas?

Was it helpful?

Solution

You have

class="com.google.android.gms.maps.SupportMapFragment"

But while you initialize you have

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

Should be

mapView = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

If a GoogleMap is not available, getMap() will return null. So better check the availability of google play services before initializin map object

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top