提问人:Lord Firestorm 提问时间:3/20/2023 更新时间:3/20/2023 访问量:19
java.lang.String android.content.Context.getPackageName() 在空对象引用上
java.lang.String android.content.Context.getPackageName() on a null object reference
问:
我努力寻找我的代码的问题,但到目前为止我不是很幸运。谁能帮我解决它? 它被认为是一个儿童游戏,可以为显示的动物指定正确的名字。在原始文件中,图片被存储,在文本文件中,“AnimalNames”的每一行都有相应的动物名称。
代码如下:
package com.example.tiereraten;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Button button01, button02, button03, button04;
ImageView imageView;
ArrayList<String> animalNames = new ArrayList<String>();
ArrayList<Integer> animalPics = new ArrayList<Integer>();
Integer chosenAnimal = 0;
Integer locationOfCorrectAnswer, score, numberOfQuestion;
TextView resultTextView, scoreTextView;
String animalNamesText = "AnimalNames.txt";
ArrayList<String>answers = new ArrayList<String>(4);
// Get the Resources object
Resources res ;
AssetManager assetManager;
// Get the package name of your app
String packageName = getPackageName();
// Get the identifier of the drawable folder
int rawId;
// Get an array of all the drawable resource IDs
TypedArray images;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button01 = findViewById(R.id.button01);
button02 = findViewById(R.id.button02);
button03 = findViewById(R.id.button03);
button04 = findViewById(R.id.button04);
imageView = findViewById(R.id.animalImageView);
resultTextView = findViewById(R.id.resultTextView);
scoreTextView = findViewById(R.id.scoreTextView);
res = getResources();
rawId = res.getIdentifier("raw", "raw", packageName);
images = res.obtainTypedArray(rawId);
for (int i = 0; i < images.length(); i++) {
int resourceId = images.getResourceId(i, -1);
if (resourceId != -1) {
animalPics.add(resourceId);
}
}
try {
// Get the InputStream for the text file
InputStream inputStream = getAssets().open(animalNamesText);
// Create a new InputStreamReader to read the text file
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
// Create a new BufferedReader to read the text file line by line
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
// Read each line of the text file and add it to the ArrayList
String line;
while ((line = bufferedReader.readLine()) != null) {
animalNames.add(line);
}
// Close the BufferedReader
bufferedReader.close();
} catch (IOException e) {
// Handle the exception
e.printStackTrace();
}
reset(scoreTextView);
}
public void newQuestion(){
resultTextView.setText("Welches Tier ist es?");
Random rnd = new Random();
locationOfCorrectAnswer = rnd.nextInt(4);
chosenAnimal = rnd.nextInt(animalPics.size());
int resourceId = animalPics.get(chosenAnimal);
imageView.setImageResource(resourceId);
answers = new ArrayList<String>(Arrays.asList("", "", "", ""));
for (int i = 0; i < 4; i++) {
if (i == locationOfCorrectAnswer) {
answers.add(animalNames.get(chosenAnimal));
} else {
int wrongAnswerLocation = rnd.nextInt(animalNames.size());
while (wrongAnswerLocation == chosenAnimal) {
wrongAnswerLocation = rnd.nextInt(animalNames.size());
}
answers.set(i, animalNames.get(wrongAnswerLocation));
Log.i("Chosen wrong animalName", animalNames.get(wrongAnswerLocation));
}
}
button01.setText(answers.get(0));
Log.i("answer(0)",answers.get(0));
button02.setText(answers.get(1));
Log.i("answer(1)",answers.get(1));
button03.setText(answers.get(2));
Log.i("answer(2)",answers.get(2));
button04.setText(answers.get(3));
Log.i("answer(3)",answers.get(3));
}
public void choseAnswer (View view){
if(Integer.toString(locationOfCorrectAnswer).equals(view.getTag().toString())){
resultTextView.setText("Korrekt!");
score++;
}else{
resultTextView.setText("falsch");
}
try {
Thread.sleep(1000); // Sleep for one second (1000 milliseconds)
} catch (InterruptedException e) {
e.printStackTrace();
}
numberOfQuestion++;
scoreTextView.setText(Integer.toString(score) +"/"+ Integer.toString(numberOfQuestion));
newQuestion();
}
public void reset (View view){
score = 0;
numberOfQuestion = 0;
scoreTextView.setText(Integer.toString(score) +"/"+ Integer.toString(numberOfQuestion));
newQuestion();
}
}
我尝试在旧的 adroid 手机(棒棒糖版本 22)上调试并运行它。不幸的是,我无法在虚拟设备上运行它。
答: 暂无答案
评论