我之前曾问过一个关于这个错误的问题(Android StackOverflowError in ViewGroup.resetResolvedTextDirection),但是现在我设法在模拟器中重现了这个错误,并缩小了发生这个问题的具体位置。
当我开始我的 Activity 时,我有一个 AsyncTask
,它从我的服务器中提取必要的数据并创建所有 View 。在 AsyncTask 的 run()
方法中,我正在创建一个自定义 View (并将其添加到 Activity 的主视图中 - 但现在这并不重要):
ListingView listing = new ListingView(MainActivity.this);
ListingView
是我的自定义 View 类,它扩展自 LinearLayout
。在此自定义 View 的构造函数中,我有以下代码:
public ListingView(Context context)
{
this.context = context;
...
LayoutInflater infl = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View top = infl.inflate(R.layout.listing, this, true);
this.addView(top);
...
LinearLayout lst = (LinearLayout)this.findViewById(R.id.LayoutListingTop);
tblVenues = new ListView(context);
tblVenues.setVisibility(VISIBLE);
tblVenues.setItemsCanFocus(true);
tblVenues.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(venueListener != null && venues.size() > 0) { venueListener.selected(venues.get(position)); }
}
});
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
lst.addView(tblVenues, lp); //this line causes the problem
...
}
在此自定义 View 类中,tblVenues
声明为
private ListView tblVenues;
正在加载的 XML 是这样的:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/LayoutListingTop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white">
<ImageView android:id="@+id/ImageView01"
android:layout_width="170dp"
android:layout_height="62dp"
android:src="@drawable/ookl_logo"
android:layout_gravity="left"
android:adjustViewBounds="false"
android:scaleType="fitXY">
</ImageView>
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginBottom="5px"
android:gravity="center"
android:textColor="@color/black"
android:text="@string/love_culture"
android:id="@+id/TextView01"
android:textSize="28dip">
</TextView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:id="@+id/ButtonBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/buttonbar"
android:paddingLeft="10px"
android:paddingRight="10px"
android:paddingTop="5px"
android:paddingBottom="5px">
<Button android:id="@+id/BtnListVenues"
android:layout_width="wrap_content"
android:layout_height="45dip"
android:background="@null"
android:text="@string/button_venues"
android:drawableTop="@drawable/icon_venues_on"
android:textColor="@color/venue_blue"
android:layout_marginRight="10px"
android:textSize="9dip">
</Button>
<Button android:id="@+id/BtnListEvents"
android:layout_width="wrap_content"
android:layout_height="45dip"
android:background="@null"
android:text="@string/button_events"
android:drawableTop="@drawable/icon_events_off"
android:textColor="@color/white"
android:layout_marginRight="10px"
android:textSize="9dip">
</Button>
<Button android:id="@+id/BtnListTrails"
android:layout_width="wrap_content"
android:layout_height="45dip"
android:background="@null"
android:text="@string/button_trails"
android:drawableTop="@drawable/icon_trails_off"
android:textColor="@color/white"
android:layout_marginRight="10px"
android:textSize="9dip">
</Button>
<Button android:id="@+id/BtnListObjects"
android:layout_width="wrap_content"
android:layout_height="45dip"
android:background="@null"
android:text="@string/button_objects"
android:drawableTop="@drawable/icon_objects_off"
android:textColor="@color/white"
android:layout_marginRight="10px"
android:textSize="9dip">
</Button>
<TextView android:id="@+id/TxtLabelDistance"
android:layout_width="fill_parent"
android:text="@string/label_distance"
android:layout_height="wrap_content"
android:gravity="right|bottom"
android:layout_alignParentBottom="true"
android:textColor="@color/white"
android:layout_marginTop="-17px"
android:textSize="9dip">
</TextView>
</LinearLayout>
</LinearLayout>
这一切在 Android form 1.6 到 3.x 上都运行良好,但是在 Ice Cream Sandwich (4.0) 线上
lst.addView(tblVenues, lp);
StackOverflowError
的结果:
java.lang.StackOverflowError
at android.view.ViewGroup.resetResolvedTextDirection(ViewGroup.java:5131)
at android.view.ViewGroup.resetResolvedTextDirection(ViewGroup.java:5131)
at android.view.ViewGroup.resetResolvedTextDirection(ViewGroup.java:5131)
at android.view.ViewGroup.resetResolvedTextDirection(ViewGroup.java:5131)
at android.view.ViewGroup.resetResolvedTextDirection(ViewGroup.java:5131)
at android.view.ViewGroup.resetResolvedTextDirection(ViewGroup.java:5131)
... this line repeats about 200 times or so ...
LogCat 中没有其他有用的东西。整个 ListingView 甚至还没有添加到主 Activity View 中,因为错误本质上是从其构造函数中抛出的。
我完全不知道可能是什么问题。任何帮助将不胜感激。
最佳答案
[老问题,但我对答案不满意,所以添加我的笔记希望它们对其他人有帮助。]
Aleks -- 我同意核心问题是 Android 错误,但跟踪代码(我最近遇到了类似的问题)我相信可以通过将 attachToRoot 参数设置为 false 来膨胀来防止该问题。这是因为使用 attacheToRoot==true 时,返回的 View 是您传入的“root”参数。在您的情况下是“this”,然后添加当前 View 的 subview (您将对象添加为 subview 本身)。这似乎让 android 感到困惑(它确实应该检查实例是否相等)。
[返回] 膨胀层次结构的 Root View 。如果提供了根并且 attachToRoot 为真,则这是根;否则它是膨胀 XML 文件的根。”
进行此更改解决了我的问题。
关于将ListView添加到LinearLayout时ViewGroup.resetResolvedTextDirection中的Android StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9417494/