Your problem here is this line
SharedPreferences sp = getSharedPreferences(prefName, MODE_PRIVATE);
getSharedPreferences()
uses a Context
so you can't call this until onCreate()
has run. Try changing your code like this
public class MainActivity extends Activity {
Set<String> tasks = new HashSet<String>();
final String prefName = "andorid";
SharedPreferences sp; // you can declare it here
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp = getSharedPreferences(prefName, MODE_PRIVATE); // but don't initialize it until at least here
SetupApp();
}
As far as reading logcat take this example
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): FATAL EXCEPTION: main
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paad.whereami/com.paad.whereami.WhereAmI}: java.lang.NullPointerException
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): at android.os.Looper.loop(Looper.java:130)
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): at java.lang.reflect.Method.invoke(Method.java:507)
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): at dalvik.system.NativeStart.main(Native Method)
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): Caused by: java.lang.NullPointerException
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): at com.example.project.MainActivity.updateWithNewLocation(MainActivity.java:290)
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): at com.example.project.MainActivity.onCreate(MainActivity.java:216)
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-18 18:29:44.160: ERROR/AndroidRuntime(2145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
After you see FatalException
look for the first line like Caused by
This tells you what your exception is. In this example, NullPointerException
. Then look for the first line that references your project. Here it is at com.example.project.MainActivity.updateWithNewLocation(MainActivity.java:290)
. This tells us that the exception occurred at line 290 of MainActivity
and this is the best place to start. You may have to trace it back from here but this is generally where your problem is.
I grabbed this stacktrace from another question, hope no one minds, but this should give you a general idea of how to debug your app. You still may not understand exactly why or where it happened but this will better prepare you to ask a question so you can post the most relevant code and give it a good go. Hope this helped
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…