简体   繁体   English

在Eclipse中调试android代码的最佳方法是什么?

[英]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. 我只是开始使用Eclipse,并想知道导致应用程序错误的原因。 I wonder if their is a way like Visual Studio. 我想知道它们是否像Visual Studio一样。

What I means is I have got Null pointer Exception but even after putting Breakpoint i am confused what cause error happen. 我的意思是我得到了Null指针异常,但是即使在放置Breakpoint之后,我也很困惑导致错误发生的原因。

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

}

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. getSharedPreferences()使用Context因此您必须在onCreate()运行之前才能调用它。 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 就阅读logcat为例

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 在看到FatalException之后,请查找第一行,例如Caused by

This tells you what your exception is. 这告诉您什么是例外。 In this example, NullPointerException . 在此示例中, NullPointerException Then look for the first line that references your project. 然后寻找引用您的项目的第一行。 Here it is at com.example.project.MainActivity.updateWithNewLocation(MainActivity.java:290) . 这里是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. 这告诉我们该异常发生在MainActivity第290行,这是最好的起点。 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 希望这有所帮助

The best way to debug android using eclipse is to use breakpoints, catch exceptions, and use the log cat. 使用eclipse调试android的最佳方法是使用断点,捕获异常并使用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? 您还可以查看这篇文章如何使用Eclipse逐行调试Android应用程序? which tells you about more debugging methods. 告诉您更多调试方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 包含调试代码的最佳方法? - Best Way to Include Debug Code? 如果应用程序具有多个模块并且每个模块都是单独的项目,那么在jboss和eclipse中调试Java应用程序的最佳方法是什么? - What is the best way to debug a java application in jboss and eclipse if application has multiple modules and each module is separate project 在Eclipse中分析Java应用程序的最佳方法是什么? - What is the best way to profile a Java application in Eclipse? 将Android源代码导入或打开到Eclipse或Android studio中的正确方法是什么 - What is the correct way to import or open the Android Source Code into Eclipse or Android studio 在调试模式下,eclipse中会自动反映哪些代码更改? - What code changes are automatically reflected in eclipse in debug mode? 在netbeans中调试客户端-服务器应用程序的最佳方法是什么? - What is the best way to debug a client-server app in netbeans? 在数据流中调试 DoFn 的 processElement 部分的最佳方法是什么 - What is the best way to debug the processElement part of the DoFn in dataflow Eclipse-Android-无法调试 - Eclipse - Android - Cannot debug 编码方法重载的最佳方法是什么? - What is the best way to code a method overloading? 分析创建UML的代码的最佳方法是什么? - What is the best way to analyze the code to create UML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM