Android-Specific Debugging

Most Android debugging is just like Java debugging. However, you will run into issues with Android-specific parts, such as resources, that the Java compiler knows nothing about. This is where Android Lint comes in.

Using Android Lint

Android Lint (or just “Lint”) is a static analyzer for Android code. A static analyzer is a program that examines your code to find defects without running it. Lint uses its knowledge of the Android frameworks to look deeper into your code and find problems that the compiler cannot. In most cases, Lint’s advice is worth taking.

In Chapter 6, you will see Lint warn you about compatibility problems. Lint can also perform type-checking for objects that are defined in XML. Make the following casting mistake in QuizActivity.

Listing 4.7  A simple mix-up (QuizActivity.java)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate(Bundle) called");
    setContentView(R.layout.activity_quiz);
    ...
    mQuestionTextView = (TextView)findViewById(R.id.question_text_view);

    mTrueButton = (Button)findViewById(R.id.true_button);
    mTrueButton = (Button)findViewById(R.id.question_text_view);
    ...
}

Because you used the wrong resource ID, this code will attempt to cast a TextView as a Button at runtime. This will cause an improper cast exception. The Java compiler sees no problem with this code, but Lint will catch this error. You should see Lint immediately highlight this line of code to indicate that there is a problem.

You can manually run Lint to see all of the potential issues in your project, including those that are not as serious as the one above. Select AnalyzeInspect Code... from the menu bar. You will be asked which parts of your project you would like to inspect. Choose Whole project and click OK. Android Studio will now run Lint as well as a few other static analyzers on your code.

Once the scan is complete, you will see a few categories of potential issues in the inspection tool window. Expand the Android Lint categories to see Lint’s information about your project (Figure 4.8).

Figure 4.8  Lint warnings

Screenshot shows Lint Warnings in Inspection Results for Inspection Profile 'Project Default' window.

You can select an issue in this list to see more detailed information and its location in your project.

The Mismatched view type warning in the Android > Lint > Correctness category is the one that you created above. Go ahead and correct the cast in onCreate(Bundle).

Listing 4.8  Fixing that simple mix-up (QuizActivity.java)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate(Bundle) called");
    setContentView(R.layout.activity_quiz);

    mQuestionTextView = (TextView)findViewById(R.id.question_text_view);

    mTrueButton = (Button)findViewById(R.id.question_text_view);
    mTrueButton = (Button)findViewById(R.id.true_button);
    ...
}

Run GeoQuiz once more and confirm that the app is back to normal.

Issues with the R class

You are familiar with build errors that occur when you reference resources before adding them or delete resources that other files refer to. Usually, resaving the files once the resource is added or the references are removed will cause Android Studio to rebuild without any fuss.

Sometimes, however, these build errors will persist or appear seemingly out of nowhere. If this happens to you, here are some things you can try:

Recheck the validity of the XML in your resource files

If your R.java file was not generated for the last build, you will see errors in your project wherever you reference a resource. Often, this is caused by a typo in one of your XML files. Layout XML is not always validated, so typos in these files may not be pointedly brought to your attention. Finding the typo and resaving the file should cause R.java to regenerate.

Clean your project

Select BuildClean Project. Android Studio will rebuild the project from scratch, which often results in an error-free build. We can all use a deep clean every now and then.

Sync your project with Gradle

If you make changes to your build.gradle file, you will need to sync those changes to update your project’s build settings. Select ToolsAndroidSync Project with Gradle Files. Android Studio will rebuild the project from scratch with the correct project settings, which can help to resolve issues after changing your Gradle configuration.

Run Android Lint

Pay close attention to the warnings from Lint. With this tool, you will often discover unexpected issues.

If you are still having problems with resources (or having different problems), give the error messages and your layout files a fresh look. It is easy to miss mistakes in the heat of the moment. Check out any Lint errors and warnings as well. A cool-headed reconsideration of the error messages may turn up a bug or typo.

Finally, if you are stuck or having other issues with Android Studio, check the archives at stackoverflow.com or visit the forum for this book at forums.bignerdranch.com.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.38.3