I am developing application to get my current location on the map and display the nearest places.
first my application was running but the camera goes at fixed Latlng ( i put the values) but when i change the code to make the camera on my current location .. my phone shutting down.
This is my logcat:
11-09 08:38:51.010: E/AndroidRuntime(1641): FATAL EXCEPTION: main
11-09 08:38:51.010: E/AndroidRuntime(1641): java.lang.NullPointerException
11-09 08:38:51.010: E/AndroidRuntime(1641): at com.example.buttontest.NearbyActivity.onConnected(NearbyActivity.java:219)
11-09 08:38:51.010: E/AndroidRuntime(1641): at com.google.android.gms.internal.u.v(Unknown Source)
11-09 08:38:51.010: E/AndroidRuntime(1641): at com.google.android.gms.internal.u$f.a(Unknown Source)
11-09 08:38:51.010: E/AndroidRuntime(1641): at com.google.android.gms.internal.u$f.a(Unknown Source)
11-09 08:38:51.010: E/AndroidRuntime(1641): at com.google.android.gms.internal.u$b.A(Unknown Source)
11-09 08:38:51.010: E/AndroidRuntime(1641): at com.google.android.gms.internal.u$a.handleMessage(Unknown Source)
11-09 08:38:51.010: E/AndroidRuntime(1641): at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 08:38:51.010: E/AndroidRuntime(1641): at android.os.Looper.loop(Looper.java:137)
11-09 08:38:51.010: E/AndroidRuntime(1641): at android.app.ActivityThread.main(ActivityThread.java:4898)
11-09 08:38:51.010: E/AndroidRuntime(1641): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 08:38:51.010: E/AndroidRuntime(1641): at java.lang.reflect.Method.invoke(Method.java:511)
11-09 08:38:51.010: E/AndroidRuntime(1641): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
11-09 08:38:51.010: E/AndroidRuntime(1641): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
11-09 08:38:51.010: E/AndroidRuntime(1641): at dalvik.system.NativeStart.main(Native Method)
This is where the error
#Override
public void onConnected(Bundle dataBundle) {
// Display the connection status
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
Location location = mLocationClient.getLastLocation();
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 17);
map.animateCamera(cameraUpdate);
}
NullPointerException is thrown because there might be a possibility that the location you got here Location location = mLocationClient.getLastLocation(); is null. Hence you might always do a null-check.
Related
When i am debugging my application on emulator which is of api level 14(4.0) the application is working fine, but when i am debugging same application on xperia p api level 14(4.0.4)) application has stopped unfortunately, and same application debugging on Samsung duos its working properly, application is working fine with emulator and samsung mobile af api level 14 but why its unfortunately stopped on xperia p with api level 14?
Please help me to understand this issue.
Below is the log:
11-09 13:22:30.678: E/AndroidRuntime(7456): FATAL EXCEPTION: main
11-09 13:22:30.678: E/AndroidRuntime(7456): java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:591)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.view.View.measure(View.java:12911)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:599)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.view.View.measure(View.java:12911)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.widget.ScrollView.onMeasure(ScrollView.java:336)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.view.View.measure(View.java:12911)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4805)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.view.View.measure(View.java:12911)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.widget.LinearLayout.measureVertical(LinearLayout.java:828)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.widget.LinearLayout.onMeasure(LinearLayout.java:557)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.view.View.measure(View.java:12911)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4805)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
11-09 13:22:30.678: E/AndroidRuntime(7456): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2097)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.view.View.measure(View.java:12911)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.os.Looper.loop(Looper.java:137)
11-09 13:22:30.678: E/AndroidRuntime(7456): at android.app.ActivityThread.main(ActivityThread.java:4441)
11-09 13:22:30.678: E/AndroidRuntime(7456): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 13:22:30.678: E/AndroidRuntime(7456): at java.lang.reflect.Method.invoke(Method.java:511)
11-09 13:22:30.678: E/AndroidRuntime(7456): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
11-09 13:22:30.678: E/AndroidRuntime(7456): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
11-09 13:22:30.678: E/AndroidRuntime(7456): at dalvik.system.NativeStart.main(Native Method)
I've got following code, which gets launched as a second activity:
public class SensorActivity extends Activity implements SensorEventListener{
List<Sensor> sensors;
Sensor selectedSens;
SensorManager SensMng;
ArrayAdapter<String> adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensor);
final ListView listSensors = (ListView) findViewById(R.id.listvalues);
SensMng = (SensorManager) getSystemService(SENSOR_SERVICE);
sensors = SensMng.getSensorList(Sensor.TYPE_ALL);
ArrayAdapter<Sensor> adapter = new ArrayAdapter<Sensor>(this, android.R.layout.simple_list_item_1, sensors);
listSensors.setAdapter(adapter);
}
Here's my XML file:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listvalues"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
I keep on getting a nullpointer exception:
10-03 21:46:57.475: E/AndroidRuntime(9893): FATAL EXCEPTION: main
10-03 21:46:57.475: E/AndroidRuntime(9893): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ch.ethz.inf.vs.android.spurra.sensors/ch.ethz.inf.vs.android.spurra.sensors.SensorActivity}: java.lang.NullPointerException
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.app.ActivityThread.access$600(ActivityThread.java:140)
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.os.Looper.loop(Looper.java:137)
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.app.ActivityThread.main(ActivityThread.java:4898)
10-03 21:46:57.475: E/AndroidRuntime(9893): at java.lang.reflect.Method.invokeNative(Native Method)
10-03 21:46:57.475: E/AndroidRuntime(9893): at java.lang.reflect.Method.invoke(Method.java:511)
10-03 21:46:57.475: E/AndroidRuntime(9893): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
10-03 21:46:57.475: E/AndroidRuntime(9893): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
10-03 21:46:57.475: E/AndroidRuntime(9893): at dalvik.system.NativeStart.main(Native Method)
10-03 21:46:57.475: E/AndroidRuntime(9893): **Caused by**: java.lang.NullPointerException
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.app.Activity.findViewById(Activity.java:1882)
10-03 21:46:57.475: E/AndroidRuntime(9893): at ch.ethz.inf.vs.android.spurra.sensors.SensorActivity.<init>(SensorActivity.java:24)
10-03 21:46:57.475: E/AndroidRuntime(9893): at java.lang.Class.newInstanceImpl(Native Method)
10-03 21:46:57.475: E/AndroidRuntime(9893): at java.lang.Class.newInstance(Class.java:1319)
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
10-03 21:46:57.475: E/AndroidRuntime(9893): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
10-03 21:46:57.475: E/AndroidRuntime(9893): ... 11 more
All I did was really just copy past from my main activity, plus adjusting the values. The main activity works without any problems, this one however doesn't. I've tried cleaning, rebuilding the project, to no avail.
Can anyone please tell me whats wrong?
Thanks,
Regards
I've declared " final ListView listVals = (ListView) findViewById(R.id.listvalues);" as a field, so before running setContentView(). Once I've moved it into the actual function, it worked. So I assume that was causing it to return null?
Yes. If you call findViewById(R.id.someId); before calling setContentView(R.layout.someLayout); it will return null because you haven't yet inflated your layout which contains the View id you are trying to find.
Basically, your Views exist inside of your layout so if you try to use findViwById() before you inflate your layout (usually with setContentView()) then there is nothing to find. So you will get NPE when you try to use like when you call a function on it.
If your project has external jar files, go to Project->properties->order and export and select all external jar files. That should do the work.
I have developed an app and it runs as expected on the emulator, but gives error while I run it in my device(HTC one X). Below is the error as shown in the logcat. There is a method which creates a notification. Error appears when this method createNotification() is invoked. Logcat is below:
11-09 13:40:13.350: E/AndroidRuntime(5294): FATAL EXCEPTION: main
11-09 13:40:13.350: E/AndroidRuntime(5294): java.lang.NoClassDefFoundError: android.app.TaskStackBuilder
11-09 13:40:13.350: E/AndroidRuntime(5294): at com.example.accessibility.Accessibility.createNotification(Accessibility.java:205)
11-09 13:40:13.350: E/AndroidRuntime(5294): at com.example.accessibility.Accessibility$1.onClick(Accessibility.java:69)
11-09 13:40:13.350: E/AndroidRuntime(5294): at android.view.View.performClick(View.java:3538)
11-09 13:40:13.350: E/AndroidRuntime(5294): at android.view.View$PerformClick.run(View.java:14319)
11-09 13:40:13.350: E/AndroidRuntime(5294): at android.os.Handler.handleCallback(Handler.java:608)
11-09 13:40:13.350: E/AndroidRuntime(5294): at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 13:40:13.350: E/AndroidRuntime(5294): at android.os.Looper.loop(Looper.java:156)
11-09 13:40:13.350: E/AndroidRuntime(5294): at android.app.ActivityThread.main(ActivityThread.java:5045)
11-09 13:40:13.350: E/AndroidRuntime(5294): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 13:40:13.350: E/AndroidRuntime(5294): at java.lang.reflect.Method.invoke(Method.java:511)
11-09 13:40:13.350: E/AndroidRuntime(5294): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-09 13:40:13.350: E/AndroidRuntime(5294): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-09 13:40:13.350: E/AndroidRuntime(5294): at dalvik.system.NativeStart.main(Native Method)
I would appreciate if someone can help me fix this error.
TaskStackBuilder is available starting from API level 16 (Android 4.1 otherwise known as Jellybean). Your HTC One X has Android 4.0 on it.
As mentioned by #David Wasser TaskStackBuilder was added in API Level 16 (Android 4.1)
You can solve your problem by adding import android.support.v4.app.TaskStackBuilder; instead of import android.app.TaskStackBuilder;
make sure your app having either v4 / v7 support library added
I hae the same problem as all this topics about the problems of getting zxing working on a project in android, but none of the solutions worked for me. This is the error log:
11-09 11:06:16.009: E/AndroidRuntime(3481): FATAL EXCEPTION: main
11-09 11:06:16.009: E/AndroidRuntime(3481): java.lang.NoClassDefFoundError: com.google.zxing.client.android.R$layout
11-09 11:06:16.009: E/AndroidRuntime(3481): at com.google.zxing.client.android.CaptureActivity.onCreate(CaptureActivity.java:155)
11-09 11:06:16.009: E/AndroidRuntime(3481): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-09 11:06:16.009: E/AndroidRuntime(3481): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
11-09 11:06:16.009: E/AndroidRuntime(3481): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
11-09 11:06:16.009: E/AndroidRuntime(3481): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
11-09 11:06:16.009: E/AndroidRuntime(3481): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
11-09 11:06:16.009: E/AndroidRuntime(3481): at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 11:06:16.009: E/AndroidRuntime(3481): at android.os.Looper.loop(Looper.java:130)
11-09 11:06:16.009: E/AndroidRuntime(3481): at android.app.ActivityThread.main(ActivityThread.java:3835)
11-09 11:06:16.009: E/AndroidRuntime(3481): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 11:06:16.009: E/AndroidRuntime(3481): at java.lang.reflect.Method.invoke(Method.java:507)
11-09 11:06:16.009: E/AndroidRuntime(3481): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
11-09 11:06:16.009: E/AndroidRuntime(3481): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
11-09 11:06:16.009: E/AndroidRuntime(3481): at dalvik.system.NativeStart.main(Native Method)
I have tried lots of combinations inside buildpath, adding the source project, adding de core.jar (named zxing-1.7.jar in my project), addiing it as external library...but when i don't get this problem at the bottom, then i get a dalvik format error, saying Lcom/zxing/android: already added.
I have also tried to put the .jar inside a folder called libs, but none working, checking and unchecking it on build path configs.
here's an image of my current Build Path order, http://postimage.org/image/f496ukwkf/ , but i tried with lots of different combinations, and none of them worked.
Thanks, i need to get it working, please.
You are trying to include code from android/ in your project. You're not intended to do this, and you haven't set it up correctly either. Integrating by Intent (http://code.google.com/p/zxing/wiki/ScanningViaIntent) is far easier if you are new to this. No library code to mess with.
I am trying an example given in o reilys book and testing it on LG P500 but all I get is force close with the following errors:
WARNING: Application does not specify an API level requirement!
[2011-11-09 15:43:38 - dloc] Device API version is 8 (Android 2.2.2)
**p.s i have mentioned api level**
**logcat:**
11-09 16:00:44.738: W/dalvikvm(8002): threadid=1: thread exiting with uncaught exception (group=0x40020ac0)
11-09 16:00:44.738: E/AndroidRuntime(8002): FATAL EXCEPTION: main
11-09 16:00:44.738: E/AndroidRuntime(8002): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.microjobsinc.dloc/com.microjobsinc.dloc.Main}: java.lang.ClassNotFoundException: com.microjobsinc.dloc.Main in loader dalvik.system.PathClassLoader[/data/app/com.microjobsinc.dloc-2.apk]
11-09 16:00:44.738: E/AndroidRuntime(8002): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
11-09 16:00:44.738: E/AndroidRuntime(8002): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-09 16:00:44.738: E/AndroidRuntime(8002): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-09 16:00:44.738: E/AndroidRuntime(8002): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-09 16:00:44.738: E/AndroidRuntime(8002): at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 16:00:44.738: E/AndroidRuntime(8002): at android.os.Looper.loop(Looper.java:123)
11-09 16:00:44.738: E/AndroidRuntime(8002): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-09 16:00:44.738: E/AndroidRuntime(8002): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 16:00:44.738: E/AndroidRuntime(8002): at java.lang.reflect.Method.invoke(Method.java:521)
11-09 16:00:44.738: E/AndroidRuntime(8002): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-09 16:00:44.738: E/AndroidRuntime(8002): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-09 16:00:44.738: E/AndroidRuntime(8002): at dalvik.system.NativeStart.main(Native Method)
11-09 16:00:44.738: E/AndroidRuntime(8002): Caused by: java.lang.ClassNotFoundException: com.microjobsinc.dloc.Main in loader dalvik.system.PathClassLoader[/data/app/com.microjobsinc.dloc-2.apk]
11-09 16:00:44.738: E/AndroidRuntime(8002): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
11-09 16:00:44.738: E/AndroidRuntime(8002): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
11-09 16:00:44.738: E/AndroidRuntime(8002): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
11-09 16:00:44.738: E/AndroidRuntime(8002): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-09 16:00:44.738: E/AndroidRuntime(8002): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
11-09 16:00:44.738: E/AndroidRuntime(8002): ... 11 more
11-09 16:00:49.848: W/dalvikvm(8013): threadid=1: thread exiting with uncaught exception (group=0x40020ac0)
11-09 16:00:49.858: E/AndroidRuntime(8013): FATAL EXCEPTION: main
Code is written below
public class main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// find the TextViews
TextView tvLatitude = (TextView)findViewById(R.id.tvLatitude);
TextView tvLongitude = (TextView)findViewById(R.id.tvLongitude);
// get handle for LocationManager
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
// connect to the GPS location service
String providerName = LocationManager.GPS_PROVIDER;
Location loc = lm.getLastKnownLocation( providerName);
// fill in the TextViews
tvLatitude.setText(Double.toString(loc.getLatitude()));
tvLongitude.setText(Double.toString(loc.getLongitude()));
}
}
What is the cause of this problem and how can I fix it?
You need to change the android project properties to use Google Apis with the platform version that you desire.