0

I'm trying do re-adapt simple weather app (http://code.tutsplus.com/tutorials/create-a-weather-app-on-android--cms-21587) to Google Glass, it returns a json object on Android and works, but in my google glass when I want to use the getCity function, I have a null pointer exception error.

My getCity function is the same as the one in SimpleWeatherApp

    public String getCity(){

    return prefs.getString("city", "Jerusalem, IL");
}

Theres is 3 Java Files, 2 Class and 1 Activity.

Citypreference , to return the city and get city.

RemoteFetch to get the Json Object

Main activity to build view for the google glass and show the information that I need.

I can share to you the whole project if you want more informations.

here's a part of my MainActivity content private View buildView() { CardBuilder card = new CardBuilder(this, CardBuilder.Layout.TEXT); card.setText(R.string.open_weather_maps_app_id); //updateWeatherData(new CityPreference(getActivity()).getCity());

    updateWeatherData(new CityPreference().getCity());
       return card.getView();
}


private void updateWeatherData(final String city){
    new Thread(){
        public void run(){
            final JSONObject json = RemoteFetch.getJSON(MainActivity.this.getApplicationContext(),city);
            if(json == null){
                handler.post(new Runnable(){
                    public void run(){
                        Toast.makeText(MainActivity.this,
                                MainActivity.this.getString(R.string.place_not_found), = SO JSON NULL
                                Toast.LENGTH_LONG).show();
                    }
                });
            } else {
                handler.post(new Runnable(){
                    public void run(){
                        renderWeather(json);
                    }
                });
            }
        }
    }.start();
}

So that's mean my json object is null. For the remotefetch.java you can find it in the SimpleWeatherApp link on the top of my post

stmorray
  • 1
  • 6

1 Answers1

0

Modify the code as under

public String getCity(){

    if(prefs != null)
        return prefs.getString("city", "Jerusalem, IL");

    return "city_not_initialized";
}

Or share more code

SimpleGuy
  • 2,764
  • 5
  • 28
  • 45
  • Now, I have a new error but that's the one I've writed so that's good : (sorry no weather data found) I'll edit my main post to share more info – stmorray Nov 19 '14 at 09:36
  • @user3272072 As I see in the link posted by you [link](http://code.tutsplus.com/tutorials/create-a-weather-app-on-android--cms-21587), CityPreference constructor needs an activity. Did you make a different CityPref ? If yes, please paste that code as welll – SimpleGuy Nov 19 '14 at 10:07
  • @user3272072 Sorry.. I see you have written it twice so got confused... reading... – SimpleGuy Nov 19 '14 at 10:08
  • @user3272072 Where are you getting the error now ? If you are getting `city_not_initialized` then `Prefs` is not initialized. Pleas e paste the error as well in question – SimpleGuy Nov 19 '14 at 10:12
  • Thanks for your answers, I get No Weather data found,so that's the Json object that appers to be null. – stmorray Nov 19 '14 at 10:14
  • @user3272072 JSON may also be NULL coz `city` may be NULL... or there isn't any JSON data for the `city` fetched using `getCity()`. Try printing value of city in logs.. use ADB for that – SimpleGuy Nov 19 '14 at 10:22
  • yes the value is null...but i don't know why, as the code I re-adapted looks basically the same... The RemoteFetch.java(with JSON informatons) used to get theses is the same on both project – stmorray Nov 19 '14 at 14:20
  • (appears to be null according to logcat) – stmorray Nov 19 '14 at 15:43
  • @user3272072 I ran the code using hardcoded URL for [Israel](http://api.openweathermap.org/data/2.5/weather?q=Israel&units=metric). It runs perfectly fine. You to try doing the same. Print `json` of `StringBuffer`. Reasons for null can be: **1.** You have an exception occuring **2.** `JSON` parsing is really null. Also **check if the device has internet**, as your code uses internet to fetch data. – SimpleGuy Nov 19 '14 at 16:07
  • Thanks, yeah I know the api works fine, it even works with the app for android, wich does the same thing. I also tought it was about the internet connection but I shared my connection with glass and have internet so... – stmorray Nov 19 '14 at 16:12
  • In the RemoteFetch object, can you check that the request did return a 200 and not a 404? – Koh Nov 19 '14 at 22:09
  • Yeah, i'm using if(data.getInt("cod") != 200){ Log.e("SimpleWeather", "json problem, cod is not 200"); return null; } , and the code is not 200 , and i've also test is not 400 ... I forgot to add android internet permission in Manifest, I've added it but still the same errors. – stmorray Nov 20 '14 at 14:55
  • I think the problem is here , The Android weatherapp use updateWeatherData(new CityPreference(getActivity()).getCity()); to get the city but can't use it in The MainActivity of Google Glass' app because I have : "Cannot resolve method getActivity(), I've tried a lot of thing to get rid of this error but even when i'm able to use getCity() function I have no data display from the JSON object so maybe all my problems come frome this line. What's the bes t Ica ndo to resolve that ? ( updateWeatherData(new CityPreference(getActivity()).getCity());) – stmorray Nov 20 '14 at 15:38
  • hey I am not very sure...as I have never tried for glass.. but can you try this... `CityPreference(PendingIntent.getActivity()).getCity());` – SimpleGuy Nov 21 '14 at 02:49
  • thanks, but couldn't resolve PendingIntent... and that's the same problem for the method getActivity()... it works on the Android App but on the glass app getActivity is an unknown method, that's not the same SDK, so maybe some function are not available, that's why I've added other libs from android original sdk but same problem... – stmorray Nov 21 '14 at 09:49
  • @user3272072 was this there ? `import android.app.PendingIntent` – SimpleGuy Nov 21 '14 at 12:07
  • i managed to get it working but getActivity() need arguments to work properly like context,request code, intent and flags... – stmorray Nov 21 '14 at 13:04
  • @user3272072 `PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class)` – SimpleGuy Nov 21 '14 at 14:48
  • thanks again, about new Intent(this,MainActivity.class) got Can not resolve constructor 'Intent(java.lang.Thread, java.lang.Class' – stmorray Nov 21 '14 at 15:08
  • @user3272072 make your class extend Application / Service and then make the write `PendingIntent.getActivity(this, 0, new Intent(getApplicationContext(), MainActivity.class)` – SimpleGuy Nov 24 '14 at 05:59
  • (sorry for late answer) My RemoteFetch.java was "extends Activity" , if I use your PendingIntent.getActivity ...[] I get "The method getActivity(Context, int, Intent, int) in the type PendingIntent is not applicable for the arguments (new Thread(){}, int, Intent)" – stmorray Dec 01 '14 at 14:37