簡體   English   中英

Android弄亂了R.java,無法找到資源ID#0xffffffff

[英]Android messed up R.java unable to find resource ID #0xffffffff

我不小心在布局xml中將按鈕的ID設置為“ mybutton-1”。 當我在Android Studio中點擊“運行應用程序”時,它切換到R.java並出現錯誤

public static final int mybutton-1=0x7f0b00e1; 錯誤消息說有一個; 預期在-1之后。 所以我將布局xml中該按鈕的ID更改為“ myButton_a”,然后再次點擊運行應用。 這次我在mainActivity.java中遇到錯誤:

ArrayList<View> focusableViews = view.getFocusables(View.FOCUS_FORWARD);
for (int i = 0; i < focusableViews.size(); i++) {
     View actualView = focusableViews.get(i);
     String actualID = actualView.getResources().getResourceEntryName(actualView.getId()); // here
     ...
}

我查看了R.java,它按預期將我的更改更新為public static final int myButton_a=0x7f0b00e1;

這是堆棧跟蹤:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: myApp.mainActivity, PID: 31364
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{myApp.mainActivity/myApp.mainActivity.mainLayout}: android.content.res.Resources$NotFoundException: Unable to find resource ID #0xffffffff
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
                      at android.app.ActivityThread.access$1100(ActivityThread.java:222)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:158)
                      at android.app.ActivityThread.main(ActivityThread.java:7229)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                   Caused by: android.content.res.Resources$NotFoundException: Unable to find resource ID #0xffffffff
                      at android.content.res.Resources.getResourceEntryName(Resources.java:3738)
                      at android.support.v7.widget.ResourcesWrapper.getResourceEntryName(ResourcesWrapper.java:264)
                      at myApp.mainActivity.doStuff(mainActivity.java:341)
                      at myApp.mainActivity.onCreate(mainActivity.java:71)
                      at android.app.Activity.performCreate(Activity.java:6876)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 
                      at android.app.ActivityThread.access$1100(ActivityThread.java:222) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:158) 
                      at android.app.ActivityThread.main(ActivityThread.java:7229) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

我怎么解決這個問題? 我讀到我必須手動設置ID,但是還有另一種方法嗎? 我不想再搞亂ID

如果您的視圖沒有ID,則getId()將返回-1。 這樣就變成了0xffffffff。

看來你的focusableViews不僅包含mybutton1, 但也有一些別人沒有的ID。 那就是問題所在。

暫無
暫無

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

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