I am trying to make a simple app that has a spinner , a button and google map .. All Was working fine.. I recently added Google Map in my app.. Google map is showing fine..The Problem is when i press Start Button The Value selected from spinner is saved in a variable and Current location is saved Using LocationManager and added to google map for drawing a line from previous locarion to new.. But As per the logcat It says NullPointerException On Google Map. Please have a look:
MainActivity.java
package com.example.akshay.bus;
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.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
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.PolylineOptions;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener {
Button start;
public static final String URL = "http://www.akshay.site90.net/sendnote.php";
Context context = MainActivity.this;
Spinner spinner;
int ROUTE;
Double LAT, LONG;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.button_START);
start.setOnClickListener(this);
spinner = (Spinner) findViewById(R.id.SPINNER_ROUTE);
List<String> route = new ArrayList<String>();
route.add("Jagadhari");
route.add("Yamunanagar");
route.add("Radaur");
route.add("Workshop");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, route);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(MainActivity.this);
}
@Override
public void onClick(View v) {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
/* googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();*/
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
LAT = location.getLatitude();
LONG = location.getLongitude();
Log.e("====LOCATION UPDATE====", "LAT==" + String.valueOf(LAT) + "LONG==" + String.valueOf(LONG));
LatLng mylatlng = new LatLng(LAT, LONG);
LatLng prev = null;
int flag = 0;
if (flag == 0) {
prev = mylatlng;
flag = 1;
}
GoogleMap googleMap = null;
if (googleMap == null)
{
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
}
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(mylatlng, 13);
googleMap.animateCamera(update);
googleMap.addPolyline((new PolylineOptions()).add(prev, mylatlng).width(6).color(Color.BLUE).visible(true));
/* List<Double> LAT = new ArrayList<Double>();
LAT.add(location.getLatitude());
List<Double>LONG = new ArrayList<Double>();
LONG.add(location.getLongitude());*/
prev = mylatlng;
mylatlng = null;
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
});
new BackgroundTask(URL, context, ROUTE).execute();
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getItemAtPosition(position).toString()) {
case "Jagadhari":
ROUTE = 401;
Toast.makeText(MainActivity.this, String.valueOf(ROUTE), Toast.LENGTH_SHORT).show();
break;
case "Yamunanagar":
ROUTE = 402;
Toast.makeText(MainActivity.this, String.valueOf(ROUTE), Toast.LENGTH_SHORT).show();
break;
case "Radaur":
ROUTE = 403;
Toast.makeText(MainActivity.this, String.valueOf(ROUTE), Toast.LENGTH_SHORT).show();
break;
case "Workshop":
ROUTE = 404;
Toast.makeText(MainActivity.this, String.valueOf(ROUTE), Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
acivity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:name="http://schemas.android.com/apk/res-auto"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:weightSum="5">
<Spinner
android:layout_weight="1"
android:id="@+id/SPINNER_ROUTE"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Spinner>
<Button
android:layout_weight="1"
android:id="@+id/button_START"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Start" />
<fragment
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="fill_parent"
android:layout_height="700dp"
android:id="@+id/map">
</fragment>
</LinearLayout>
LogCat:
09-06 20:24:38.780 8112-8196/com.example.akshay.bus E/BufferQueue﹕ [unnamed-8112-0] dequeueBuffer: min undequeued buffer count (2) exceeded (dequeued=2 undequeudCount=1)
09-06 20:24:38.790 8112-8196/com.example.akshay.bus E/BufferQueue﹕ [unnamed-8112-0] dequeueBuffer: min undequeued buffer count (2) exceeded (dequeued=2 undequeudCount=1)
09-06 20:24:38.790 8112-8196/com.example.akshay.bus E/BufferQueue﹕ [unnamed-8112-0] dequeueBuffer: min undequeued buffer count (2) exceeded (dequeued=2 undequeudCount=1)
09-06 20:24:38.790 8112-8196/com.example.akshay.bus E/BufferQueue﹕ [unnamed-8112-0] dequeueBuffer: min undequeued buffer count (2) exceeded (dequeued=2 undequeudCount=1)
09-06 20:24:38.790 8112-8196/com.example.akshay.bus E/BufferQueue﹕ [unnamed-8112-0] dequeueBuffer: min undequeued buffer count (2) exceeded (dequeued=2 undequeudCount=1)
09-06 20:24:38.790 8112-8196/com.example.akshay.bus E/BufferQueue﹕ [unnamed-8112-0] dequeueBuffer: min undequeued buffer count (2) exceeded (dequeued=2 undequeudCount=1)
09-06 20:24:38.790 8112-8196/com.example.akshay.bus E/BufferQueue﹕ [unnamed-8112-0] dequeueBuffer: min undequeued buffer count (2) exceeded (dequeued=2 undequeudCount=1)
09-06 20:24:38.800 8112-8196/com.example.akshay.bus E/BufferQueue﹕ [unnamed-8112-0] dequeueBuffer: min undequeued buffer count (2) exceeded (dequeued=2 undequeudCount=1)
09-06 20:24:53.984 8112-8112/com.example.akshay.bus E/ViewRootImpl﹕ sendUserActionEvent() mView == null
09-06 20:24:57.288 8112-8112/com.example.akshay.bus E/ViewRootImpl﹕ sendUserActionEvent() mView == null
09-06 20:25:31.111 8112-8112/com.example.akshay.bus E/====LOCATION UPDATE====﹕ LAT==30.13956043766201LONG==77.25664889705503
09-06 20:25:31.121 8112-8112/com.example.akshay.bus E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.akshay.bus, PID: 8112
java.lang.NullPointerException
at com.example.akshay.bus.MainActivity$1.onLocationChanged(MainActivity.java:79)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:279)
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:208)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:224)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)