簡體   English   中英

Android findViewById有時可以工作

[英]Android findViewById Working Sometimes

我有一個很奇怪的小故障,我無法理解。 我們將不勝感激,因為它可以在運行Android 2.3的手機,運行3.1的平板電腦上運行,但不能在運行Android 2.1的手機上運行。

我正在嘗試獲取ID為“ overview_linear_layout”的以下XML中定義的LinearLayout。

overview_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <include layout="@layout/listing_view" android:layout_width="fill_parent" android:id="@+id/header_view" android:layout_height="wrap_content"></include>
    <ScrollView android:id="@+id/scrollView1" android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="wrap_content">
        <LinearLayout android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="fill_parent" android:id="@+id/overview_linear_layout">
            <TextView android:scrollbars="vertical" android:longClickable="false" android:layout_weight="1" android:clickable="false" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/main_description"></TextView>
        </LinearLayout>
    </ScrollView>
    <include layout="@layout/listing_view" android:layout_width="fill_parent" android:id="@+id/footer_view" android:layout_height="wrap_content" android:layout_weight="0.0"></include>

</LinearLayout>

我用來獲取視圖的代碼如下:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.overview_view);
    LinearLayout linearLayout = (LinearLayout)findViewById(R.id.overview_linear_layout);

    if(linearLayout!=null) {
        System.out.println("Found linear layout");
    }
    else {
        System.out.println("Did not find linear layout");
    }
}

我已經在Eclipse中嘗試過Project-> Clean,但這也無濟於事。

改變這個

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.overview_linear_layout);  

LinearLayout linearLayout = (LinearLayout)findViewById(R.layout.overview_linear_layout);`

如果在鍵入時eclipse沒有顯示overview_linear_layout,請單擊紅叉,然后單擊R中的創建常量overview_linear_layout。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM