当我运行这个应用程序时,我正在开发一个测验应用程序我收到错误android.content.res.Resources $ NotFoundException:将最小sdk从16更改为19之后的字符串资源ID#0x0。请帮我运行此应用程序< / p>
这是我的activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.awwalsoft.javaplay.MainActivity">
<LinearLayout
android:id="@+id/data_list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="0dp">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view1"
android:layout_weight="2"
android:layout_width="0dp"
android:onClick="startGame"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:layout_marginTop="@dimen/eightdpmargin"
android:src="@drawable/play"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"/>
<TextView
android:text="@string/start"
android:gravity="center"
android:fontFamily="sans-serif-light"
android:textSize="@dimen/twozerotext"
android:textColor="@android:color/black"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view1_1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:background="@drawable/scoreboard"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/highscore"
android:gravity="center"
android:textSize="@dimen/textsizebutton"
android:layout_marginTop="@dimen/eightdpmargin"
android:layout_weight="2"/>
<TextView
android:text="@string/highscore"
android:gravity="center"
android:fontFamily="sans-serif-light"
android:textSize="@dimen/twozerotext"
android:textColor="@android:color/black"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<TextView
android:text="@string/useapps"
android:layout_marginTop="@dimen/eightdpmargin"
android:textColor="@color/colorPrimaryDark"
android:textSize="@dimen/sptextsize2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:layout_weight="3"
android:layout_marginTop="@dimen/eightdpmargin"
android:layout_width="match_parent"
android:layout_height="0dp">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view2"
android:layout_width="@dimen/minhigh"
android:onClick="javaBasicsLearningApp"
android:layout_marginEnd="@dimen/cardviewmarginright"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:layout_marginTop="@dimen/eightdpmargin"
android:src="@drawable/jbl"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"/>
<TextView
android:text="@string/jbl"
android:gravity="center"
android:fontFamily="sans-serif-light"
android:textSize="@dimen/twozerotext"
android:textColor="@android:color/black"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view2_1"
android:layout_width="@dimen/minhigh"
android:onClick="javaDeepLearningApp"
android:layout_marginEnd="@dimen/eightdpmargin"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_marginTop="@dimen/eightdpmargin"
android:src="@drawable/jdl"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"/>
<TextView
android:text="@string/jdl"
android:gravity="center"
android:fontFamily="sans-serif-light"
android:textSize="@dimen/twozerotext"
android:textColor="@android:color/black"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view2_2"
android:layout_width="@dimen/minhigh"
android:onClick="learnJdbcApp"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_marginTop="@dimen/eightdpmargin"
android:src="@drawable/jdbc"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"/>
<TextView
android:text="@string/ljdbc"
android:gravity="center"
android:fontFamily="sans-serif-light"
android:textSize="@dimen/twozerotext"
android:textColor="@android:color/black"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<LinearLayout
android:layout_marginTop="@dimen/eightdpmargin"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="0dp">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view3"
android:onClick="helpClick"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_marginTop="@dimen/eightdpmargin"
android:src="@drawable/help"
android:layout_width="0dp"
android:layout_margin="@dimen/sixteendppadding"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:text="@string/help"
android:gravity="center"
android:id="@+id/helptext"
android:fontFamily="sans-serif-light"
android:textSize="@dimen/textsizemain"
android:textColor="@android:color/black"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
这是我的MainActivity.java文件
package com.awwalsoft.javaplay;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String moreApps1 = "com.awwalsoft.javabasicslearning";
String moreApps2 = "com.awwalsoft.javadeeplearning";
String moreApps3 = "com.awwalsoft.learnjdbc";
private Player currentPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
currentPlayer = ((JavaPlay) getApplication()).player; //gets player from lightquiz application
updateHighScore();
}
public void javaBasicsLearningApp(View v){
try {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + moreApps1)));
} catch (android.content.ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + moreApps1)));
}
}
public void javaDeepLearningApp(View v){
try {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + moreApps2)));
} catch (android.content.ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + moreApps2)));
}
}
public void learnJdbcApp(View v){
try {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + moreApps3)));
} catch (android.content.ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + moreApps3)));
}
}
public void helpClick(View view){
Intent helpIntent = new Intent(this,Help.class);
startActivity(helpIntent);
}
@Override
public void onResume() {
super.onResume();
((JavaPlay) this.getApplicationContext()).clearQuestions();
updateHighScore();
}
@Override
public void onBackPressed() {
this.finish();
}
public void startGame(View view) {
Intent intent = new Intent(this, PlayGame.class);
startActivity(intent);
}
void selecGenre() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.dialog_title);
String[] genres = QuestionGenre.names();
builder.setItems(QuestionGenre.names(), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
beginQuizGame(QuestionGenre.names()[which]);
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
void beginQuizGame(String genre) {
Intent intent = new Intent(this, PlayGame.class);
intent.putExtra("Genre", genre);
startActivity(intent);
}
private void updateHighScore() {
TextView highScoreText = (TextView) findViewById(R.id.highscore);
highScoreText.setText(currentPlayer.getHighScore());
}
}
这是我的PlayGame.java文件
package com.awwalsoft.javaplay;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
public class PlayGame extends AppCompatActivity {
private final int questionsDelay = 500;
private final int questionsPoints = 10;
private final int questionSeconds = 8;
private int points;
private int lives;
private int correctAnswer;
//Layout Stuff
private ImageView correctImg;
private ImageView wrongImg;
private ImageView questionImg;
private TextView questionText;
private Button soundButton;
private TextView pointsText;
private TextView lifeText;
private TextView countdownText;
private Button[] answerButtons = new Button[4];
private CountDownTimer countdown;
private QuestionsGenerator generator;
private ProgressDialog progress;
private String genreSelection = null;
private boolean gameFinished = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_game);
Intent intent = getIntent();
genreSelection = intent.getStringExtra("Genre");
progress = new ProgressDialog(this);
progress.setMessage("Loading Database ");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.show();
new Thread(new Runnable() {
public void run() {
try {
loadQuestions();
} catch (IOException e) {
e.printStackTrace();
}
loadLayout();
runOnUiThread(new Runnable() {
public void run() {
startGame();
}
});
}
}).start();
}
@Override
public void onPause() {
super.onPause();
countdown.cancel();
hideQuestionMultimedia();
this.finish();
gameFinished = true;
// gameOver();
}
@Override
public void onBackPressed() {
// this.finish();
gameOver();
}
//an answer was clicked
public void answerClicked(View view) {
countdown.cancel();
int answer; //-1 by default
buttonsActive(false);
switch (view.getId()) {
case R.id.answer_1:
answer = 1;
break;
case R.id.answer_2:
answer = 2;
break;
case R.id.answer_3:
answer = 3;
break;
case R.id.answer_4:
answer = 4;
break;
default:
throw new RuntimeException("Unknown button ID");
}
if (correctAnswer == answer) correctAnswer();
else wrongAnswer();
updateTexts();
nextQuestion();
}
public void soundClick(View view) {
switch (view.getId()) {
case R.id.sound_button:
playSound();
break;
default:
throw new RuntimeException("Unknown button ID");
}
}
//set a new question from generator
private void playSound() {
if (!gameFinished)
((JavaPlay) this.getApplicationContext()).soundHandler.playQuestionSound();
}
private void setQuestion() {
if (!gameFinished) {
hideQuestionMultimedia();
buttonsActive(true);
Question quest = generator.getQuestion();//get a randomized question
if (!quest.isValid()) throw new RuntimeException("Invalid Question");
this.correctAnswer = quest.getCorrectAnswer();
for (int i = 0; i < 4; i++)
answerButtons[i].setText(quest.getAnswer(i)); //set questions layout
hideAnswerImage();
questionText.setText(quest.getText());
QuestionType qt = quest.type();
switch (qt) {
case TEXT:
//showQuestionText(((TextQuestion) quest).text);
break;
case IMAGE:
showQuestionImage(((ImageQuestion) quest).image);
break;
case SOUND:
showQuestionSound(((SoundQuestion) quest).sound);
playSound();
break;
default:
throw new RuntimeException("Invalid question type");
}
countdown = new CountDownTimer(questionSeconds * 1000, 500) {
@Override
public void onTick(long millisUntilFinished) {
countdownText.setText(Integer.toString((int) (millisUntilFinished / 1000) + 1));
/* if(millisUntilFinished/1000 == 1) {
wrongAnswer();
updateTexts();
nextQuestion();
}*/
}
@Override
public void onFinish() {
buttonsActive(false);
wrongAnswer();
updateTexts();
nextQuestion();
}
}.start();
}
}
private void showQuestionSound(String sound) {
soundButton.setVisibility(View.VISIBLE);
soundButton.setClickable(true);
((JavaPlay) this.getApplicationContext()).soundHandler.setQuestionSound(sound);
}
private void showQuestionImage(String imagename) {
int resourceId = this.getResources().getIdentifier(imagename, "drawable", this.getPackageName());
questionImg.setImageResource(resourceId);
questionImg.setVisibility(View.VISIBLE);
//questionImg.setMaxHeight();
}
private void hideQuestionMultimedia() {
questionImg.setVisibility(View.INVISIBLE);
soundButton.setVisibility(View.INVISIBLE);
((JavaPlay) this.getApplicationContext()).soundHandler.stopQuestionSound();
soundButton.setClickable(false);
}
//next question after som time
private void nextQuestion() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (generator.size() == 0) gameOver(); //no more questions left
else setQuestion();
}
}, questionsDelay);
}
//What happens when a correct answer was clicked
private void correctAnswer() {
points += questionsPoints;
wrongImg.setVisibility(View.INVISIBLE);
correctImg.setVisibility(View.VISIBLE);
((JavaPlay) this.getApplicationContext()).soundHandler.playCorrectSound();
}
//What happens when a wrong answer was clicked
private void wrongAnswer() {
correctImg.setVisibility(View.INVISIBLE);
wrongImg.setVisibility(View.VISIBLE);
((JavaPlay) this.getApplicationContext()).soundHandler.playWrongSound();
lives--;
if (lives == 0) gameOver();
}
//Hides any answer image (tick or cross)
private void hideAnswerImage() {
correctImg.setVisibility(View.INVISIBLE);
wrongImg.setVisibility(View.INVISIBLE);
}
private void buttonsActive(boolean b) {
for (Button button : answerButtons) {
button.setClickable(b);
}
}
private void loadLayout() {
correctImg = (ImageView) findViewById(R.id.correct_img);
wrongImg = (ImageView) findViewById(R.id.wrong_img);
pointsText = (TextView) findViewById(R.id.points_text);
lifeText = (TextView) findViewById(R.id.life_text);
countdownText = (TextView) findViewById(R.id.countdown_text);
questionImg = (ImageView) findViewById(R.id.question_image);
questionText = (TextView) findViewById(R.id.question);
soundButton = (Button) findViewById(R.id.sound_button);
answerButtons[0] = (Button) findViewById(R.id.answer_1);
answerButtons[1] = (Button) findViewById(R.id.answer_2);
answerButtons[2] = (Button) findViewById(R.id.answer_3);
answerButtons[3] = (Button) findViewById(R.id.answer_4);
hideAnswerImage();
}
//Updates life and score texts
private void updateTexts() {
lifeText.setText("Life:" + lives);
pointsText.setText("Score:" + points);
countdownText.setText("");
}
private void gameOver() {
//hideQuestionMultimedia();
gameFinished = true;
((JavaPlay) this.getApplicationContext()).soundHandler.stopQuestionSound();
soundButton.setClickable(false);
countdown.cancel();
buttonsActive(false);
Intent intent = new Intent(this, GameOver.class);
intent.putExtra("Score", points);
startActivity(intent);
this.finish();
}
//reloads questions from xml (old)
/* private void reload_questions() throws IOException, XmlPullParserException {
XmlResourceParser xmlq = getResources().getXml(R.xml.questions);
MainActivity.generator.loadQuestions(xmlq);
xmlq.close();
}*/
//Starts game
private void startGame() {
points = 0;
lives = 3;
gameFinished = false;
buttonsActive(true);
updateTexts();
setQuestion();
progress.dismiss();
Toast toast = Toast.makeText(this, "Quiz is Ready", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
View fore = findViewById(R.id.foreground);
fore.setVisibility(View.INVISIBLE);
}
private void loadQuestions() throws IOException {
if (!Question.isQuestionListReady()) {
((JavaPlay) this.getApplicationContext()).loadRawQuestions(genreSelection);
}
this.generator = new QuestionsGenerator();
}
}
答案 0 :(得分:0)
问题在于您将文本设置为int。比如下面的行
highScoreText.setText(currentPlayer.getHighScore());
如果getHighScore()返回int,则os将在string.xml中查找资源,该资源将为您提供 .Resources $ NotFoundException 。尝试使用它如下。
highScoreText.setText(String.valueOf(currentPlayer.getHighScore()));
或者
highScoreText.setText(currentPlayer.getHighScore()+"");