Android:操作栏按钮上的Google Map Null指针异常错误(Map按钮)

时间:2014-07-05 01:36:33

标签: android android-fragments android-actionbar android-maps-v2

我正在开发简单的地图应用程序。我用动作吧。我在操作栏上添加了Map按钮。当用户单击“地图”按钮时,将进入“下一个”活动(“地图活动”)。将从sqlite db显示Map Activity。 当我点击地图按钮时,我收到Null指针异常错误。但是当我使用Next活动(Map Activity)作为主要活动时。它运作良好。

你认为为什么我在Map Button上收到错误? 当我点击操作栏上的地图按钮进入下一个活动(地图活动)时,我是否需要做一些工作?

欢迎所有反馈和建议:) 这是我的代码:

MainActivity.java

public class MainActivity extends Activity {



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

    ActionBar actionBar = getActionBar();
    actionBar.setIcon(R.drawable.logo);


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
    case R.id.action_map:
        Intent i = new Intent(this, AllMap.class);
        startActivity(i);
        Toast.makeText(getApplicationContext(), "Map selected",
                Toast.LENGTH_SHORT).show();
        return true;
case R.id.action_search:
        // Toast.makeText(getApplicationContext(), "Search selected",
        // Toast.LENGTH_SHORT).show();
        return true;


default:
        return super.onOptionsItemSelected(item);
    }
  } 

}

MapActivity.Java

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

   import com.google.android.gms.maps.GoogleMap;
   import com.google.android.gms.maps.MapFragment;
   import com.google.android.gms.maps.model.LatLng;
   import com.google.android.gms.maps.model.Marker;
   import com.google.android.gms.maps.model.MarkerOptions;

   public class AllMap extends Activity {
    // Google Map
    private GoogleMap googleMap;
    private Marker marker;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        initilizeMap();
    } catch (Exception e) {
        e.printStackTrace();
    }
     LatLng ny= new LatLng(40.712784, -74.005941);
     marker = googleMap.addMarker(new MarkerOptions().position(sg).title("New York,NY"));

// Enable my location button
    googleMap.getUiSettings().setMyLocationButtonEnabled(true);

    // disable the zooming button
    googleMap.getUiSettings().setZoomControlsEnabled(false);

}

private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map1)).getMap();

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();

        }
    }
}

@Override
protected void onResume() {
    super.onResume();
    initilizeMap();
}

}

activity_map.xml

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

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="2dp"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/map1"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
   </LinearLayout>

   </LinearLayout>

1 个答案:

答案 0 :(得分:1)

您需要指定布局

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    ...

AllMap活动的onCreate()方法内部。在您的initializeMap()方法中,无法找到片段R.id.map1,因此您获得了NullPointerException