gpt4 book ai didi

java - 我的 Android 应用程序在 10 次尝试后崩溃

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

刚刚开始使用 Android,我的问题出在我正在制作的游戏上。因此,我用狗的图片填充 ImageView ,用户必须猜测狗的品种。所以我想出了如何填充它,但现在我遇到了这个问题,在应用程序崩溃之前我只能重新填充 imageView 10 次。

非常具有描述性,

我启动应用程序,打开将您插入此 Activity 的部分,其中显示图片,您必须选择您认为是的品种

我单击提交转到下一张图片(它不检查答案是否正确。它只是再次运行设置并尝试重新填充 ImageView )

所以我继续这样做,经过 10 次尝试,应用程序退出 Activity 并返回到菜单 Activity。此时我尝试再次进入游戏。

此时此刻。它要么允许我单击“提交”一次,要么单击几次,或者可能多次,但然后一次又一次完全崩溃。

错误代码


E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.doggyroulette, PID: 17359
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:7339)
at android.widget.TextView.performClick(TextView.java:14226)
at android.view.View.performClickInternal(View.java:7305)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27787)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7094)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:7339) 
at android.widget.TextView.performClick(TextView.java:14226) 
at android.view.View.performClickInternal(View.java:7305) 
at android.view.View.access$3200(View.java:846) 
at android.view.View$PerformClick.run(View.java:27787) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7094) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975) 
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x1
at android.content.res.ResourcesImpl.getValueForDensity(ResourcesImpl.java:237)
at android.content.res.Resources.getDrawableForDensity(Resources.java:902)
at android.content.res.Resources.getDrawable(Resources.java:842)
at android.content.Context.getDrawable(Context.java:628)
at com.example.doggyroulette.IdentifyBreedActivity.appSetup(IdentifyBreedActivity.java:146)
at com.example.doggyroulette.IdentifyBreedActivity.submitAnswer(IdentifyBreedActivity.java:167)
at java.lang.reflect.Method.invoke(Native Method) 
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
at android.view.View.performClick(View.java:7339) 
at android.widget.TextView.performClick(TextView.java:14226) 
at android.view.View.performClickInternal(View.java:7305) 
at android.view.View.access$3200(View.java:846) 
at android.view.View$PerformClick.run(View.java:27787) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7094) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975) 

主要代码

private void appSetup(){

breeds = getResources().getStringArray(R.array.breeds);
int randInt;

//Hides Next Button
nextBtn = findViewById(R.id.nextBtn);
nextBtn.setVisibility(View.INVISIBLE);

//Spinner Setup
spinner = findViewById(R.id.breedDropdown);

// ArrayAdapter to pass String Array list to Spinner, uses spinner_container.xml as layout
ArrayAdapter<CharSequence> spinnerArrayAdapter = ArrayAdapter.createFromResource(this, R.array.breeds, R.layout.spinner_container);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);


//Setup for Image View

randInt = AppUtility.getRandomNumber(1, 10);
Log.i("Rand_ID", String.valueOf(randInt));


String filePath = "";
correctName = breeds[randInt];
switch (breeds[randInt]){

case "Australian Terrier":
Log.i("Breed", breeds[randInt]);
filePath = "at_"+randInt;

case "Beagle":
Log.i("Breed", breeds[randInt]);
filePath = "b_"+randInt;
break;

case "Boxer":
Log.i("Breed", breeds[randInt]);
filePath = "boxer_"+randInt;
break;

case "Chihuahua":
Log.i("Breed", breeds[randInt]);
filePath = "chihuahua_"+randInt;
break;

case "Cockerspaniel":
Log.i("Breed", breeds[randInt]);
filePath = "cs_"+randInt;
break;

case "Doberman":
Log.i("Breed", breeds[randInt]);
filePath = "db_"+randInt;
break;

case "German Shepherd":
Log.i("Breed", breeds[randInt]);
filePath = "gs_"+randInt;
break;

case "Golden Retriever":
Log.i("Breed", breeds[randInt]);
filePath = "gr_"+randInt;
break;

case "Great Dane":
Log.i("Breed", breeds[randInt]);
filePath = "gd_"+randInt;
break;

case "Labrador Retriever":
Log.i("Breed", breeds[randInt]);
filePath = "lr_"+randInt;
break;

case "Maltese Dog":
Log.i("Breed", breeds[randInt]);
filePath = "md_"+randInt;
break;

case "Saint Bernard":
Log.i("Breed", breeds[randInt]);
filePath = "sb_"+randInt;
break;

case "Schipperke":
Log.i("Breed", breeds[randInt]);
filePath = "schipperke_"+randInt;
break;

case "Siberian Husky":
Log.i("Breed", breeds[randInt]);
filePath = "sh_"+randInt;
break;

case "Tibetan Mastiff":
Log.i("Breed", breeds[randInt]);
filePath = "tm_"+randInt;
break;

default:
Log.i("Error", "Not Found");
}


