I am working on a tutorial found on this link:
http://www.androidhive.info/2012/08/android-working-with-google-places-and-maps-tutorial/
I am getting the following error on clicking the button:
08-13 12:28:03.226: E/AndroidRuntime(9180): FATAL EXCEPTION: main
08-13 12:28:03.226: E/AndroidRuntime(9180): java.lang.NoClassDefFoundError: com.androidhive.googleplacesandmaps.PlacesMapActivity
08-13 12:28:03.226: E/AndroidRuntime(9180): at com.androidhive.googleplacesandmaps.MainActivity$1.onClick(MainActivity.java:110)
08-13 12:28:03.226: E/AndroidRuntime(9180): at android.view.View.performClick(View.java:2538)
08-13 12:28:03.226: E/AndroidRuntime(9180): at android.view.View$PerformClick.run(View.java:9152)
08-13 12:28:03.226: E/AndroidRuntime(9180): at android.os.Handler.handleCallback(Handler.java:587)
08-13 12:28:03.226: E/AndroidRuntime(9180): at android.os.Handler.dispatchMessage(Handler.java:92)
08-13 12:28:03.226: E/AndroidRuntime(9180): at android.os.Looper.loop(Looper.java:130)
08-13 12:28:03.226: E/AndroidRuntime(9180): at android.app.ActivityThread.main(ActivityThread.java:3689)
08-13 12:28:03.226: E/AndroidRuntime(9180): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 12:28:03.226: E/AndroidRuntime(9180): at java.lang.reflect.Method.invoke(Method.java:507)
08-13 12:28:03.226: E/AndroidRuntime(9180): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
08-13 12:28:03.226: E/AndroidRuntime(9180): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-13 12:28:03.226: E/AndroidRuntime(9180): at dalvik.system.NativeStart.main(Native Method)
08-13 12:28:03.226: E/AndroidRuntime(9180): Caused by: java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
08-13 12:28:03.226: E/AndroidRuntime(9180): at dalvik.system.DexFile.defineClass(Native Method)
08-13 12:28:03.226: E/AndroidRuntime(9180): at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:207)
08-13 12:28:03.226: E/AndroidRuntime(9180): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:200)
08-13 12:28:03.226: E/AndroidRuntime(9180): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
08-13 12:28:03.226: E/AndroidRuntime(9180): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
08-13 12:28:03.226: E/AndroidRuntime(9180): ... 12 more
The placesmapactivity.java class is shown below:
package com.androidhive.googleplacesandmaps;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class PlacesMapActivity extends MapActivity {
// Nearest places
PlacesList nearPlaces;
// Map view
MapView mapView;
// Map overlay items
List<Overlay> mapOverlays;
AddItemizedOverlay itemizedOverlay;
GeoPoint geoPoint;
// Map controllers
MapController mc;
double latitude;
double longitude;
OverlayItem overlayitem;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_places);
// Getting intent data
Intent i = getIntent();
// Users current geo location
String user_latitude = i.getStringExtra("user_latitude");
String user_longitude = i.getStringExtra("user_longitude");
// Nearplaces list
nearPlaces = (PlacesList) i.getSerializableExtra("near_places");
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
// Geopoint to place on map
geoPoint = new GeoPoint((int) (Double.parseDouble(user_latitude) * 1E6),
(int) (Double.parseDouble(user_longitude) * 1E6));
// Drawable marker icon
Drawable drawable_user = this.getResources()
.getDrawable(R.drawable.mark_red);
itemizedOverlay = new AddItemizedOverlay(drawable_user, this);
// Map overlay item
overlayitem = new OverlayItem(geoPoint, "Your Location",
"That is you!");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
itemizedOverlay.populateNow();
// Drawable marker icon
Drawable drawable = this.getResources()
.getDrawable(R.drawable.mark_blue);
itemizedOverlay = new AddItemizedOverlay(drawable, this);
mc = mapView.getController();
// These values are used to get map boundary area
// The area where you can see all the markers on screen
int minLat = Integer.MAX_VALUE;
int minLong = Integer.MAX_VALUE;
int maxLat = Integer.MIN_VALUE;
int maxLong = Integer.MIN_VALUE;
// check for null in case it is null
if (nearPlaces.results != null) {
// loop through all the places
for (Place place : nearPlaces.results) {
latitude = place.geometry.location.lat; // latitude
longitude = place.geometry.location.lng; // longitude
// Geopoint to place on map
geoPoint = new GeoPoint((int) (latitude * 1E6),
(int) (longitude * 1E6));
// Map overlay item
overlayitem = new OverlayItem(geoPoint, place.name,
place.vicinity);
itemizedOverlay.addOverlay(overlayitem);
// calculating map boundary area
minLat = (int) Math.min( geoPoint.getLatitudeE6(), minLat );
minLong = (int) Math.min( geoPoint.getLongitudeE6(), minLong);
maxLat = (int) Math.max( geoPoint.getLatitudeE6(), maxLat );
maxLong = (int) Math.max( geoPoint.getLongitudeE6(), maxLong );
}
mapOverlays.add(itemizedOverlay);
// showing all overlay items
itemizedOverlay.populateNow();
}
// Adjusting the zoom level so that you can see all the markers on map
mapView.getController().zoomToSpan(Math.abs( minLat - maxLat ), Math.abs( minLong - maxLong ));
// Showing the center of the map
mc.animateTo(new GeoPoint((maxLat + minLat)/2, (maxLong + minLong)/2 ));
mapView.postInvalidate();
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
The button click code is shown below:
btnShowOnMap.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(MainActivity.this,
PlacesMapActivity.class);
// Sending user current geo location
i.putExtra("user_latitude", Double.toString(gps.getLatitude()));
i.putExtra("user_longitude", Double.toString(gps.getLongitude()));
// passing near places to map activity
i.putExtra("near_places", nearPlaces);
// staring activity
startActivity(i);
}
});
}
Related
I've made a simple accelerometer app and I want to make a companion widget. But every time I add a sensor to my widgetprovider I get the following error:
07-25 10:31:01.337: E/AndroidRuntime(8908): FATAL EXCEPTION: main
07-25 10:31:01.337: E/AndroidRuntime(8908):
java.lang.RuntimeException: Unable to start receiver
com.example.axelo.AxeloAppWidgetProvider:
java.lang.NullPointerException 07-25 10:31:01.337:
E/AndroidRuntime(8908): at
android.app.ActivityThread.handleReceiver(ActivityThread.java:2153)
07-25 10:31:01.337: E/AndroidRuntime(8908): at
android.app.ActivityThread.access$1500(ActivityThread.java:127) 07-25
10:31:01.337: E/AndroidRuntime(8908): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
07-25 10:31:01.337: E/AndroidRuntime(8908): at
android.os.Handler.dispatchMessage(Handler.java:99) 07-25
10:31:01.337: E/AndroidRuntime(8908): at
android.os.Looper.loop(Looper.java:137) 07-25 10:31:01.337:
E/AndroidRuntime(8908): at
android.app.ActivityThread.main(ActivityThread.java:4448) 07-25
10:31:01.337: E/AndroidRuntime(8908): at
java.lang.reflect.Method.invokeNative(Native Method) 07-25
10:31:01.337: E/AndroidRuntime(8908): at
java.lang.reflect.Method.invoke(Method.java:511) 07-25 10:31:01.337:
E/AndroidRuntime(8908): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
07-25 10:31:01.337: E/AndroidRuntime(8908): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590) 07-25
10:31:01.337: E/AndroidRuntime(8908): at
dalvik.system.NativeStart.main(Native Method) 07-25 10:31:01.337:
E/AndroidRuntime(8908): Caused by: java.lang.NullPointerException
07-25 10:31:01.337: E/AndroidRuntime(8908): at
com.example.axelo.AxeloAppWidgetProvider.onUpdate(AxeloAppWidgetProvider.java:29)
07-25 10:31:01.337: E/AndroidRuntime(8908): at
android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66)
07-25 10:31:01.337: E/AndroidRuntime(8908): at
android.app.ActivityThread.handleReceiver(ActivityThread.java:2146)
07-25 10:31:01.337: E/AndroidRuntime(8908): ... 10 more
This is what my widget class looks like
package com.example.axelo;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.TextView;
public class AxeloAppWidgetProvider extends AppWidgetProvider implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mSensor;
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null){
Log.v("SENSOR_SERVICE","accelerometer found!");
}
else {
Log.v("SENSOR_SERVICE","Not found!");
}
// Create an Intent to launch MainActivity
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.axelo_widget);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
// // Show changes on screen.
views.setTextViewText(R.id.coord_X, Float.toString(linear_acceleration[0]));
views.setTextViewText(R.id.coord_Y, Float.toString(linear_acceleration[1]));
views.setTextViewText(R.id.coord_Z, Float.toString(linear_acceleration[2]));
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
#Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
float[] gravity={(float) 9.81,(float) 9.81,(float) 9.81};
float[] linear_acceleration=new float[3];
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
final float alpha = (float) 0.8;
Log.v("SENSOR_SERVICE", "on sensor change");
// Isolate the force of gravity with the low-pass filter.
gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
// Remove the gravity contribution with the high-pass filter.
linear_acceleration[0] = event.values[0] - gravity[0];
linear_acceleration[1] = event.values[1] - gravity[1];
linear_acceleration[2] = event.values[2] - gravity[2];
}
}
Please tell me where I'm going wrong or is there no way to add a sensor to the widget?
I think that sensor widget will not be applicable this way, you should extends Activity instead of AppWidgetProvider and do not forget registerListener/unregisterListener, you can do something more you can add activity class to the widget manifest and put your code within this class as normal, from widget you can setOnClickPendingIntent a button to this Activity.
I'm receiving a weird IAE during one of my activities.
08-13 16:24:59.991: E/AndroidRuntime(924): FATAL EXCEPTION: main
08-13 16:24:59.991: E/AndroidRuntime(924): java.lang.IllegalArgumentException: text cannot be null
08-13 16:24:59.991: E/AndroidRuntime(924): at android.graphics.Paint.measureText(Paint.java:1281)
08-13 16:24:59.991: E/AndroidRuntime(924): at com.echo.holographlibrary.BarGraph.onDraw(BarGraph.java:151)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.View.draw(View.java:10880)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.drawChild(ViewGroup.java:2899)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2495)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.drawChild(ViewGroup.java:2897)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2495)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.View.draw(View.java:10883)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.widget.FrameLayout.draw(FrameLayout.java:450)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.widget.ScrollView.draw(ScrollView.java:1524)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.drawChild(ViewGroup.java:2899)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2495)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.View.draw(View.java:10883)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.widget.FrameLayout.draw(FrameLayout.java:450)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.drawChild(ViewGroup.java:2899)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2495)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.drawChild(ViewGroup.java:2897)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2495)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.View.draw(View.java:10883)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.widget.FrameLayout.draw(FrameLayout.java:450)
08-13 16:24:59.991: E/AndroidRuntime(924): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2106)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2005)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1613)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.os.Looper.loop(Looper.java:137)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.app.ActivityThread.main(ActivityThread.java:4340)
08-13 16:24:59.991: E/AndroidRuntime(924): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 16:24:59.991: E/AndroidRuntime(924): at java.lang.reflect.Method.invoke(Method.java:511)
08-13 16:24:59.991: E/AndroidRuntime(924): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-13 16:24:59.991: E/AndroidRuntime(924): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-13 16:24:59.991: E/AndroidRuntime(924): at dalvik.system.NativeStart.main(Native Method)
08-13 16:29:41.041: I/Process(924): Sending signal. PID: 924 SIG: 9
This worked fine before but now that I've used some dynamic adding to xml, it seemed to have crashed. Below are the calls, and I've been unable to catch this exception.
package com.example.exercise;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Menu;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import com.echo.holographlibrary.Bar;
import com.echo.holographlibrary.BarGraph;
public class GraphDetail extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_graph_detail);
try{
boolean done = false;
int begin = 0;
int count = 4;
while(!done)
{
if(count>=MainActivity.test.size())
{
done = true;
break;
}
ArrayList<Bar> points = new ArrayList<Bar>();
Intent intent1 = getIntent();
int max = (intent1.getIntExtra("max", 0));
int[] weight = new int[MainActivity.test.size()];
for(int i=begin; i<count; i++)
{
weight[i] = MainActivity.test.get(i).getWeight();
}
String[] names = new String[MainActivity.test.size()];
for(int i = begin; i<count; i++)
{
names[i] = MainActivity.test.get(i).getName();
}
for(int i = 0; i< weight.length; i++)
{
Bar d = new Bar();
if(weight[i]!=max)
d.setColor(Color.parseColor("#99CC00"));
else
{
d.setColor(Color.parseColor("#FFBB33"));
}
d.setName(names[i]);
d.setValue(weight[i]);
points.add(d);
}
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 130, r.getDisplayMetrics());
RelativeLayout rl = (RelativeLayout)findViewById(R.id.container);
int px1 = (int)px;
BarGraph bar = new BarGraph(this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
px1);
bar.setBars(points);
bar.setUnit("0");
bar.appendUnit(true);
rl.addView(bar, lp);
begin+=count;
count+=3;
if(count>=MainActivity.test.size())
{
done = true;
break;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
// ArrayList<Bar> points = new ArrayList<Bar>();
// ArrayList<Bar> points1 = new ArrayList<Bar>();
// ArrayList<Bar> points2 = new ArrayList<Bar>();
// Intent intent1 = getIntent();
// int max = (intent1.getIntExtra("max",0));
// int[] weight = new int[MainActivity.test.size()];
// for(int i = 0; i<MainActivity.test.size(); i++)
// {
// weight[i] = MainActivity.test.get(i).getWeight();
// }
// String[] names = new String[MainActivity.test.size()];
// for(int i = 0; i<MainActivity.test.size(); i++)
// {
// names[i] = MainActivity.test.get(i).getName();
// }
// for(int i = 0; i< weight.length; i++)
// {
// Bar d = new Bar();
// if(weight[i]!=max)
// d.setColor(Color.parseColor("#99CC00"));
// else
// {
// d.setColor(Color.parseColor("#FFBB33"));
// }
//
// d.setName(names[i]);
// d.setValue(weight[i]);
// if(i<4)
// points.add(d);
// if(i>=4 && i<8)
// points1.add(d);
// if(i>=8)
// points2.add(d);
// }
// BarGraph g = (BarGraph) findViewById(R.id.graph);
// g.setBars(points);
// BarGraph g1 = (BarGraph) findViewById(R.id.graph1);
// g1.setBars(points1);
// BarGraph g2 = (BarGraph) findViewById(R.id.graph2);
// g2.setBars(points2);
// g.setUnit("0");
// g.appendUnit(true);
// g1.setUnit("0");
// g1.appendUnit(true);
// g2.setUnit("0");
// g2.appendUnit(true);
//
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.graph_detail, menu);
return true;
}
}
String[] names = new String[MainActivity.test.size()];
for(int i = begin; i<count; i++)
{
names[i] = MainActivity.test.get(i).getName();
}
...
d.setName(names[i]);
...
bar.setBars(points);
Have you checked the length of your names array versus count? It's likely that you are generating an array that is larger than the size of count, and because of that you're passing null later to d.setName, which ends up causing an exception later when calling bar.setBars.
I am currently working on the new Google Maps Android API v2.
My MapActivity class is working with my Samsung galaxy s3 ans s2 with the code below:
However, there are some particular devices are not working such as: GT-S5830i
class MapActivity extends FragmentActivity implements OnMapClickListener, OnMapLongClickListener, OnMarkerClickListener
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
tvLocInfo = (TextView)findViewById(R.id.locinfo);
android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment myMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = myMapFragment.getMap();
myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
ArrayList<Cooridnates> cooridnatesList = MainActivity.getList();
for(int i=0;i<cooridnatesList.size();i++)
{
//Toast.makeText(MapActivity.this, "SIZE : " + " " + cooridnatesList.size(), Toast.LENGTH_LONG).show();
LatLng point = new LatLng(cooridnatesList.get(i).getLat(),cooridnatesList.get(i).getLon());
tvLocInfo.setText("markers added");
myMap.addMarker(new MarkerOptions().position(point).title(point.toString()));
center=CameraUpdateFactory.newLatLng(point);
}
CameraUpdate zoom=CameraUpdateFactory.zoomTo(16);
myMap.moveCamera(center);
myMap.animateCamera(zoom);
myMap.setOnMapClickListener(this);
myMap.setOnMarkerClickListener(this);
markerClicked = false;
}
For some reason, I am getting this error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.coldice.plotfinder/com.coldice.plotfinder.MapActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source)
at com.coldice.plotfinder.MapActivity.onCreate(MapActivity.java:70)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more
You function MainActivity.getList() probably returned an empty list, so for loop doesn't execute center=CameraUpdateFactory.newLatLng(point);.
it seems center is null in this >> myMap.moveCamera(center);, make an appropriate check on this variable before passing it in moveCamera method.
It seems problem is here. center might have null value, So to get proper explanation debug your application.
center is initialzed here.
center=CameraUpdateFactory.newLatLng(point);
It might assign null in center.
myMap.moveCamera(center); <<<PROBLEM is here
if(center != null )
myMap.moveCamera(center);
This is my code:
Line no. 94 corresponds to my method of getting all ids.
Always I get an error : Your application has stopped unexpectedly.
package karan.app.caloriecalculator;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class Main extends Activity implements AdapterView.OnItemSelectedListener {
Button bcal;
TextView intro, choose, duration, min, weight, kg;
EditText dur, weigh;
Spinner sel;
String temp;
public double w, d, calories;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
get_all_ids();
setArrayAdapter();
sel.setOnItemSelectedListener(this);
bcal.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
w = Float.valueOf(weigh.getText().toString());
d = Float.valueOf(dur.getText().toString());
if (temp.contentEquals("Aerobics:general")){
calories = 0.11*w*d;
}
else if (temp.contentEquals("Badminton")){
calories = (w/13)*d;
}
else if (temp.contentEquals("Basketball")){
calories = (w/7.45)*d;
}
else if (temp.contentEquals("Bicycling:slow (10-12 mph)")){
calories = (w/9.65)*d;
}
else if (temp.contentEquals("Bicycling:general (12-14 mph)")){
calories = (w/7.2)*d;
}
else if (temp.contentEquals("Bicycling:moderate (14-16 mph)")){
calories = (w/6)*d;
}
else if (temp.contentEquals("Bicycling:fast (16+ mph)")){
calories = (w/5)*d;
}
else if (temp.contentEquals("Bowling")){
calories = (w/19.1)*d;
}
else if (temp.contentEquals("Boxing: punching bag")){
calories = (w/9.6)*d;
}
else if (temp.contentEquals("Boxing: sparring")){
calories = (w/6.375)*d;
}
/* Thread t = new Thread(){
public void run(){
try{
sleep(1);
}
catch (InterruptedException e){
e.printStackTrace();
}
finally{
Intent openResult = new Intent("karan.app.caloriecalculator.RESULT");
startActivity(openResult);
}
}
}; // close the thread with a ;
*/
}});
}
public void get_all_ids() {
bcal.findViewById(R.id.b1);
intro.findViewById(R.id.tv1);
choose.findViewById(R.id.tv2);
duration.findViewById(R.id.tv3);
min.findViewById(R.id.tv4);
weight.findViewById(R.id.tv5);
kg.findViewById(R.id.tv6);
dur.findViewById(R.id.et2);
weigh.findViewById(R.id.et1);
sel.findViewById(R.id.sp1);
}
public double getCalories() {
return calories;
}
public void setArrayAdapter(){
//The createFromResource() method allows you to create an ArrayAdapter from the string array
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.choices, android.R.layout.simple_spinner_dropdown_item);
//You should then call setDropDownViewResource(int) to specify the layout the adapter should use to display the list of spinner choices
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Call setAdapter() to apply the adapter to your Spinner.
sel.setAdapter(adapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Object item = parent.getItemAtPosition(position);
temp = item.toString();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
LogCat:
07-28 14:10:03.545: E/AndroidRuntime(266): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-28 14:10:03.545: E/AndroidRuntime(266): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-28 14:10:03.545: E/AndroidRuntime(266): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-28 14:10:03.545: E/AndroidRuntime(266): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-28 14:10:03.545: E/AndroidRuntime(266): at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 14:10:03.545: E/AndroidRuntime(266): at android.os.Looper.loop(Looper.java:123)
07-28 14:10:03.545: E/AndroidRuntime(266): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-28 14:10:03.545: E/AndroidRuntime(266): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 14:10:03.545: E/AndroidRuntime(266): at java.lang.reflect.Method.invoke(Method.java:521)
07-28 14:10:03.545: E/AndroidRuntime(266): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-28 14:10:03.545: E/AndroidRuntime(266): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-28 14:10:03.545: E/AndroidRuntime(266): at dalvik.system.NativeStart.main(Native Method)
07-28 14:10:03.545: E/AndroidRuntime(266): Caused by: java.lang.NullPointerException
07-28 14:10:03.545: E/AndroidRuntime(266): at karan.app.caloriecalc.MainActivity.get_all_ids(MainActivity.java:96)
07-28 14:10:03.545: E/AndroidRuntime(266): at karan.app.caloriecalc.MainActivity.onCreate(MainActivity.java:28)
07-28 14:10:03.545: E/AndroidRuntime(266): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-28 14:10:03.545: E/AndroidRuntime(266): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-28 14:10:03.545: E/AndroidRuntime(266): ... 11 more
You are incorrectly fetching views in get_all_ids().
To get a view, you must do:
TextView intro;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
intro = (TextView) findViewById(R.id.tv1);
....
}
// You can now use intro and methods of TextView class.
I recommend you read the Android Beginner Guides.
Use My location Latitude Longitude and DB Latitude Longitude Calculation Distance.but always error "The application AndroidGoogleMaps(process com.androidhive.googlemaps)has stopped unexpectedly.Please try again."
public class AndroidGoogleMapsActivity extends MapActivity {
public int mm[][]= new int[100][100];
double[][] rtdist= new double[50][2];
double[][] okdist= new double[50][2];
public GeoPoint[] endpp = new GeoPoint[100];
private static String KEY_SUCCESS = "success";
private MyLocationOverlay mylayer;
private MapController mapController;
private MapView mapView;
protected GeoPoint geoPoint;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
setupMap();
}
private void findViews() {
mapView = (MapView) findViewById(R.id.mapView);
mapController = mapView.getController();
mapView.setBuiltInZoomControls(true);
}
private void setupMap() {
List<Overlay> overlays = mapView.getOverlays();
mylayer = new MyLocationOverlay(this, mapView);
mylayer.runOnFirstFix(new Runnable() {
public void run() {
// Zoom in to current location
mapView.setTraffic(true);
mapController.setZoom(17);
mapController.animateTo(mylayer.getMyLocation());
}
});
overlays.add(mylayer);
GeoPoint startpp = mylayer.getMyLocation();
String b="SELECT lat,lng FROM markers";
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.execqlcmd(b);
try {
if (json.getString(KEY_SUCCESS) != null)
{
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1)
{
for(int i=0 ; i<100 ; i++) {
JSONObject json_row = json.getJSONObject("r"+i);
mm[i][0]=(int)(Double.parseDouble(json_row.getString("c0")) * 1E6) ;
mm[i][1]=(int)(Double.parseDouble(json_row.getString("c1")) * 1E6) ;
endpp[i] =new GeoPoint(mm[i][0], mm[i][1] );
MapController mc = mapView.getController();
DistanceCalculator caldist= new DistanceCalculator(6371);
for(int j = 0; j < 2; j++)
{
rtdist[j][0]=1;
rtdist[j][1]=caldist.CalculationByDistance(startpp, endpp[j]);
//startpp The location of the phone is
//endpp DB's lat lng
//To judge the results of this array is in line with the distance required by the user
if(rtdist[j][1]<=50)
{
okdist[j][0]=rtdist[j][0]; //The amount of store
okdist[j][0]=rtdist[j][1]; //Store the distance
}
}
mc.animateTo(geoPoint);
mc.setZoom(15);
mapView.invalidate();
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.mark_red);
AddItemizedOverlay itemizedOverlay =
new AddItemizedOverlay(drawable, this);
OverlayItem overlayitem = new OverlayItem(geoPoint, "Hello", "Sample Overlay item");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
}}}}
catch (JSONException e) {
e.printStackTrace();
}
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mylayer.enableMyLocation();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mylayer.disableMyLocation();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
public class DistanceCalculator {
private double Radius;
// R = earth's radius (mean radius = 6,371km)
// Constructor
DistanceCalculator(double R) {
Radius = R;
}
public double CalculationByDistance(GeoPoint startpp, GeoPoint endpp) {
double lat1 = startpp.getLatitudeE6()/1E6;
double lat2 = endpp.getLatitudeE6()/1E6;
double lon1 = startpp.getLongitudeE6()/1E6;
double lon2 = endpp.getLongitudeE6()/1E6;
double dLat = Math.toRadians(lat2-lat1);
double dLon = Math.toRadians(lon2-lon1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.asin(Math.sqrt(a));
return Radius * c;
}
}
}
LogCat :
05-13 14:49:44.703: D/dalvikvm(303): GC_FOR_MALLOC freed 4358 objects / 275032 bytes in 316ms
05-13 14:49:44.903: D/dalvikvm(303): GC_FOR_MALLOC freed 10567 objects / 641664 bytes in 58ms
05-13 14:49:45.063: D/dalvikvm(303): GC_FOR_MALLOC freed 4727 objects / 310440 bytes in 45ms
05-13 14:49:45.243: D/dalvikvm(303): GC_FOR_MALLOC freed 6480 objects / 394584 bytes in 48ms
05-13 14:49:45.443: D/dalvikvm(303): GC_FOR_MALLOC freed 7949 objects / 616640 bytes in 51ms
05-13 14:49:45.623: D/dalvikvm(303): GC_FOR_MALLOC freed 6072 objects / 370040 bytes in 51ms
05-13 14:49:45.813: D/dalvikvm(303): GC_FOR_MALLOC freed 4449 objects / 354128 bytes in 62ms
05-13 14:49:45.823: I/dalvikvm-heap(303): Grow heap (frag case) to 3.048MB for 87396-byte allocation
05-13 14:49:45.884: D/dalvikvm(303): GC_FOR_MALLOC freed 45 objects / 2392 bytes in 66ms
05-13 14:49:45.953: D/dalvikvm(303): GC_FOR_MALLOC freed 2 objects / 80 bytes in 67ms
05-13 14:49:45.953: I/dalvikvm-heap(303): Grow heap (frag case) to 3.129MB for 87396-byte allocation
05-13 14:49:46.013: D/dalvikvm(303): GC_FOR_MALLOC freed 0 objects / 0 bytes in 58ms
05-13 14:49:46.273: E/JSON(303): {"tag":"exesqlcmd","success":1,"error":0,"rownum":2,"colnum":2,"r0":{"c0":"23.973728","c1":"121.583641"},"r1":{"c0":"23.973827","c1":"121.585625"}}
05-13 14:49:46.293: D/AndroidRuntime(303): Shutting down VM
05-13 14:49:46.293: W/dalvikvm(303): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-13 14:49:46.313: E/AndroidRuntime(303): FATAL EXCEPTION: main
05-13 14:49:46.313: E/AndroidRuntime(303): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.googlemaps/com.androidhive.googlemaps.AndroidGoogleMapsActivity}: java.lang.NullPointerException
05-13 14:49:46.313: E/AndroidRuntime(303): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-13 14:49:46.313: E/AndroidRuntime(303): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-13 14:49:46.313: E/AndroidRuntime(303): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-13 14:49:46.313: E/AndroidRuntime(303): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-13 14:49:46.313: E/AndroidRuntime(303): at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 14:49:46.313: E/AndroidRuntime(303): at android.os.Looper.loop(Looper.java:123)
05-13 14:49:46.313: E/AndroidRuntime(303): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-13 14:49:46.313: E/AndroidRuntime(303): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 14:49:46.313: E/AndroidRuntime(303): at java.lang.reflect.Method.invoke(Method.java:521)
05-13 14:49:46.313: E/AndroidRuntime(303): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-13 14:49:46.313: E/AndroidRuntime(303): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-13 14:49:46.313: E/AndroidRuntime(303): at dalvik.system.NativeStart.main(Native Method)
05-13 14:49:46.313: E/AndroidRuntime(303): Caused by: java.lang.NullPointerException
05-13 14:49:46.313: E/AndroidRuntime(303): at com.androidhive.googlemaps.AndroidGoogleMapsActivity$DistanceCalculator.CalculationByDistance(AndroidGoogleMapsActivity.java:178)
05-13 14:49:46.313: E/AndroidRuntime(303): at com.androidhive.googlemaps.AndroidGoogleMapsActivity.setupMap(AndroidGoogleMapsActivity.java:106)
05-13 14:49:46.313: E/AndroidRuntime(303): at com.androidhive.googlemaps.AndroidGoogleMapsActivity.onCreate(AndroidGoogleMapsActivity.java:41)
05-13 14:49:46.313: E/AndroidRuntime(303): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-13 14:49:46.313: E/AndroidRuntime(303): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-13 14:49:46.313: E/AndroidRuntime(303): ... 11 more
if you want to calculate distance between two geographic point you have to see those two methods in the Location Class
Location Class
you can use it like this:
Location locationA = new Location("point A");
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location("point B");
locationB.setLatitude(latB);
LocationB.setLongitude(lngB);
distance = locationA.distanceTo(locationB);