簡體   English   中英

嘗試 10 次后,我的 android 應用程序崩潰

[英]My android application crashes after 10 tries

剛開始使用 Android,我的問題出在我正在制作的游戲中。 所以我用狗的圖片填充圖像視圖,用戶必須在其中猜測品種。 所以我想出了如何填充它,但現在我遇到了這個問題,我只能在應用程序崩潰之前重新填充 imageView 10 次。

要非常描述,

我啟動應用程序,打開將您推入顯示圖片的活動的部分,您必須選擇您認為的品種

我點擊提交轉到下一張圖片(rn它不檢查答案是否正確。它只是再次運行設置並嘗試重新填充圖像視圖)

所以我繼續這樣做,經過 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();
    }

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; .

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM