gpt4 book ai didi

java - Android 开发 : "R cannot be resolved", 但仅在类中的一个上下文中

转载 作者:行者123 更新时间:2023-12-02 00:48:08 25 4
gpt4 key购买 nike

我在代码中相当严格地遵循 Lauren Darcy 的《24 小时自学 Android 应用程序开发》。我遇到了“R 无法解析”的问题。一行:

final View layout=inflater.inflate(R.layout.password_dialog, (ViewGroup) findViewById(R.id.root));

“R 无法解析”在此行中出现两次,每次引用 R 一次。

  • 如果我复制并粘贴作者的代码,我会得到相同的结果,因此这不是拼写错误
  • 从阅读其他遇到同样问题的人的帖子来看,听起来 R 在类中的任何地方都没有得到解决。不过,我确实在本类(class)的其他上下文中使用 R,没有任何异议,并且取得了预期的结果。
  • 另一个常见的建议是验证我没有导入 android.R。我不是。

这里无计可施......我应该看哪里?

类(class)完整:

package com.oneinfinity.btdt;

import java.util.Calendar;


import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.text.format.Time;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class QuizSettingsActivity extends QuizActivity {
SharedPreferences mGameSettings;
static final int DATE_DIALOG_ID=0;
static final int PASSWORD_DIALOG_ID=1;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
mGameSettings=getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
initTextField(R.id.etNickname, GAME_PREFERENCES_NICKNAME);
initTextField(R.id.etEmail, GAME_PREFERENCES_EMAIL);
writeBirthday();
Button setPass=(Button) findViewById(R.id.ButtonPassword);
Button setBd=(Button) findViewById(R.id.ButtonBirthday);
setPass.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//TODO - password dialog
Toast.makeText(QuizSettingsActivity.this, "TODO: Password form", Toast.LENGTH_LONG).show();
}
});
setBd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//TODO - birthday dialog
//Toast.makeText(QuizSettingsActivity.this, "TODO: birthday form", Toast.LENGTH_LONG).show();
showDialog(DATE_DIALOG_ID);
}
});
setSpinner(R.id.spinnerGender, R.array.genderSet, GAME_PREFERENCES_GENDER);
}

private void setSpinner(int whichSpinner, int whichArray, String whichPref) {
final String PutString=whichPref;
final Spinner spinner=(Spinner) findViewById(whichSpinner);
ArrayAdapter<?> adapter=ArrayAdapter.createFromResource(this, whichArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
if(mGameSettings.contains(PutString)) {
int toSelect=mGameSettings.getInt(PutString, 0);
spinner.setSelection(toSelect);
}
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View itemSelected, int selectedItemPosition, long selectedId) {
Editor editor = mGameSettings.edit();
editor.putInt(PutString, selectedItemPosition);
editor.commit();
//test whether committed
if(mGameSettings.contains(PutString)) {
Log.i("trace", "gender is set to " + mGameSettings.getInt(PutString, 0)+"");
}
else {
Log.i("trace", "gender has not been specied yet");
}
}
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(QuizSettingsActivity.this, "TODO:handle nothing selected", Toast.LENGTH_LONG).show();
}
});
}


private void initTextField(int whichField, String whichSetting) {
final String PutSetting=whichSetting;
final EditText findField=(EditText) findViewById(whichField);
if(mGameSettings.contains(PutSetting)) {
String textString=mGameSettings.getString(PutSetting, "");
findField.setText(textString);
}
findField.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if((event.getAction()==KeyEvent.ACTION_DOWN) && (keyCode==KeyEvent.KEYCODE_ENTER)) {
String strValueToSave=findField.getText().toString();
Editor editor = mGameSettings.edit();
editor.putString(PutSetting, strValueToSave);
editor.commit();
return true;
}
return false;
}
});

}

/* Just in case it really is necessary to repeat these instructions
private void initnicknameEntry() {
final EditText nicknameText=(EditText) findViewById(R.id.etNickname);
nicknameText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if((event.getAction()==KeyEvent.ACTION_DOWN) && (keyCode==KeyEvent.KEYCODE_ENTER)) {
String strValueToSave=nicknameText.getText().toString();
return true;
}
return false;
}
});
}
*/

@Override
protected Dialog onCreateDialog(int id) {
switch(id) {
case DATE_DIALOG_ID:
DatePickerDialog dateDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
TextView dob=(TextView) findViewById(R.id.TextView_Bday_Info);
Time dateOfBirth=new Time();
dateOfBirth.set(dayOfMonth, monthOfYear, year);
long dtDob = dateOfBirth.toMillis(true);
dob.setText(DateFormat.format("MMMM dd, yyyy", dtDob));
Editor editor=mGameSettings.edit();
editor.putLong(GAME_PREFERENCES_DOB, dtDob);
editor.commit();

}
}, 0, 0, 0 );
return dateDialog;
case PASSWORD_DIALOG_ID:
//create the dialog
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout=inflater.inflate(R.layout.password_dialog, (ViewGroup) findViewById(R.id.root));
//return the dialog;
}
return null;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch(id) {
case DATE_DIALOG_ID:
DatePickerDialog dateDialog = (DatePickerDialog) dialog;
int iDay, iMonth, iYear;
if(mGameSettings.contains(GAME_PREFERENCES_DOB)) {
long bd=mGameSettings.getLong(GAME_PREFERENCES_DOB, 0);
Time birthday=new Time();
birthday.set(bd);
iDay=birthday.monthDay;
iMonth=birthday.month;
iYear=birthday.year;
Log.i("trace", "verifying that it is no longer the year 2");
}
else {
Calendar cal=Calendar.getInstance();
iDay=cal.get(Calendar.DAY_OF_MONTH);
iMonth=cal.get(Calendar.MONTH);
iYear=cal.get(Calendar.YEAR);
}
dateDialog.updateDate(iYear, iMonth, iDay);
return;
case PASSWORD_DIALOG_ID:
//prepare the dialog
return;
}
}

private void writeBirthday() {
TextView dob=(TextView) findViewById(R.id.TextView_Bday_Info);
CharSequence myBirthday=getResources().getString(R.string.no_birthday);
if(mGameSettings.contains(GAME_PREFERENCES_DOB)) {
long bd=mGameSettings.getLong(GAME_PREFERENCES_DOB, 0);
myBirthday=DateFormat.format("MMMM dd, yyyy", bd);
}
dob.setText(myBirthday);
}
}

最佳答案

尝试转到“项目”->“清理”,然后运行它。然后转到“项目”->“构建项目”。您可能需要先取消选中“自动构建”。

关于java - Android 开发 : "R cannot be resolved", 但仅在类中的一个上下文中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4340634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com