 |
BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2
 | This is the forum to discuss the Wrox book Professional Android Application Development by Reto Meier; ISBN: 9780470344712 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

February 16th, 2011, 12:50 PM
|
|
Registered User
|
|
Join Date: Feb 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
GPS example page 250
Hola!
I have a question, AGAIN.
I've got a null exception, cant figure out whats wrong with my code.
This is what the console says:
Code:
[2011-02-16 17:38:45 - ddms]null
java.lang.NullPointerException
at com.android.ddmlib.Client.sendAndConsume(Client.java:571)
at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
at com.android.ddmlib.Client.getJdwpPacket(Client.java:670)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)
[2011-02-16 17:38:45 - ddms]null
java.lang.NullPointerException
at com.android.ddmlib.Client.sendAndConsume(Client.java:571)
at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
at com.android.ddmlib.Client.getJdwpPacket(Client.java:670)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)
Her is my localizer.java
Code:
package no.local.android;
import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.widget.*;
public class Localizer extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager location;
String innhold = Context.LOCATION_SERVICE;
location = (LocationManager)getSystemService(innhold);
String provider = LocationManager.GPS_PROVIDER;
Location lokasjon = location.getLastKnownLocation(provider);
updateWithNewLocation(lokasjon);
}
private void updateWithNewLocation(Location lokasjon) {
String latitudeText;
TextView lokText;
lokText = (TextView)findViewById(R.id.lokText);
if(lokasjon != null) {
double lat = lokasjon.getLatitude();
double lngtit = lokasjon.getLongitude();
latitudeText = "Latitude:" + lat + "Longtitude" + lngtit;
}
else {
latitudeText = "Kunne ikke finne lokasjonen din";
}
lokText.setText("Din nåværende lokasjon er:\t" + latitudeText);
}
}
And here is my manifest.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="no.local.android"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Localizer"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"
/>
</application>
</manifest>
And here is the main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android="@+id/lokText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<uses-permission android:name="android.permission.INTERNET"/>
</LinearLayout>
Thanks!
|
|

February 16th, 2011, 01:27 PM
|
|
Authorized User
|
|
Join Date: Nov 2010
Posts: 12
Thanks: 0
Thanked 3 Times in 2 Posts
|
|
Extra line in main.xml
I would try removing the
Code:
<uses-permission android:name="android.permission.INTERNET"/>
line from the main.xml. It is printed in the book, but does not seem to be in any of the on-line sources. it looks more like a line that should be in a manifest to me.
This may not have any effect on the null pointer problem, you may have to run your application in debug to try and find where it is being caused.
|
|

February 17th, 2011, 01:05 PM
|
|
Registered User
|
|
Join Date: Feb 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I worked my way from the top of the code, and to the end, commented every line, and running the application every changes I made. I found out that this line causes that "apps stopped running problem I've got:
Code:
Location sted =
locationManager.getLastKnownLocation(provider)
solution, any1?
thanks.
|
|

February 17th, 2011, 01:18 PM
|
|
Authorized User
|
|
Join Date: Nov 2010
Posts: 12
Thanks: 0
Thanked 3 Times in 2 Posts
|
|
Perhaph you should check that the variable locationManager is not null before using it. If it is null this probably indicates that the device you are trying to run the program on has no GPS, or some similar problem.
|
|

February 17th, 2011, 01:34 PM
|
|
Registered User
|
|
Join Date: Feb 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for fast respons! :) I've tried it on my computer with avd, my computer doesnt have GPS, but I tried the app on my friends phone which HAVE gps, but the app stops responding as soon I start it. Frustrating :(
|
|

February 17th, 2011, 02:30 PM
|
|
Authorized User
|
|
Join Date: Nov 2010
Posts: 12
Thanks: 0
Thanked 3 Times in 2 Posts
|
|
Next thing has definitly got to be to check if the locationManager object is null before you try and use it. If it is then you are on to the next step of figuring out WHY it is null. If it is not null and is a valid object then you have more knowledge and can start looking elsewhere for the problem
|
|

February 18th, 2011, 12:14 PM
|
|
Registered User
|
|
Join Date: Feb 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Again, I've tried this to check if my object returns null
Code:
if(locationManager.equals(null)) { String warning = "Return null"; }
But the app do crash when I started it. This is what my debug says
Code:
Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2663
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2679
ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 125
ActivityThread$H.handleMessage(Message) line: 2033
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626
NativeStart.main(String[]) line: not available [native method]
Any ideas?
|
|

February 19th, 2011, 01:20 PM
|
|
Authorized User
|
|
Join Date: Nov 2010
Posts: 12
Thanks: 0
Thanked 3 Times in 2 Posts
|
|
You can't test if a variable is null like that, if the variable is null when you call the equals() method on it that will cause problems.
What you need to test the variable for null is more like
Code:
if (locationManager == null)
{
Log.d("Testing", "locationManager is null!");
}
else
{
... code to be executed if locationManager is not null
}
The Log.d() will write a message to the log if the locationManager variable is null.
|
|

February 20th, 2011, 11:24 AM
|
|
Registered User
|
|
Join Date: Feb 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi, thanks!
I've got some more information from the debugger now. LogCat says following:
Code:
unknown element under <application> uses-permission at /data/app/vmdl70050.tmp Binary XML file line #17
unknown element under <application> uses-permission at /data/app/vmdl70050.tmp Binary XML file line #18
unknown element under <application> uses-permission at /data/app/vmdl70050.tmp Binary XML file line #19
unknown element under <application> uses-permission at /data/app/vmdl70050.tmp Binary XML file line #20
unknown element under <application> uses-permission at /data/app/vmdl70050.tmp Binary XML file line #21
So there have to something wrong with my manifest.xml file? because line 17-21 contains the uses-permission. My Manifest file:
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="no.local.android"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Localizer"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
</application>
</manifest>
|
|

February 21st, 2011, 04:20 AM
|
|
Authorized User
|
|
Join Date: Nov 2010
Posts: 12
Thanks: 0
Thanked 3 Times in 2 Posts
|
|
Permissions need to be after the </application> tag. Also the book does not mention the ACCESS_GPS or ACCESS_LOCATION permissions so I would remove them as I don't think they do anything, but they probably wont' do any harm once they are after the </application> tag.
|
|
 |