0

ブール値のオプションに基づいてビューを作成しています。ビューのタイトルは socialoptions で、別の xml ファイルで定義されています。などのメソッドでビューを使用しようとすると、ビューは常に null を返しますsocialoptions.setClickable(true);

Java コード:

        if (isUserProfile) {
            Log.d("user","user");
            middlelayout = (LinearLayout) findViewById (R.layout.usermiddlelayout);
        } else {
            Log.d("item","item");
            middlelayout = (LinearLayout)  findViewById (R.layout.itemmiddlelayout);
            socialoptions = (TextView) findViewById (R.id.socialoptions);
            map = (TextView) findViewById (R.id.map);
        }

XML コード:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/itemmiddlelayout"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="25"
    android:background="@android:color/transparent"
    android:clickable="false"
    android:orientation="horizontal" >

    <RelativeLayout
        android:id="@+id/sociallayout"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:background="@android:color/transparent"
        android:paddingBottom="0dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp" >

        <TextView
            android:id="@+id/socialoptions"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/transparent"
            android:clickable="false" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/maplayout"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:paddingBottom="10dp"
        android:paddingTop="0dp" >

        <TextView
            android:id="@+id/map"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/transparent"
            android:clickable="false" />
    </RelativeLayout>

</LinearLayout>
4

2 に答える 2

1

コメントによると

やろうとしていることはできません。設定されていない XML レイアウトは参照できません。つまり、最初にそのようなレイアウトsetContentViewを使用または拡張する必要があります。

ドローアブルは、参照可能なオブジェクトとしてアプリケーションに格納されます。APK 内に「ファイル」として存在します。レイアウトは、「作成」するまでは何の意味もない単なるデータです。

コンテンツ ビューを設定するか、レイアウトをビューに展開するとnull、レイアウトのコンテンツを参照するときにオブジェクトを受け取ることはなくなります。

于 2012-09-19T20:07:20.950 に答える
0

xml ページが usermiddlelayout.xml として保存されている場合、R.layout.usermiddlelayout を使用できます。xml ファイル内の線形レイアウトで id が android:id="@+id/itemiddlelayout" に設定されている場合、R.layout.itemiddlelayout ではなく R.id.itemiddlelayout を使用する必要があります。

ここ、

middlelayout = (LinearLayout) findViewById (R.id.itemiddlelayout);

于 2012-09-19T20:06:32.767 に答える