What is the best way to debug the android code in Eclipse?

Solution 1:

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

Solution 2:

The best way to debug android using eclipse is to use breakpoints, catch exceptions, and use the log cat.

Set breakpoints like you have been doing to try and locate the error.

If the breakpoints aren't working then you can view the log cat and it may give you more information such as the line the error occurred on in your code.

Then you could try catching exceptions:

try{
  //do some stuff
}catch(Exception e){
  Log.wtf("A terrible error occured.", e);
}

You could also check this post How to Debug Android application line by line using Eclipse? which tells you about more debugging methods.