Android:FAQ

From Juneday education
Jump to: navigation, search


Build problems

Why do I get Cannot resolve symbol 'R' ?

Most likely you have some kind of error in an XML file, e g activity_main.xml. Fix this problem and rebuild the project:

  • Build->Clean project
  • Build->Rebuild project

Why do I get Cannot resolve symbol 'button' ?

Most likely you in your Java code are referring to a view you think is defined in an XML file, e g activity_main.xml.

If your Java code looks like this:

        EditText et = findViewById(R.id.textField);
        Button barilla = findViewById(R.id.button);
        Button b = (Button) view;

And you get a compilation error saying Cannot resolve symbol 'button' associated with the highlighted line. Then make sure your layout has defined button:

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="helloPressed"
        android:text="ManUtd loosers"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Run time problems

What does Could not find method ... mean?

Most likely you've added an OnClickListener, using either XML or the Design view, to a Button and at the same time forgotten to the actual method which will be called by Android when clicking (in runtime).

Check out the exercise [Android:Exercises_Android_basics#Add_an_action_to_the_view Add an action to the view] and the ones following that.

What does java.lang.ClassCastException mean?

It simply means you're doing a typecast which, in runtime, did not work since the object you're casting is not of the type you're casting to. As an exmaple you can't cast a Spinner to a Button.

This is usually caused by looking up, using findViewById using the wrong id.