Android IllegalArgumentException: parameter must be a descendant of this view Error
- 引言
java.lang.IllegalArgumentException: parameter must be a descendant of this view
at android.view.ViewGroup.offsetRectBetweenParentAndChild(ViewGroup.java:4153)
at android.view.ViewGroup.offsetDescendantRectToMyCoords(ViewGroup.java:4090)
at android.view.ViewRootImpl.scrollToRectOrFocus(ViewRootImpl.java:2129)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:1849)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1641)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2449)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
当我尝试使用 Intent 或 finish() 返回此视图时,会发生此错误;方法。当我使用 Back Button 时,偶尔也会出现问题。
这是我的解决方案:
代码实现:
mViewFlow.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
或者:
android:descendantFocusability = afterDescendants
知识点:
他定义在查找 a 以获得焦点时 与其后代之间的关系。ViewGroup View
必须是以下常量值之一。
±-----------------------------------------------------------------------------------------+
| Constant Value Description |
±-----------------------------------------------------------------------------------------+
| afterDescendants 1 The ViewGroup will get focus only if |
| none of its descendants want it. |
±-----------------------------------------------------------------------------------------+
| beforeDescendants 0 The ViewGroup will get focus before |
| any of its descendants. |
±-----------------------------------------------------------------------------------------+
| blocksDescendants 2 The ViewGroup will block its descendants from |
| receiving focus. |
±-----------------------------------------------------------------------------------------+
您可以在此处查看完整示例。
代码段为:
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
ListView listView = getListView();
Log.d(TAG, "onItemSelected gave us " + view.toString());
Button b = (Button) view.findViewById(R.id.button);
EditText et = (EditText) view.findViewById(R.id.editor);
if (b != null || et != null) {
// Use afterDescendants to keep ListView from getting focus
listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
if(et!=null) et.requestFocus();
else if(b!=null) b.requestFocus();
} else {
if (!listView.isFocused()) {
// Use beforeDescendants so that previous selections don't re-take focus
listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
listView.requestFocus();
}
}
}
根据上面的代码片段, 用于防止获得焦点,以便 or 可以请求焦点。afterDescendants listview EditText Button