Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 16th, 2011, 12:50 PM
Registered User
 
Join Date: Feb 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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!
Reply With Quote
  #2 (permalink)  
Old February 16th, 2011, 01:27 PM
Authorized User
Points: 62, Level: 1
Points: 62, Level: 1 Points: 62, Level: 1 Points: 62, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2010
Posts: 12
Thanks: 0
Thanked 3 Times in 2 Posts
Default 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.
Reply With Quote
  #3 (permalink)  
Old February 17th, 2011, 01:05 PM
Registered User
 
Join Date: Feb 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
Reply With Quote
  #4 (permalink)  
Old February 17th, 2011, 01:18 PM
Authorized User
Points: 62, Level: 1
Points: 62, Level: 1 Points: 62, Level: 1 Points: 62, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2010
Posts: 12
Thanks: 0
Thanked 3 Times in 2 Posts
Default

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.
Reply With Quote
  #5 (permalink)  
Old February 17th, 2011, 01:34 PM
Registered User
 
Join Date: Feb 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 :(
Reply With Quote
  #6 (permalink)  
Old February 17th, 2011, 02:30 PM
Authorized User
Points: 62, Level: 1
Points: 62, Level: 1 Points: 62, Level: 1 Points: 62, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2010
Posts: 12
Thanks: 0
Thanked 3 Times in 2 Posts
Default

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
Reply With Quote
  #7 (permalink)  
Old February 18th, 2011, 12:14 PM
Registered User
 
Join Date: Feb 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?
Reply With Quote
  #8 (permalink)  
Old February 19th, 2011, 01:20 PM
Authorized User
Points: 62, Level: 1
Points: 62, Level: 1 Points: 62, Level: 1 Points: 62, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2010
Posts: 12
Thanks: 0
Thanked 3 Times in 2 Posts
Default

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.
Reply With Quote
  #9 (permalink)  
Old February 20th, 2011, 11:24 AM
Registered User
 
Join Date: Feb 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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>
Reply With Quote
  #10 (permalink)  
Old February 21st, 2011, 04:20 AM
Authorized User
Points: 62, Level: 1
Points: 62, Level: 1 Points: 62, Level: 1 Points: 62, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2010
Posts: 12
Thanks: 0
Thanked 3 Times in 2 Posts
Default

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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
page 250 overlap footer by columns:solution ok? korinthos BOOK: Professional CSS: Cascading Style Sheets for Web Design 1 January 31st, 2012 08:29 AM
Page 250 static over styles kiwibrit BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 March 27th, 2010 04:35 AM
Implementing GPS aravwind General .NET 5 November 3rd, 2004 09:43 PM
Ch. 7 - Policy Form (p. 250) MER78 BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 September 27th, 2004 03:51 PM
how to get data from GPS clabs Beginning VB 6 2 September 1st, 2004 11:06 PM



All times are GMT -4. The time now is 06:29 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.