我在布局xml中意外地将按钮的ID设置为“mybutton-1”。当我在Android Studio中点击“运行应用程序”时,它在
处切换到R.java并出现错误 public static final int mybutton-1=0x7f0b00e1;
并且错误消息表示在-1之后预期有;
。所以我将布局xml中该按钮的id更改为“myButton_a”并再次点击运行app。这次我的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了
答案 0 :(得分:2)
如果您的视图没有ID,则getId()
将返回-1。所以它变成了0xffffffff。
您的focusableViews
似乎不仅包含mybutton1,,还包含其他一些没有ID 的人。这就是问题所在。