NullPointerException in SharedPreferences

  • Replies:3
  • OpenNot stickiedUnanswered
  • Forum posts: 2

Jul 6, 2015 12:05:45 PM via Website

Here is the code...

package com.wavedevelopers.pakistanweather;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

/**
* Created by Haider885 on 18/05/2015.
*/
public class WeatherFragment extends Fragment {

    Typeface weatherFont;
    TextView cityField;
    TextView updatedField;
    TextView detailsField;
    TextView currentTemperatureField;
    TextView weatherIcon;
    String PREFS_NAME = "PakistanWeather";
    Context c;
    CityPreference obj;
    Handler handler;

    public WeatherFragment(){
        handler = new Handler();
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        weatherFont = Typeface.createFromAsset(getActivity().getAssets(), "fonts/256BYTES.TTF");
        updateWeatherData(new CityPreference(getActivity()).getCity());

        cityField.setText( obj.getPreference(c,PREFS_NAME,"CityField"));
        updatedField.setText( obj.getPreference(c,PREFS_NAME,"UpdatedField"));
        detailsField.setText( obj.getPreference(c,PREFS_NAME,"DetailsField"));
        currentTemperatureField.setText( obj.getPreference(c,PREFS_NAME,"CurrentTemperatureField"));


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_weather, container, false);
        cityField = (TextView)rootView.findViewById(R.id.city_field);
        updatedField = (TextView)rootView.findViewById(R.id.updated_field);
        detailsField = (TextView)rootView.findViewById(R.id.details_field);
        currentTemperatureField = (TextView)rootView.findViewById(R.id.current_temperature_field);
        weatherIcon = (TextView)rootView.findViewById(R.id.weather_icon);




        weatherIcon.setTypeface(weatherFont);
        return rootView;
    }




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


    private void renderWeather(JSONObject json){
        try {
            cityField.setText(json.getString("name").toUpperCase(Locale.US) +
                    ", " +
                    json.getJSONObject("sys").getString("country"));

            JSONObject details = json.getJSONArray("weather").getJSONObject(0);
            JSONObject main = json.getJSONObject("main");
            detailsField.setText(
                    details.getString("description").toUpperCase(Locale.US) +
                            "\n" + "Humidity: " + main.getString("humidity") + "%" +
                            "\n" + "Pressure: " + main.getString("pressure") + " hPa");

            currentTemperatureField.setText(
                    String.format("%.2f", main.getDouble("temp"))+ " °C");

            DateFormat df = DateFormat.getDateTimeInstance();
            String updatedOn = df.format(new Date(json.getLong("dt")*1000));
            updatedField.setText("Last update: " + updatedOn);

            setWeatherIcon(details.getInt("id"),
                    json.getJSONObject("sys").getLong("sunrise") * 1000,
                    json.getJSONObject("sys").getLong("sunset") * 1000);

        }catch(Exception e){
            Log.e("SimpleWeather", "One or more fields not found in the JSON data");
        }
    }

    private void setWeatherIcon(int actualId, long sunrise, long sunset){
        int id = actualId / 100;
        String icon = "";
        if(actualId == 800){
            long currentTime = new Date().getTime();
            if(currentTime>=sunrise && currentTime<sunset) {
                icon = getActivity().getString(R.string.weather_sunny);
            } else {
                icon = getActivity().getString(R.string.weather_clear_night);
            }
        } else {
            switch(id) {
                case 2 : icon = getActivity().getString(R.string.weather_thunder);
                    break;
                case 3 : icon = getActivity().getString(R.string.weather_drizzle);
                    break;
                case 7 : icon = getActivity().getString(R.string.weather_foggy);
                    break;
                case 8 : icon = getActivity().getString(R.string.weather_cloudy);
                    break;
                case 6 : icon = getActivity().getString(R.string.weather_snowy);
                    break;
                case 5 : icon = getActivity().getString(R.string.weather_rainy);
                    break;
                case 800 : icon = getActivity().getString(R.string.day_sunny);
                    break;




            }
        }
        weatherIcon.setText(icon);
    }

    public void changeCity(String city)
    {
        updateWeatherData(city);
    }

    @Override
    public void onStop() {
        super.onStop();


        obj.setPreference(c,PREFS_NAME,cityField.getText().toString(),"CityField");
        obj.setPreference(c,PREFS_NAME,updatedField.getText().toString(),"UpdatedField");
        obj.setPreference(c,PREFS_NAME,detailsField.getText().toString(),"DetailsField");
        obj.setPreference(c,PREFS_NAME,currentTemperatureField.getText().toString(),"CurrentTemperatureField");

    }

}

