0

PreferenceManager を使用しているときに null ポイント例外が発生する理由を理解するのに苦労しています。

現在のアクティビティ内のボタンを使用して、という名前の別のアクティビティを呼び出していますLocation

    /** Location selection */
    bLocation = (Button) findViewById(R.id.bLocation);
    bLocation.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            loc = new Location();
            loc.locationSelection();
        }
    });

// 上のボタンは下のクラスを呼び出します

public class Location extends Activity {

    NetworkConnection nc;
    SharedPreferences prefs;
    SharedPreferences sharedPrefs;
    NetworkConnection netConnect;
    Options opts;

    public void locationSelection(){

        try{
            sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            if (sharedPrefs.contains("LOCATION")){
                Log.d("if", "if");
            }else{
                Log.d("if", "else");

            }

        }catch(Exception e){
            Log.d("EXCEPTION", "Location Selection " + e);
        }
    }
}

編集:

//Location クラスをクラス化する Options クラス全体。

public class Options extends Activity {

    /** Include classes */
    SharedPreferences sharedPrefs;
    Preferences prefs;
    Location loc;
    LocationSQL locSQL;
    NetworkConnection netConnect;

    /** Declare buttons */
    Button bLocation;
    Button bRefresh;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.options);

        sharedPrefs = PreferenceManager.getDefaultSharedPreferences(Options.this);

        /** Set the location for the user */
        setLocationPref();

        /** Refresh selection */
        bRefresh = (Button) findViewById(R.id.bRefresh);
        bRefresh.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {


            }
        });

        /** Location selection */
        bLocation = (Button) findViewById(R.id.bLocation);
        bLocation.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                loc = new Location();
                loc.locationSelection();
            }
        });
    }

//丸太猫全体

