我很难理解为什么在使用PreferenceManager时我得到一个零点异常。
我正在使用当前活动中的按钮来调用另一个名为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);
}
}
}
编辑:
//整个Options类,它是Location类的类。
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
编辑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
答案 0 :(得分:1)
使用loc = new Location()创建Location类的实例;现在Location是一个扩展Activity的类,因为你没有像普通活动那样启动它(例如通过Intents),所以永远不会调用onCreate()。因此,您的getApplicationContext()将返回null,因为您的Activity尚未按预期实例化。
要解决此问题,您可以像这样在类中添加构造函数:
//New Global Variable
Context context;
public Location (Context arg)
{
context = arg;
}
此外,您需要更改SharedPreferences初始化以使用context
变量而不是getApplicationContext()。
然后使用:
分配locloc = new Location(getApplicationContext());
这应该至少解决你的NullPointerException。
由于地址类型不受支持,您的SocketException似乎正在发生,但是根据您发布的代码,您似乎根本没有使用任何地址。