我是一个初学者到应用程序开发,我有这个第一个项目,我试图建立一个问答游戏。我一直试图运行这个应用程序的最长时间可能。我试图编辑我的Android manifest.xml根据这里的解决方案,但没有工作
C:\用户\管理员\桌面\教育应用\应用\src\main\java\com\示例\趣味拼写\游戏活动。java:48:错误:包R不存在setContentView(R. layout. activity_game
下面是我的清单. xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sinanli.funspelling">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.sinanli.funspelling.MainActivity" />
</activity>
<activity
android:name=".GameActivity"
android:label="@string/title_activity_game"
android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.sinanli.funspelling.MainActivity" />
</activity>
<activity
android:name=".ScoreActivity"
android:label="@string/title_activity_score"
android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.sinanli.funspelling.MainActivity" />
</activity>
</application>`
</manifest>
我的www.example.com出现错误activity.java with the error
package com.example.funspelling;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.R;
import java.util.Collections;
import java.util.List;
public class GameActivity extends AppCompatActivity {
private TextView questionT;
private ImageView foodImage;
private TextView timeT;
private EditText inputT;
private Button buttonC;
private CountDownTimer count;
private SharedPreferences prefs;
private AssetManager assetManager;
private SoundManager soundManager;
private int clap;
private String rightAnswer;
private String alertTitle;
private int rightAnswerCount;
private int quizCount = 1;
private List<String> foodNames;
private String difOption;
private String musicOption;
static private final int QUIZ_COUNT = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
questionT = (TextView) findViewById(R.id.questionText);
foodImage = (ImageView) findViewById(R.id.foodImage);
timeT = (TextView) findViewById(R.id.timeText);
inputT = (EditText) findViewById(R.id.inputText);
buttonC = (Button) findViewById(R.id.button);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
difOption = prefs.getString("difSelection","1");
assetManager = new AssetManager(this, difOption);
foodNames = assetManager.allFoodNames(difOption);
soundManager = new SoundManager(this);
clap = soundManager.addSound(R.raw.clap);
musicOption = prefs.getString("musicSelection", "On");
showNextQuiz();
count = new CountDownTimer(10 * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timeT.setText("Seconds Remaining: " + millisUntilFinished / 1000);
}
@Override
public void onFinish() {
timeT.setText("Done");
buttonC.performClick();
}
};
count.start();
}
public void showNextQuiz () {
questionT.setText("Question " + quizCount + " Out of " + QUIZ_COUNT);
Collections.shuffle(foodNames);
foodImage.setImageDrawable(assetManager.imageForFood(foodNames.get(0)));
rightAnswer = foodNames.get(0);
foodNames.remove(0);
Collections.shuffle(foodNames);
}
public void checkAnswer(View view) {
String answer = inputT.getText().toString();
inputT.setText("");
if (answer.equals(rightAnswer)){
alertTitle = "Correct!";
rightAnswerCount++;
if (musicOption.equals("On")){
soundManager.play(clap);
}
} else {
alertTitle = "Wrong...";
}
prefs = getSharedPreferences("value", MODE_PRIVATE);
prefs.edit().putInt("Score", rightAnswerCount * 10).apply();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
if (quizCount == QUIZ_COUNT) {
count.cancel();
builder.setTitle("Game Over");
builder.setMessage("Score: " + rightAnswerCount * 10);
builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
} else {
count.cancel();
builder.setTitle(alertTitle);
builder.setMessage("Answer : " + rightAnswer);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
quizCount++;
showNextQuiz();
count = new CountDownTimer(10 * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timeT.setText("Seconds Remaining: " + millisUntilFinished / 1000);
}
@Override
public void onFinish() {
timeT.setText("Done");
buttonC.performClick();
}
};
count.start();
}
});
}
builder.setCancelable(false);
builder.show();
}
}
我最初在intellij idea中制作了这个项目,我的gradle最初是4.2,当我改用android studio时,它为我提供了升级到7.4的选项
一次都没成功
1条答案
按热度按时间v8wbuo2f1#
删除
import android.R
行-这是系统资源,因此当您键入setContentView(R.layout.activity_game)
时,它将扩展为而且
android.R
资源中不存在该ID。您实际需要的是应用资源中的ID:
但是因为你的文件使用了这个包声明:
你已经“在”
com.example.funspelling
里面了,如果这是有意义的话。所以引用像R.layout
这样的东西是相对于那个包的,你不需要再次显式地包含这个包。这就是为什么你总是写R.layout.whatever
。但是如果你导入
android.R
,这意味着 “每当我引用R
时,我的意思是android.R
“,这会覆盖 “在当前包中查找” 的行为。如果你曾经想在
android.R
中使用一些东西,而不是导入它,你可能想在引用它的时候使用完整的显式包:这样,您的命名空间就不会被导入所污染,并且所有基本的
R
引用都是相对于应用包的,这正是您想要的!