1

view.xmlと呼ばれるを含むとView呼ばれるレイアウトがありますview_related。基本的に、私の辞書アプリでは、エントリに関連する単語がある場合、ヘッダー「関連するエントリ」とすべての関連する単語を表す一連のsview_relatedを含むに置き換えます。LinearLayoutTextView

のメソッドににをNullPointerException追加するたびに取得し続けます。コードは非常に単純に見えますが、その理由がわかりません。TextViewLinearLayoutActivityonCreate()

view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView android:id="@+id/view_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12pt"
        android:textColor="#fff"
        android:textStyle="bold"
        android:background="#990011"
        android:padding="10dp"
        android:text="lalala word"
        />

    <ScrollView android:id="@+id/view_description"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/view_header">
        <LinearLayout android:id="@+id/view_description_content"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="20dp"
                android:text="@string/demo_definition"
                />
                <!-- THIS WILL BE REPLACED -->
            <View android:id="@+id/view_related"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"></View>
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

view_related.xml、view.xmlの要素をLinearLayout置き換えますView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_related_entries"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView android:id="@+id/view_related_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textColor="#fff"
        android:textStyle="bold"
        android:textSize="7pt"
        android:background="#000"
        android:text="Related entries"
        />
    <LinearLayout android:id="@+id/view_related_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    </LinearLayout>
</LinearLayout>

そして最後に、onCreate今のところ関連するエントリが「Hello」と「Goodbye:」であると想定しているメソッド。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view);

    LinearLayout ll = (LinearLayout) findViewById(R.id.view_related_list);
    TextView tv = new TextView(this);
    tv.setText("Hello");
    ll.addView(tv); // NULL POINTER EXCEPTION

    tv = new TextView(this);
    tv.setText("Goodbye");
    ll.addView(tv);

    LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View view = findViewById(R.id.view_related);
    ViewGroup parent = (ViewGroup) view.getParent();
    int index = parent.indexOfChild(view);
    parent.removeView(view);
    View llview = li.inflate(R.id.view_related_entries, parent, false);
    parent.addView(llview, index);
}
4

5 に答える 5

2
setContentView(R.layout.view);
LinearLayout ll = (LinearLayout) findViewById(R.id.view_related_list);

この関数findViewByIdは、実際に設定されているビューのみを検索します。あなたの現在setContentViewはあなたが探しているIDを持つxmlを設定していないので、私が引用した最初の行は何ももたらさないでしょう。

正しいXMLをロードするか、探しているビューをインフレータで膨らませる必要があります

于 2012-10-25T09:38:14.563 に答える
1

行( )は、その前の行が使用しているビューであるように、をLinearLayout ll = (LinearLayout) findViewById(R.id.view_related_list);検索します。view_related_listview.xml

view_related.xml最初に膨らませてから、そこからビューを取得する必要があります。

于 2012-10-25T09:38:12.433 に答える
1

あなたはあなたのコードで多くの間違ったことをしています。LinearLayout最初に、現在のレイアウトに含まれておらず、その時点で拡張レイアウトにも含まれていないを検索します。id次に、参照ではなく参照を膨らませようとしlayoutます。最後に、あなたがやろうとしていることに別のアプローチを使うべきです。あなたのコードは(私が理解したことから)あるべきです:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view);   

    LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View view = findViewById(R.id.view_related);
    ViewGroup parent = (ViewGroup) view.getParent();
    int index = parent.indexOfChild(view);
    parent.removeView(view);
    View llview = li.inflate(R.layout.view_related, parent, false); // is view_related the name of the extra layout file?
    parent.addView(llview, index);
    LinearLayout ll = (LinearLayout) findViewById(R.id.view_related_list); // or search for it in llview, llview.findViewById(R.id.view_related_list);
    TextView tv = new TextView(this);
    tv.setText("Hello");
    ll.addView(tv); // NULL POINTER EXCEPTION

    tv = new TextView(this);
    tv.setText("Goodbye");
    ll.addView(tv);
}

ViewStubこれは、を使用して新しいレイアウトファイルを追加する必要がある状況です。view.xmlただし、メソッドにレイアウトのコンテンツを追加する場合は、新しいレイアウトファイルを直接追加しない理由がわかりませんonCreate

于 2012-10-25T09:51:19.027 に答える
0

setContentView(R.layout.view_related);代わりに使う必要があると思いますsetContentView(R.layout.view);

または、同じレイアウトの「ビュー」で線形レイアウトを取得し、不要な場合は非表示にし、必要な場合は表示するという簡単な方法があります。

于 2012-10-25T09:37:35.467 に答える
-1

マニフェスト.xmlでアクティビティを宣言するのを忘れている可能性があります

于 2012-10-25T09:39:59.453 に答える