Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

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

I just start my hands on Eclipse and want to know what cause error in my apps. I wonder if their is a way like Visual Studio.

What I means is I have got Null pointer Exception but even after putting Breakpoint i am confused what cause error happen.

Because I am new most of time I run bad or code which will not work. How I recognize my mistake. Sometime my code is not working and exception are hard to figure out the real issue happen with my code.

Do someone check the code and guide me how to find it. I means how to know where the null pointer exception happen.

public class MainActivity extends Activity {

    Set<String> tasks = new HashSet<String>();
    final String prefName = "andorid";
    SharedPreferences sp = getSharedPreferences(prefName, MODE_PRIVATE);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SetupApp();
    }

    // / Add the task in this function
    private void SetupApp() {

        tasks.add("blah");

        if (!sp.contains("tasks")) {
            Editor edit = sp.edit();

            edit.putStringSet("tasks", tasks);

            edit.commit();
        }

    }

    public void btnClick(View view) {

        EditText etxt = (EditText) findViewById(R.id.txtTask);

        tasks.add(etxt.getText().toString());

    }

    public void UpdateUI(String TaskName) {

        final ListView listview = (ListView) findViewById(R.id.listView1);

        Set<String> tasks = sp.getStringSet("tasks", new HashSet<String>());

        @SuppressWarnings("unchecked")
        ArrayList<String> taskarr = (ArrayList<String>) tasks;

        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < taskarr.size(); ++i) {
            list.add(taskarr.get(i));
        }
    }

    public void LoadTasks() {

    }

}

class StableArrayAdapter extends ArrayAdapter<String> {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public StableArrayAdapter(Context context, int textViewResourceId,
            List<String> objects) {
        super(context, textViewResourceId, objects);
        for (int i = 0; i < objects.size(); ++i) {
            mIdMap.put(objects.get(i), i);
        }
    }

    @Override
    public long getItemId(int position) {
        String item = getItem(position);
        return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...