This is sharedpreferences class...

package com.wavedevelopers.pakistanweather;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

/**
* Created by Haider885 on 18/05/2015.
*/
public class CityPreference {

    SharedPreferences prefs;

    public CityPreference(Activity activity){
        prefs = activity.getPreferences(Activity.MODE_PRIVATE);
    }


    // If the user has not chosen a city yet, return
    // Lahore as the default city
    String getCity(){
        return prefs.getString("city", "Lahore");
    }

    void setCity(String city){
        prefs.edit().putString("city", city).commit();


    }

    public boolean setPreference(Context c, String PREFS_NAME, String value, String key) {
        SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0);
        settings = c.getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    public String getPreference(Context c, String PREFS_NAME, String key) {
        SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0);
        settings = c.getSharedPreferences(PREFS_NAME, 0);
        String value = settings.getString(key, "00");
        return value;
    }

}

This is logcat..

07-05 22:26:52.665        61-68/system_process I/ActivityManager﹕ Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.wavedevelopers.pakistanweather/.WeatherActivity } from pid 124
07-05 22:26:54.202       61-154/system_process I/ActivityManager﹕ Start proc com.wavedevelopers.pakistanweather for activity com.wavedevelopers.pakistanweather/.WeatherActivity: pid=376 uid=10046 gids={3003}
07-05 22:26:55.112        33-33/? D/dalvikvm﹕ GC_EXPLICIT freed 11K, 53% free 2538K/5379K, external 1625K/2137K, paused 861ms
07-05 22:26:55.345        33-33/? D/dalvikvm﹕ GC_EXPLICIT freed <1K, 53% free 2538K/5379K, external 1625K/2137K, paused 119ms
07-05 22:26:55.411        33-33/? D/dalvikvm﹕ GC_EXPLICIT freed <1K, 53% free 2538K/5379K, external 1625K/2137K, paused 74ms
07-05 22:26:56.801        61-69/system_process I/ARMAssembler﹕ generated scanline__00000177:03515104_00001002_00000000 [ 87 ipp] (110 ins) at [0x44d3a6f0:0x44d3a8a8] in 64502598 ns
07-05 22:26:57.792      376-376/com.wavedevelopers.pakistanweather I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
07-05 22:26:57.792      376-376/com.wavedevelopers.pakistanweather W/dalvikvm﹕ VFY: unable to resolve virtual method 12219: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
07-05 22:26:57.802      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
07-05 22:26:57.802      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: dead code 0x0003-0015 in Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
07-05 22:26:57.802      376-376/com.wavedevelopers.pakistanweather I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
07-05 22:26:57.802      376-376/com.wavedevelopers.pakistanweather W/dalvikvm﹕ VFY: unable to resolve virtual method 12225: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
07-05 22:26:57.802      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
07-05 22:26:57.802      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: dead code 0x0003-0025 in Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.onStopNestedScroll (Landroid/view/View;)V
07-05 22:26:57.802      376-376/com.wavedevelopers.pakistanweather I/dalvikvm﹕ Could not find method android.view.ViewGroup.onWindowSystemUiVisibilityChanged, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onWindowSystemUiVisibilityChanged
07-05 22:26:57.802      376-376/com.wavedevelopers.pakistanweather W/dalvikvm﹕ VFY: unable to resolve virtual method 12227: Landroid/view/ViewGroup;.onWindowSystemUiVisibilityChanged (I)V
07-05 22:26:57.802      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0008
07-05 22:26:57.812      376-376/com.wavedevelopers.pakistanweather I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
07-05 22:26:57.812      376-376/com.wavedevelopers.pakistanweather W/dalvikvm﹕ VFY: unable to resolve virtual method 9790: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
07-05 22:26:57.812      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
07-05 22:26:57.846      376-376/com.wavedevelopers.pakistanweather I/dalvikvm﹕ Could not find method android.widget.FrameLayout.jumpDrawablesToCurrentState, referenced from method android.support.v7.internal.widget.ActionBarContainer.jumpDrawablesToCurrentState
07-05 22:26:57.846      376-376/com.wavedevelopers.pakistanweather W/dalvikvm﹕ VFY: unable to resolve virtual method 12581: Landroid/widget/FrameLayout;.jumpDrawablesToCurrentState ()V
07-05 22:26:57.852      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0006
07-05 22:26:57.852      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: dead code 0x0009-0023 in Landroid/support/v7/internal/widget/ActionBarContainer;.jumpDrawablesToCurrentState ()V
07-05 22:26:57.862      376-376/com.wavedevelopers.pakistanweather W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/view/ActionMode;)
07-05 22:26:57.962      376-376/com.wavedevelopers.pakistanweather I/dalvikvm﹕ Could not find method android.view.ViewGroup.onHoverEvent, referenced from method android.support.v7.widget.Toolbar.onHoverEvent
07-05 22:26:57.962      376-376/com.wavedevelopers.pakistanweather W/dalvikvm﹕ VFY: unable to resolve virtual method 12212: Landroid/view/ViewGroup;.onHoverEvent (Landroid/view/MotionEvent;)Z
07-05 22:26:57.962      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0010
07-05 22:26:57.962      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: dead code 0x0013-0019 in Landroid/support/v7/widget/Toolbar;.onHoverEvent (Landroid/view/MotionEvent;)Z
07-05 22:26:57.982      376-376/com.wavedevelopers.pakistanweather I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
07-05 22:26:57.982      376-376/com.wavedevelopers.pakistanweather W/dalvikvm﹕ VFY: unable to resolve virtual method 12222: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
07-05 22:26:57.982      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
07-05 22:26:58.002      376-376/com.wavedevelopers.pakistanweather I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
07-05 22:26:58.011      376-376/com.wavedevelopers.pakistanweather W/dalvikvm﹕ VFY: unable to resolve virtual method 401: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
07-05 22:26:58.011      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-05 22:26:58.011      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: dead code 0x0005-0006 in Landroid/support/v7/internal/widget/TintTypedArray;.getChangingConfigurations ()I
07-05 22:26:58.011      376-376/com.wavedevelopers.pakistanweather I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
07-05 22:26:58.011      376-376/com.wavedevelopers.pakistanweather W/dalvikvm﹕ VFY: unable to resolve virtual method 423: Landroid/content/res/TypedArray;.getType (I)I
07-05 22:26:58.011      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-05 22:26:58.011      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: dead code 0x0005-0006 in Landroid/support/v7/internal/widget/TintTypedArray;.getType (I)I
07-05 22:26:58.041      376-376/com.wavedevelopers.pakistanweather I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
07-05 22:26:58.041      376-376/com.wavedevelopers.pakistanweather W/dalvikvm﹕ VFY: unable to resolve virtual method 364: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-05 22:26:58.041      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-05 22:26:58.041      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: dead code 0x0005-0006 in Landroid/support/v7/internal/widget/ResourcesWrapper;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-05 22:26:58.041      376-376/com.wavedevelopers.pakistanweather I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
07-05 22:26:58.051      376-376/com.wavedevelopers.pakistanweather W/dalvikvm﹕ VFY: unable to resolve virtual method 365: Landroid/content/res/Resources;.getDrawableForDensity (II)Landroid/graphics/drawable/Drawable;
07-05 22:26:58.051      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-05 22:26:58.051      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: dead code 0x0005-0006 in Landroid/support/v7/internal/widget/ResourcesWrapper;.getDrawableForDensity (II)Landroid/graphics/drawable/Drawable;
07-05 22:26:58.051      376-376/com.wavedevelopers.pakistanweather I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
07-05 22:26:58.051      376-376/com.wavedevelopers.pakistanweather W/dalvikvm﹕ VFY: unable to resolve virtual method 366: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-05 22:26:58.051      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-05 22:26:58.061      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: dead code 0x0005-0006 in Landroid/support/v7/internal/widget/ResourcesWrapper;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-05 22:26:58.071      376-376/com.wavedevelopers.pakistanweather I/dalvikvm﹕ Could not find method android.content.res.Resources.getValueForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getValueForDensity
07-05 22:26:58.071      376-376/com.wavedevelopers.pakistanweather W/dalvikvm﹕ VFY: unable to resolve virtual method 389: Landroid/content/res/Resources;.getValueForDensity (IILandroid/util/TypedValue;Z)V
07-05 22:26:58.071      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-05 22:26:58.071      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: dead code 0x0005-0005 in Landroid/support/v7/internal/widget/ResourcesWrapper;.getValueForDensity (IILandroid/util/TypedValue;Z)V
07-05 22:26:58.241      376-376/com.wavedevelopers.pakistanweather I/dalvikvm﹕ Could not find method android.view.accessibility.AccessibilityEvent.setSource, referenced from method android.support.v7.internal.widget.ActionBarContextView.onInitializeAccessibilityEvent
07-05 22:26:58.241      376-376/com.wavedevelopers.pakistanweather W/dalvikvm﹕ VFY: unable to resolve virtual method 12332: Landroid/view/accessibility/AccessibilityEvent;.setSource (Landroid/view/View;)V
07-05 22:26:58.241      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
07-05 22:26:58.261      376-376/com.wavedevelopers.pakistanweather I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.AbsActionBarView.onInitializeAccessibilityEvent, referenced from method android.support.v7.internal.widget.ActionBarContextView.onInitializeAccessibilityEvent
07-05 22:26:58.261      376-376/com.wavedevelopers.pakistanweather W/dalvikvm﹕ VFY: unable to resolve virtual method 9488: Landroid/support/v7/internal/widget/AbsActionBarView;.onInitializeAccessibilityEvent (Landroid/view/accessibility/AccessibilityEvent;)V
07-05 22:26:58.261      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x002d
07-05 22:26:58.261      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: dead code 0x0011-002b in Landroid/support/v7/internal/widget/ActionBarContextView;.onInitializeAccessibilityEvent (Landroid/view/accessibility/AccessibilityEvent;)V
07-05 22:26:58.261      376-376/com.wavedevelopers.pakistanweather D/dalvikvm﹕ VFY: dead code 0x0030-0030 in Landroid/support/v7/internal/widget/ActionBarContextView;.onInitializeAccessibilityEvent (Landroid/view/accessibility/AccessibilityEvent;)V
07-05 22:26:58.629      376-376/com.wavedevelopers.pakistanweather D/AndroidRuntime﹕ Shutting down VM
07-05 22:26:58.629      376-376/com.wavedevelopers.pakistanweather W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-05 22:26:58.742      376-376/com.wavedevelopers.pakistanweather E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wavedevelopers.pakistanweather/com.wavedevelopers.pakistanweather.WeatherActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.wavedevelopers.pakistanweather.WeatherFragment.onCreate(WeatherFragment.java:48)
            at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
            at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
            at android.app.Activity.performStart(Activity.java:3791)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1620)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