06-10 13:56:03.015: D/AndroidRuntime(27310): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
06-10 13:56:03.015: D/AndroidRuntime(27310): CheckJNI is ON
06-10 13:56:03.135: D/AndroidRuntime(27310): --- registering native functions ---
06-10 13:56:03.685: D/dalvikvm(249): GC_EXPLICIT freed 88 objects / 4296 bytes in 43ms
06-10 13:56:03.726: D/PackageParser(59): Scanning package: /data/app/vmdl72287.tmp
06-10 13:56:03.845: I/PackageManager(59): Removing non-system package:com.swiftdrink
06-10 13:56:03.845: I/ActivityManager(59): Force stopping package com.swiftdrink uid=10036
06-10 13:56:03.855: I/Process(59): Sending signal. PID: 26952 SIG: 9
06-10 13:56:03.865: I/UsageStats(59): Unexpected resume of com.android.launcher while already resumed in com.swiftdrink
06-10 13:56:03.875: I/WindowManager(59): WIN DEATH: Window{44f77270 com.swiftdrink/com.swiftdrink.SwiftDrinkMain paused=false}
06-10 13:56:03.905: W/InputManagerService(59): Got RemoteException sending setActive(false) notification to pid 26952 uid 10036
06-10 13:56:04.016: W/IInputConnectionWrapper(114): showStatusIcon on inactive InputConnection
06-10 13:56:04.205: D/PackageManager(59): Scanning package com.swiftdrink
06-10 13:56:04.205: I/PackageManager(59): Package com.swiftdrink codePath changed from /data/app/com.swiftdrink-1.apk to /data/app/com.swiftdrink-2.apk; Retaining data and using new
06-10 13:56:04.215: I/PackageManager(59): /data/app/com.swiftdrink-2.apk changed; unpacking
06-10 13:56:04.225: D/installd(34): DexInv: --- BEGIN '/data/app/com.swiftdrink-2.apk' ---
06-10 13:56:04.435: D/dalvikvm(27317): DexOpt: load 36ms, verify 99ms, opt 2ms
06-10 13:56:04.445: D/installd(34): DexInv: --- END '/data/app/com.swiftdrink-2.apk' (success) ---
06-10 13:56:04.445: W/PackageManager(59): Code path for pkg : com.swiftdrink changing from /data/app/com.swiftdrink-1.apk to /data/app/com.swiftdrink-2.apk
06-10 13:56:04.445: W/PackageManager(59): Resource path for pkg : com.swiftdrink changing from /data/app/com.swiftdrink-1.apk to /data/app/com.swiftdrink-2.apk
06-10 13:56:04.455: D/PackageManager(59):   Activities: com.swiftdrink.SplashScreen com.swiftdrink.SwiftDrinkMain com.swiftdrink.Deals com.swiftdrink.Events com.swiftdrink.Featured com.swiftdrink.Options com.swiftdrink.SortBy com.swiftdrink.NetworkConnection com.swiftdrink.Constents com.swiftdrink.getSetLocation com.swiftdrink.Location com.swiftdrink.DBAdapter com.swiftdrink.JSON com.swiftdrink.Preferences
06-10 13:56:04.465: I/ActivityManager(59): Force stopping package com.swiftdrink uid=10036
06-10 13:56:04.565: I/installd(34): move /data/dalvik-cache/data@app@com.swiftdrink-2.apk@classes.dex -> /data/dalvik-cache/data@app@com.swiftdrink-2.apk@classes.dex
06-10 13:56:04.565: D/PackageManager(59): New package installed in /data/app/com.swiftdrink-2.apk
06-10 13:56:04.685: I/ActivityManager(59): Force stopping package com.swiftdrink uid=10036
06-10 13:56:04.835: D/dalvikvm(114): GC_EXPLICIT freed 1024 objects / 58032 bytes in 140ms
06-10 13:56:04.845: D/dalvikvm(59): GC_EXPLICIT freed 12088 objects / 692712 bytes in 141ms
06-10 13:56:05.015: W/RecognitionManagerService(59): no available voice recognition services found
06-10 13:56:05.235: D/dalvikvm(59): GC_EXPLICIT freed 4804 objects / 260184 bytes in 94ms
06-10 13:56:05.255: D/dalvikvm(148): GC_EXPLICIT freed 2567 objects / 129888 bytes in 540ms
06-10 13:56:05.265: I/installd(34): unlink /data/dalvik-cache/data@app@com.swiftdrink-1.apk@classes.dex
06-10 13:56:05.276: D/AndroidRuntime(27310): Shutting down VM
06-10 13:56:05.285: D/dalvikvm(27310): Debugger has detached; object registry had 1 entries
06-10 13:56:05.305: I/dalvikvm(27310): JNI: AttachCurrentThread (from ???.???)
06-10 13:56:05.305: I/AndroidRuntime(27310): NOTE: attach of thread 'Binder Thread #3' failed
06-10 13:56:05.786: D/AndroidRuntime(27322): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
06-10 13:56:05.786: D/AndroidRuntime(27322): CheckJNI is ON
06-10 13:56:05.895: D/AndroidRuntime(27322): --- registering native functions ---
06-10 13:56:06.466: I/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.swiftdrink/.SplashScreen }
06-10 13:56:06.505: I/ActivityManager(59): Start proc com.swiftdrink for activity com.swiftdrink/.SplashScreen: pid=27328 uid=10036 gids={3003, 1015}
06-10 13:56:06.535: D/AndroidRuntime(27322): Shutting down VM
06-10 13:56:06.535: D/dalvikvm(27322): Debugger has detached; object registry had 1 entries
06-10 13:56:06.575: I/AndroidRuntime(27322): NOTE: attach of thread 'Binder Thread #3' failed
06-10 13:56:07.445: D/dalvikvm(27328): GC_EXTERNAL_ALLOC freed 858 objects / 59096 bytes in 175ms
06-10 13:56:07.815: I/ActivityManager(59): Displayed activity com.swiftdrink/.SplashScreen: 1333 ms (total 1333 ms)
06-10 13:56:08.155: I/ActivityManager(59): Starting activity: Intent { act=com.swiftdrink.SWIFTDRINKMAIN cmp=com.swiftdrink/.SwiftDrinkMain }
06-10 13:56:08.275: V/one(27328): locationSelection
06-10 13:56:08.585: I/ActivityManager(59): Displayed activity com.swiftdrink/.SwiftDrinkMain: 428 ms (total 428 ms)
06-10 13:56:11.646: D/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol
06-10 13:56:16.886: D/dalvikvm(114): GC_EXPLICIT freed 970 objects / 55696 bytes in 124ms
06-10 13:56:20.166: D/EXCEPTION(27328): Location Selection java.lang.NullPointerException
06-10 13:56:21.906: D/dalvikvm(260): GC_EXPLICIT freed 66 objects / 3040 bytes in 128ms

EDIT 2: //ネットワーク接続クラス

public class NetworkConnection extends Activity {
    /** Called when the activity is first created. */

    public boolean isNetworkConnAvail(Context context) {

        ConnectivityManager connMgr = (ConnectivityManager) 
            context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null)
            return networkInfo.isConnected();

        return false;
    }
}

//印刷ステートメント