final ImageView imageView = findViewById(R.id.questionImage);
imageView.setImageDrawable(getDrawable(AppUtility.getResourceID(filePath,this.getApplicationContext())));


}

@Override
public void onBackPressed() {
finish();
}



public void submitAnswer(View view) {
// Unhides Next Button after question has been submitted
if(spinner.getSelectedItem().toString().equalsIgnoreCase(correctName )){

}

nextBtn = findViewById(R.id.nextBtn);
nextBtn.setVisibility(View.VISIBLE);

appSetup();
}

AppUtility类

import android.content.Context;

class AppUtility {

static int getResourceID(final String resource, final Context appContext) {

final int RES_ID = appContext.getResources().getIdentifier(resource, "drawable",
appContext.getApplicationInfo().packageName);
if (RES_ID == 0) {
// throw new IllegalArgumentException("No application resource found at : " + resource);
return RES_ID+1;
}else {
return RES_ID;
}
}


static int getRandomNumber(int min, int max){
int returnNum = (int)Math.round(Math.random()*max);

while(returnNum<min){
returnNum = (int)Math.round(Math.random()*max);
}

return returnNum;
}
}

更新:

因此,在我的 getResourceID() 方法中取消注释 throw new IllegalExceptionError 行后。我现在得到这一行错误,它响应传递给该方法的资源什么都没有??

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.doggyroulette, PID: 25960
java.lang.IllegalArgumentException: No application resource found at :
at com.example.doggyroulette.AppUtility.getResourceID(AppUtility.java:12)
at com.example.doggyroulette.IdentifyBreedActivity.appSetup(IdentifyBreedActivity.java:152)
at com.example.doggyroulette.IdentifyBreedActivity.access$000(IdentifyBreedActivity.java:18)
at com.example.doggyroulette.IdentifyBreedActivity$1.onClick(IdentifyBreedActivity.java:186)
at android.view.View.performClick(View.java:7339)
at android.widget.TextView.performClick(TextView.java:14226)
at android.view.View.performClickInternal(View.java:7305)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27787)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7094)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)

这不可能是图像的问题,因为我记录了通过 Log Cat 传递到图像中的数字,这就是所经历的情况。最后一个数字是崩溃的地方。

2020-02-24 06:03:55.338 26898-26898/com.example.doggyroulette I/Rand_ID: 3
2020-02-24 06:03:59.716 26898-26898/com.example.doggyroulette I/Rand_ID: 7
2020-02-24 06:04:01.855 26898-26898/com.example.doggyroulette I/Rand_ID: 8
2020-02-24 06:04:03.455 26898-26898/com.example.doggyroulette I/Rand_ID: 5
2020-02-24 06:04:05.347 26898-26898/com.example.doggyroulette I/Rand_ID: 6

最佳答案

Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x1

您正在尝试查找 ID 为 1 的资源。鉴于资源的编号方式,不会有具有该 ID 的资源。

static int getResourceID(final String resource, final Context appContext) {

final int RES_ID = appContext.getResources().getIdentifier(resource, "drawable",
appContext.getApplicationInfo().packageName);
if (RES_ID == 0) {
// throw new IllegalArgumentException("No application resource found at : " + resource);
return RES_ID+1;
}else {
return RES_ID;
}
}

如果资源查找失败,您将返回1。所以,我的猜测是,您的第 10 个项目的资源查找失败。也许您缺少适当的可绘制对象。

我建议您取消注释 IllegalArgumentException 行,看看是否会在您的场景中抛出该异常。这将确认失败的 1 查找来自 return RES_ID+1;

关于java - 我的 Android 应用程序在 10 次尝试后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60368101/

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