user1234167
user1234167

Reputation: 83

Beginner at intents

Can anyone spot what is wrong with this code? It's the first time I have used intents:

Intent intent = new Intent( this,XmlParserActivity.class);

String Postcode = returnedAddress.getPostalCode().toString();

intent.putExtra("postcode", Postcode);

and in my parser I have:

Intent intent = getIntent();

String Postcode = intent.getStringExtra("postcode");

URL url = new URL("http://new.myweather2.com/developer/forecast.ashx?uac=gcV3ynNdoV&output=xml&query=" + Postcode);

Please note that my gps is working, I have tested it

Logcat:

04-01 21:07:25.514: E/AndroidRuntime(19408): FATAL EXCEPTION: main
04-01 21:07:25.514: E/AndroidRuntime(19408): java.lang.NullPointerException
04-01 21:07:25.514: E/AndroidRuntime(19408):    at weather.app.WeatherApplicationActivity.onLocationChanged(WeatherApplicationActivity.java:116)
04-01 21:07:25.514: E/AndroidRuntime(19408):    at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:228)
04-01 21:07:25.514: E/AndroidRuntime(19408):    at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:160)
04-01 21:07:25.514: E/AndroidRuntime(19408):    at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:176)
04-01 21:07:25.514: E/AndroidRuntime(19408):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 21:07:25.514: E/AndroidRuntime(19408):    at android.os.Looper.loop(Looper.java:150)
04-01 21:07:25.514: E/AndroidRuntime(19408):    at android.app.ActivityThread.main(ActivityThread.java:4293)
04-01 21:07:25.514: E/AndroidRuntime(19408):    at java.lang.reflect.Method.invokeNative(Native Method)
04-01 21:07:25.514: E/AndroidRuntime(19408):    at java.lang.reflect.Method.invoke(Method.java:507)
04-01 21:07:25.514: E/AndroidRuntime(19408):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
04-01 21:07:25.514: E/AndroidRuntime(19408):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
04-01 21:07:25.514: E/AndroidRuntime(19408):    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 193

Answers (7)

jigar
jigar

Reputation: 1591

If you are usin "putExra" then in second activity use "getExtra"...!

Upvotes: 0

nurisezgin
nurisezgin

Reputation: 1570

Your "onLocationChanged" method will surrond with try-catch(NullPionerException) If you didn't get force close than this method' s variable and another variable in use method check.

Upvotes: 0

Snicolas
Snicolas

Reputation: 38168

According to your log, you have a null pointer exception on the line 116 of WeatherApplicationActivity.java.

Check this line, you should have a statement of the form A.B where A is null.

It could something like a().b.c().d where something at the left of a dot is null : either a() or a().b or a().b.c() is null

Upvotes: 1

pouzzler
pouzzler

Reputation: 1834

This code is perfect, nothing is wrong with it. You need to cut & paste a little more; in particular, do you use

startActivity(intent);

after setting up the intent?

Upvotes: 0

Yilmaz Guleryuz
Yilmaz Guleryuz

Reputation: 9745

How about trying to get intent extras as below? plz also log the extras bundle and see if you are getting correct key-val

Bundle extras = getIntent().getExtras(); 
if(extras !=null) {
     String PostCode = extras.getString("postcode");
}

Upvotes: 0

Exorcist
Exorcist

Reputation: 252

The first code should be something like this :

Intent intent = new Intent( YourCurrentClassname.this,XmlParserActivity.class);

String Postcode = returnedAddress.getPostalCode().toString();

intent.putExtra("postcode", Postcode);

And to receive the value :

Intent intent = getIntent();

String Postcode = intent.getExtras(FirstClassName.postcode);

URL url = new URL("http://new.myweather2.com/developer/forecast.ashx?uac=gcV3ynNdoV&output=xml&query=" + Postcode);

Upvotes: 0

Snicolas
Snicolas

Reputation: 38168

Are you sure to use the second part of the code (in XmlParserActivity) in the onCreate method ?

It should work. And log more values to see what happens.

Upvotes: 0

Related Questions