问题
I'm upgrading the UI of a game that I built some time ago and am heading toward Google's Material Design. As part of the code upgrade, I installed the backwards compatibility support library, android-support-v7-appcompat, into my Eclipse project and extended the main activity to AppCompatActivity, like so:
public class FivetoGo extends AppCompatActivity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
.
.
.
but when I call setSupportActionBar(), I get the above error. Here's main.xml:
<?xml version='1.0' encoding='UTF-8' ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff6d7fe2"
android:minHeight="?android:attr/actionBarSize"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp" >
</android.support.v7.widget.Toolbar>
<Button android:id="@+id/intro_button"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:text="@string/rules"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/secret_word_label"
android:layout_below="@id/intro_button"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:textStyle="bold"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/my_secret_word"/>
<EditText android:id="@+id/my_secret_word_text"
android:layout_below="@id/intro_button"
android:layout_toRightOf="@id/secret_word_label"
android:layout_alignBaseline="@id/secret_word_label"
android:inputType="textNoSuggestions"
android:capitalize="none"
android:maxLength="5"
android:textSize="20sp"
android:layout_width="100dp"
android:layout_height="wrap_content"/>
<Button android:id="@+id/done_button_main"
android:layout_below="@id/intro_button"
android:layout_toRightOf="@id/my_secret_word_text"
android:layout_alignBaseline="@id/my_secret_word_text"
android:enabled="false"
android:layout_margin="10dp"
android:text="@string/done_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/language_label"
android:layout_below="@id/my_secret_word_text"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="20dp"
android:textSize="20sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/language"/>
<Spinner android:id="@+id/language_list"
android:layout_below="@id/my_secret_word_text"
android:layout_toRightOf="@id/language_label"
android:layout_alignBaseline="@id/language_label"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:prompt="@string/language"/>
<Button android:id="@+id/word_list_button"
android:layout_below="@id/my_secret_word_text"
android:layout_toRightOf="@id/language_list"
android:layout_alignBaseline="@id/language_list"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dip"
android:text="@string/word_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/level_label"
android:layout_below="@id/word_list_button"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="20dp"
android:textSize="20sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/level"/>
<Spinner android:id="@+id/level_spinner"
android:layout_below="@id/language_list"
android:layout_toRightOf="@id/level_label"
android:layout_alignBaseline="@id/level_label"
android:layout_marginTop="30dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button android:id="@+id/quit_button"
android:layout_alignParentLeft="true"
android:layout_below="@id/level_spinner"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="20dp"
android:text="@string/quit_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Can't see what I'm doing wrong. Thanks for any help.
回答1:
Change:
import android.widget.Toolbar;
to:
import android.support.v7.widget.Toolbar;
in the Java source code that presumably appears above what you have shown in your question. :-)
来源:https://stackoverflow.com/questions/32633867/getting-the-method-setsupportactionbartoolbar-in-the-type-appcompatactivity-i