刚开始使用Android,我的问题出在我正在开发的游戏中。因此,我用狗的图片填充了图像视图,用户必须在其中猜测其品种。所以我想出了如何填充它,但是现在我遇到了这个问题,在该应用崩溃之前,我只能重新填充imageView 10次。
描述性很强
我启动应用程序,打开将您带入显示照片的活动部分,您必须选择您认为是的品种
我单击“提交”以转到下一张图片(它没有检查答案是否正确。它只是再次运行设置并尝试重新填充“图像视图”)
因此,我继续执行此操作,并且在尝试了10次之后,该应用程序退出了该活动并返回到菜单活动。在这一点上,我尝试再次进入游戏。
此时。它可以让我单击一次提交,也可以单击几次甚至更多次,然后又一次又一次崩溃。
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();
}
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)
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
答案 0 :(得分:1)
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个项目的资源查找失败。也许您缺少适当的drawable。
我建议您取消注释IllegalArgumentException
行,并查看是否在您的方案中抛出该行。这将确认失败的1
查找来自return RES_ID+1;
。