在 Eclipse 中调试 android 代码的最佳方法是什么? [英] What is the best way to debug the android code in Eclipse?

查看:15
本文介绍了在 Eclipse 中调试 android 代码的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始接触 Eclipse,想知道是什么导致了我的应用程序出错.我想知道他们是否像 Visual Studio 那样.

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;
    }

}

推荐答案

你的问题是这一行

SharedPreferences sp = getSharedPreferences(prefName, MODE_PRIVATE);

getSharedPreferences() 使用一个 Context,所以你不能在 onCreate() 运行之前调用它.尝试像这样更改您的代码

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();
}

至于阅读 logcat 以这个例子

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)

在你看到 FatalException 后寻找第一行,如 Caused by

After you see FatalException look for the first line like Caused by

这会告诉您您的例外是什么.在这个例子中,NullPointerException.然后查找引用您的项目的第一行.这是 at com.example.project.MainActivity.updateWithNewLocation(MainActivity.java:290).这告诉我们异常发生在 MainActivity 的第 290 行,这是最好的起点.您可能必须从这里追溯到它,但这通常是您的问题所在.

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

这篇关于在 Eclipse 中调试 android 代码的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