NullPointer MapFragment.getMap()

时间:2013-10-21 02:20:46

标签: java android

所以,我正在尝试创建一个DrawerLayout,当你按下按钮时会启动一个地图片段。我尝试过的所有内容都会抛出一个backstack异常或nullpointer。这是我到目前为止所拥有的。

activity_main.xml中

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_view"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    />

<!--The drawer-->
<ListView android:id="@+id/drawer_view"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#9E9E9E"/>



</android.support.v4.widget.DrawerLayout>

geo_fence.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

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


</LinearLayout>

MainActivity.java

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

import java.util.ArrayList;


public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private SharedPreferences mPreferences;
    private JSONManager jsonManager;
    private PlacesListAdapter adapter;
    private Place currentPlace = null;
    private GoogleMap gmap = null;
    final int RQS_GooglePlayServices = 1;

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

        //Construct the action bar
        ActionBar ab = getActionBar();
        ab.setHomeButtonEnabled(true);
        ab.setDisplayHomeAsUpEnabled(true);


        //Construct the slide out drawer
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.drawer_view);

        createButtons();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        switch(menuItem.getItemId()) {
            case android.R.id.home:
                mDrawerLayout.openDrawer(mDrawerList);
            case R.id.new_action:
                mDrawerLayout.openDrawer(mDrawerList);
            case R.id.remove_place:
                adapter.remove(currentPlace);
                mDrawerLayout.openDrawer(mDrawerList);
        }
        return (super.onOptionsItemSelected(menuItem));
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mPreferences.getBoolean("firstrun", true)) {
            //FIX THIS - needs to run to check for play services
            ProgressDialog showWait = new ProgressDialog(this);
            showWait.setTitle("Initializing");
            showWait.setMessage("Please wait, checking for Google Play Services");
            showWait.show();
            startActivity(new Intent(this, CheckServices.class));
            showWait.dismiss();
            //FIX THIS
            firstRun();
        }

    }

    /*
    Display a map
     */
    private void addPlacesPressed() {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        MapFragment map = (MapFragment)fm.findFragmentById(R.id.map);
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this.getApplicationContext());
        if (resultCode == ConnectionResult.SUCCESS){

            if(gmap == null){
                gmap = map.getMap();
                if(gmap != null){
                    //myMap.setMyLocationEnabled(true);
                    gmap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                }else{
                    Toast.makeText(this.getApplicationContext(),
                            "cannot getMap!",
                            Toast.LENGTH_LONG).show();
                }
            }

        }else{
            GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
        }

        ft = fm.beginTransaction();
        ft.replace(R.id.content_view, map, "map");
        ft.commit();
        mDrawerLayout.closeDrawer(mDrawerList);

    }

    private void createButtons() {
        Button addPlacesButton = new Button(this);
        addPlacesButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        addPlacesPressed();
                    }
                }
        );
    mDrawerList.addView(addPlacesButton);
    }
}

在我尝试对地图片段执行getMap()时,它只会抛出一个nullpointer。我已经将所有必要的东西添加到清单中,如果我在应用程序启动时启动地图,我已经完成了这项工作。当我尝试使用LinearLayout中的MapFragment替换FramLayout时,它就失败了。

0 个答案:

没有答案