07-05 22:26:59.023       61-171/system_process W/ActivityManager﹕ Force finishing activity com.wavedevelopers.pakistanweather/.WeatherActivity
07-05 22:26:59.538        61-72/system_process W/ActivityManager﹕ Activity pause timeout for HistoryRecord{40658438 com.wavedevelopers.pakistanweather/.WeatherActivity}
07-05 22:27:09.913        61-72/system_process W/ActivityManager﹕ Activity destroy timeout for HistoryRecord{40658438 com.wavedevelopers.pakistanweather/.WeatherActivity}
07-05 22:29:24.641       61-114/system_process D/SntpClient﹕ request time failed: java.net.SocketException: Address family not supported by protocol
07-05 22:31:59.141      376-376/com.wavedevelopers.pakistanweather I/Process﹕ Sending signal. PID: 376 SIG: 9
07-05 22:31:59.522       61-151/system_process I/ActivityManager﹕ Process com.wavedevelopers.pakistanweather (pid 376) has died.
07-05 22:31:59.621       61-288/system_process W/InputManagerService﹕ Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40832048

This is line 48 ...

cityField.setText( obj.getPreference(c,PREFS_NAME,"CityField"));
  • Forum posts: 12

Jul 11, 2015 11:22:48 PM via Website

To me, it seems like your "c" variable is uninitialized at your call to getPreferences in line 48.

  • Forum posts: 7

Jul 12, 2015 9:54:59 AM via Website

Mats Hillborg

To me, it seems like your "c" variable is uninitialized at your call to getPreferences in line 48.

Indeed, that's seems to be the reason. You should either use getActivity() instead of "c" or assign the result of getActivity() inside "c".

  • Forum posts: 12

Jul 13, 2015 12:12:08 AM via Website

No, sorry, but getActivity() will not work!
You have to use getBaseContext() to set your c variable before using it.