3

以前にこのエラーについて1つの質問をしましたが(ViewGroup.resetResolvedTextDirectionのAndroid StackOverflowError)、エミュレータでエラーを再現し、この問題が発生する特定の場所を絞り込みました。

アクティビティを開始するAsyncTaskと、サーバーから必要なデータを取得してすべてのビューを作成するがあります。run()AsyncTaskのメソッド内で、カスタムビューを作成しています(そしてそれをアクティビティのメインビューに追加していますが、今は重要ではありません)。

ListingView listing = new ListingView(MainActivity.this);

ListingViewから拡張する私のカスタムビュークラスLinearLayoutです。このカスタムビューのコンストラクターには、次のコードがあります。

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

    ...
}

このカスタムビュークラスで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フォーム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全体はまだメインアクティビティビューに追加されていません。

何が問題なのか、私は全損です。どんな助けでも大歓迎です。

4

3 に答える 3

5

[古い質問ですが、答えに満足できなかったので、他の人の役に立つことを願ってメモを追加しました。]

Aleks-コアの問題がAndroidのバグであることに同意しますが、コードをトレースすると(最近同様の問題が発生しました)、attachToRootパラメーターをfalseに設定して膨らませることで問題を防ぐことができると思います。これは、attacheToRoot == trueの場合、返されるビューは渡した'root'パラメーターであるためです。この場合は'this'であり、現在のビューの子が追加されます(オブジェクトを子として追加します)。それ自体の)。これはAndroidを混乱させるようです(実際にはインスタンスの同等性をチェックする必要があります)。

http://developer.android.com/reference/android/view/LayoutInflater.html#inflate(org.xmlpull.v1.XmlPullParser、android.view.ViewGroup、boolean)

「[戻り値]拡張された階層のルートビュー。rootが指定され、attachToRootがtrueの場合、これはrootです。それ以外の場合は、拡張されたXMLファイルのルートです。」

この変更を行うと、私の場合は問題が解決しました。

于 2013-05-27T04:21:21.760 に答える
3

更新:これは実際には悪いアドバイスであり、私は何年にもわたってAPIを使用して学んだ。上位投票の回答を参照してください。取得が難しい場合でも、親ビューを追加する必要があります。これはlayout_、拡張されたXMLのルート要素のパラメーターが親のレイアウトに依存しているためだと思います。たとえばlayout_below(またはこのようなもの)は、要素がの子である場合に使用できますRelativeLayout。少なくとも、私が合格nullしたとき、私は関連する問題を抱えていました。

同じ間違いをしたので、Android自体をデバッグしていましたが、本当の理由を見つけました。本当の問題はこれらの行の中にあります:

View top = infl.inflate(R.layout.listing, this, true);
this.addView(top);

ご覧のとおりthis、inflateの2番目のパラメーターとして渡すと、すでにinflatedビューがに追加されているthisため、追加するthis.addView(top)と、もう一度追加されます。このような間違いを修正するには、2つのオプションがあります。行this.addView(top)を削除するか、に置き換えthisますnull。私は2番目のものを試していませんが、それもうまくいくはずであり、インフレーション後に親に追加する前にビューを操作したい場合は、おそらくより良い解決策です。

attachToRootBigDSKが言及したパラメーターで十分かもしれませんが、を渡しnullて、その後にaddViewingを実行するか、を省略するaddViewと、より防弾のソリューション(IMHO)になるので、共有したいと思いました。

于 2014-06-08T16:20:16.037 に答える
2

かなり長い間壁に頭をぶつけて、さまざまなアプローチを試した後、これは本当にAndroidのバグであり、ビューvisibilityが明示的に設定されてVISIBLEいるが、ビュー自体とビューの親がメインビューには追加されません。

私はついにListViewをXMLに追加し、ビューsetVisibility(View.VISIBLE)全体がメインビューに追加された後にコードを移動することでそれを回避しました(つまり、親階層は各子から最後まで追跡できます)。

少なくとも、このエラーはもう発生していません。

于 2012-02-24T15:12:32.373 に答える