我得到"无法解析符号"在位于public boolean onCreateOptionsMenu(菜单菜单)下的菜单上。同样的错误出现在位于public boolean onOptionsItemSelected(MenuItem item)下的action_settings上。
这是.java代码:
package com.bignerdranch.android.geoquizz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class QuizActivity extends AppCompatActivity {
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private TextView mQuestionTextView;
private Question[] mQuestionBank = new Question[] {
new Question(R.string.question_oceans, true),
new Question(R.string.question_mideast, false),
new Question(R.string.question_africa, false),
new Question(R.string.question_americas, false),
new Question(R.string.question_asia, true),
};
private int mCurrentIndex = 0;
private void updateQuestion() {
int question = mQuestionBank [mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);
}
private void checkAnswer(boolean userPressedTrue) {
boolean answerisTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
int messageResId = 0;
if (userPressedTrue == answerisTrue) {
messageResId = R.string.correct_toast;
} else {
messageResId = R.string.incorrect_toast;
}
Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
int question = mQuestionBank[mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);
mTrueButton = (Button) mTrueButton.findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(true);
}
});
mNextButton = (Button) findViewById(R.id.next_buton);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateQuestion();
}
});
updateQuestion();
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(false);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.quiz, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的strings.xml代码:
<resources>
<string name="app_name">GeoQuizz</string>
<string name="true_button">True</string>
<string name="false_button">False</string>
<string name="next_button">Next</string>
<string name="action_settings">Settings</string>
<string name="correct_toast">Correct!</string>
<string name="incorrect_toast">Incorrect!</string>
<string name="question_oceans">The Pacific Ocean is larger than the Atlantic Ocean.</string>
<string name="question_mideast">The Suez Canal connects the Red Sea and the Indian Ocean.</string>
<string name="question_africa">The source of the Nile River is in Egypt.</string>
<string name="question_americas">The Amazon River is the longest river in the Americas.</string>
<string name="question_asia">Lake Baikal is the world\'s oldest and deepest freshwater lake.</string>
</resources>
答案 0 :(得分:0)
正如此处的建议,
https://github.com/ribot/android-guidelines/blob/master/project_and_code_guidelines.md
首先,您可以在menu.xml中设置您的ID(使用前缀&#39;菜单_&#39;到避免混淆含义)
<menu>
<item
android:id="@+id/menu_setting"
android:title="Done" />
</menu>
其次,更改onOptionsItemSelected()
方法
@Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if (id == R.id.menu_setting) {
return true;
}
return super.onOptionsItemSelected(item);
}
答案 1 :(得分:0)
确保您的quiz.xml位于正确的目录中 - &gt; res / menu文件夹