06-10 14:10:40.015: D/AndroidRuntime(27731): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
06-10 14:10:40.015: D/AndroidRuntime(27731): CheckJNI is ON
06-10 14:10:40.145: D/AndroidRuntime(27731): --- registering native functions ---
06-10 14:10:40.535: D/dalvikvm(27670): GC_EXPLICIT freed 1408 objects / 74352 bytes in 898ms
06-10 14:10:40.645: D/AndroidRuntime(27731): Shutting down VM
06-10 14:10:40.645: D/dalvikvm(27731): Debugger has detached; object registry had 1 entries
06-10 14:10:40.665: I/AndroidRuntime(27731): NOTE: attach of thread 'Binder Thread #3' failed
06-10 14:10:41.045: D/AndroidRuntime(27739): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
06-10 14:10:41.045: D/AndroidRuntime(27739): CheckJNI is ON
06-10 14:10:41.166: D/AndroidRuntime(27739): --- registering native functions ---
06-10 14:10:41.655: I/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.swiftdrink/.SplashScreen }
06-10 14:10:41.695: D/AndroidRuntime(27739): Shutting down VM
06-10 14:10:41.695: D/dalvikvm(27739): Debugger has detached; object registry had 1 entries
06-10 14:10:41.775: I/dalvikvm(27739): JNI: AttachCurrentThread (from ???.???)
06-10 14:10:41.775: I/AndroidRuntime(27739): NOTE: attach of thread 'Binder Thread #3' failed
06-10 14:10:42.465: I/ActivityManager(59): Displayed activity com.swiftdrink/.SplashScreen: 799 ms (total 799 ms)
06-10 14:10:42.845: I/ActivityManager(59): Starting activity: Intent { act=com.swiftdrink.SWIFTDRINKMAIN cmp=com.swiftdrink/.SwiftDrinkMain }
06-10 14:10:42.895: V/one(27670): locationSelection
06-10 14:10:43.186: I/ActivityManager(59): Displayed activity com.swiftdrink/.SwiftDrinkMain: 337 ms (total 337 ms)
06-10 14:10:45.185: D/EXCEPTION(27670): Location Selection java.lang.NullPointerException
06-10 14:10:45.185: W/System.err(27670): java.lang.NullPointerException
06-10 14:10:45.205: W/System.err(27670):    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
06-10 14:10:45.205: W/System.err(27670):    at com.swiftdrink.Location.locationSelection(Location.java:39)
06-10 14:10:45.205: W/System.err(27670):    at com.swiftdrink.Options$2.onClick(Options.java:73)
06-10 14:10:45.205: W/System.err(27670):    at android.view.View.performClick(View.java:2408)
06-10 14:10:45.205: W/System.err(27670):    at android.view.View$PerformClick.run(View.java:8816)
06-10 14:10:45.205: W/System.err(27670):    at android.os.Handler.handleCallback(Handler.java:587)
06-10 14:10:45.216: W/System.err(27670):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-10 14:10:45.216: W/System.err(27670):    at android.os.Looper.loop(Looper.java:123)
06-10 14:10:45.216: W/System.err(27670):    at android.app.ActivityThread.main(ActivityThread.java:4627)
06-10 14:10:45.216: W/System.err(27670):    at java.lang.reflect.Method.invokeNative(Native Method)
06-10 14:10:45.216: W/System.err(27670):    at java.lang.reflect.Method.invoke(Method.java:521)
06-10 14:10:45.216: W/System.err(27670):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-10 14:10:45.216: W/System.err(27670):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-10 14:10:45.226: W/System.err(27670):    at dalvik.system.NativeStart.main(Native Method)
06-10 14:10:48.366: D/dalvikvm(114): GC_EXPLICIT freed 239 objects / 11320 bytes in 128ms
4

1 に答える 1

1

loc = new Location(); を使用して、Location クラスのインスタンスを作成します。現在、Location はアクティビティを拡張するクラスであり、通常のアクティビティのように (たとえば、インテントを介して) 起動していないため、onCreate() が呼び出されることはありません。このため、アクティビティが期待どおりにインスタンス化されていないため、getApplicationContext() は null を返します。

これを修正するには、次のようにコンストラクターをクラスに追加します。

//New Global Variable

Context context;

public Location (Context arg)
{
context = arg;
}

contextまた、 getApplicationContext() の代わりに変数を使用するように SharedPreferences の初期化を変更する必要があります。

そして、次を使用して loc を割り当てます。

loc = new Location(getApplicationContext());

これにより、少なくとも NullPointerException が解決されるはずです。

サポートされていない Address タイプが原因で SocketException が発生しているようですが、投稿したコードからは、アドレスをまったく使用していないようです。

于 2012-06-10T14:22:14.430 に答える