启动Mapfragment / Polyline NullpointException

时间:2015-11-03 09:15:42

标签: java android google-maps android-fragments

我尝试在地图中绘制折线。我的mapfragment放在一个片段上。我想从我的活动中启动polylinedraw并在以下位置获取NullpointException:

GoogleMap map = mapFragment.getMap();

我的活动:

public void buttonMaps_Click(View view) {
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1);
    GoogleMap map = mapFragment.getMap();
    Polyline line = map1.addPolyline(
            new PolylineOptions().add(mP0, mP1, mP2, mP3, mP4, mP5, mP6, mP7, mP8).width(5).color(Color.BLUE));
}

我的片段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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"    >
        <fragment
        android:id="@+id/map1"   
        android:name="com.google.android.gms.maps.SupportMapFragment"  
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

活动的很大一部分:

package com.Dennis.pagers_adapter;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

import Classes.Chronometer;
import Classes.Function;
import Classes.IntentIDs;
import Classes.MyLocation;
import Classes.Time;
import Fragment.FifthFragment;
import Fragment.FourthFragment;
import android.content.Context;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

public class ThirdActivity extends FragmentActivity
        implements OnTabChangeListener, OnPageChangeListener, LocationListener {

    Time mTime;
    Function mFinishLine, mNewLine;
    MyLocation mFinishLoc1, mFinishLoc2, mLocation;
    LocationManager mLocMan;
    List<MyLocation> mLocList = new ArrayList<MyLocation>();
    int i = 0, mRoundCounter = 0, mIntersects, mCount;
    boolean mOnDistance1, mOnDistance2, mBigEnough;
    String mInst1, mInst2, mInst3, mInst4;
    long mInsta1, mInsta2, mInsta3, mInsta4;

    double mSaveTime, mStartTime, mMillis;
    long seconds = 0, millis = 0;
    Timer timer;

    MyLocation[] mFinishLoc;

    MyPageAdapter pageAdapter;
    private ViewPager mViewPager;
    private TabHost mTabHost;
    boolean mClicked = true;
    Chronometer mChronometer;
    private GoogleMap map1;

    double mP0x = 11.13053373992443;
    double mP0y = 49.46026297311668;

    double mP1x = 11.130577996373177;
    double mP1y = 49.46029696881047;

    double mP2x = 11.130626276135445;
    double mP2y = 49.460328349429936;

    double mP3x = 11.130714789032936;
    double mP3y = 49.46035624329705;

    double mP4x = 11.131033971905708;
    double mP4y = 49.46023072076999;

    double mP5x = 11.131257936358452;
    double mP5y = 49.4600973527327;

    double mP6x = 11.13104335963726;
    double mP6y = 49.45994393545658;

    double mP7x = 11.130516305565834;
    double mP7y = 49.460228977399304;

    double mP8x = 11.130727;
    double mP8y = 49.460385;

    LatLng mP0 = new LatLng(mP0y, mP0x);
    LatLng mP1 = new LatLng(mP1y, mP1x);
    LatLng mP2 = new LatLng(mP2y, mP2x);
    LatLng mP3 = new LatLng(mP3y, mP3x);
    LatLng mP4 = new LatLng(mP4y, mP4x);
    LatLng mP5 = new LatLng(mP5y, mP5x);
    LatLng mP6 = new LatLng(mP6y, mP6x);
    LatLng mP7 = new LatLng(mP7y, mP7x);
    LatLng mP8 = new LatLng(mP8y, mP8x);

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

        Bundle b = getIntent().getExtras();

        double mValue = b.getDouble(IntentIDs.INTENT_BUNDLE_LONGITUDE);
        double mValue1 = b.getDouble(IntentIDs.INTENT_BUNDLE_LATITUDE);
        double mRiseV = b.getDouble("RiseV");

        mViewPager = (ViewPager) findViewById(R.id.viewpager);

        // Tab Initialization
        initialiseTabHost();

        // Fragments and ViewPager Initialization
        List<Fragment> fragments = getFragments();
        pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
        mViewPager.setAdapter(pageAdapter);
        mViewPager.setOnPageChangeListener(ThirdActivity.this);

        // 2
        mTime = new Time();
        MyLocation mStartLocation = new MyLocation(mValue, mValue1, mTime);
        mFinishLine = new Function(mRiseV, mStartLocation);
        mFinishLoc = MyLocation.finishLine(mStartLocation, mFinishLine);
        mFinishLoc1 = mFinishLoc[0];
        mFinishLoc2 = mFinishLoc[1];

    }

    public void buttonChrono_Click(View view) {

        mChronometer = (Chronometer) findViewById(R.id.chronometer);
        mChronometer.start();
        mClicked = true;

    }

    public void buttonChrono_Click2(View view) {
        mChronometer = (Chronometer) findViewById(R.id.chronometer);
        mChronometer.stop();

    }

    public void buttonChrono_Click3(View view) {
        mChronometer = (Chronometer) findViewById(R.id.chronometer);
        mChronometer.setStarted(mClicked);

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub

        super.onResume();

        mLocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        mLocMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        mLocMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0f, this);

    }

    public void buttonMaps_Click(View view) {
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1);
        GoogleMap map = mapFragment.getMap();
        // map1 = ((SupportMapFragment)
        // getSupportFragmentManager().findFragmentById(R.id.map1)).getMap();
        Polyline line = map1.addPolyline(
                new PolylineOptions().add(mP0, mP1, mP2, mP3, mP4, mP5, mP6, mP7, mP8).width(5).color(Color.BLUE));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.third, menu);
        return true;
    }

0 个答案:

没有答案