2015-06-15 183 views
1

我在RelativeLayout中添加了一個進度條。見下面findviewbyid進度條返回null

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:id="@+id/top_control_bar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#FF6600"> 

     <TextView 
      android:id="@+id/DetailHeading" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_centerInParent="true" 
      android:layout_margin="5dip" 
      android:text="Product Details" 
      android:textColor="#FFFFFF" 
      android:textStyle="bold" /> 

    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/bottom_control_bar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:background="#FFFFFF"> 

     <Button 
      android:id="@+id/btnAddToCart" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_margin="5dp" 
      android:background="@drawable/mybutton" 
      android:minHeight="2dp" 
      android:text="Add to Cart" 
      android:textColor="#FFFFFF" 
      android:textSize="12sp" 
      android:textStyle="bold" /> 

     <Button 
      android:id="@+id/btnWishlist" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_margin="5dp" 
      android:background="@drawable/mybutton" 
      android:minHeight="2dp" 
      android:text="Add to Wishlist" 
      android:textColor="#FFFFFF" 
      android:textSize="12sp" 
      android:textStyle="bold" /> 

     </RelativeLayout> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@id/bottom_control_bar" 
     android:layout_below="@id/top_control_bar" 
     android:orientation="vertical"> 

     <ImageView 
      android:id="@+id/detImage" 
      android:layout_width="fill_parent" 
      android:layout_height="300dp" 
      android:adjustViewBounds="true" /> 

     <TableLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <TableRow 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="left"> 

       <TextView 
        android:id="@+id/codelbl" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="1dp" 
        android:gravity="left" 
        android:text="Product Code:" 
        android:textColor="#000000" 
        android:textStyle="bold" /> 

       <TextView 
        android:id="@+id/detPCode" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="1dp" 
        android:gravity="left" 
        android:textColor="#000000" /> 

      </TableRow> 

      <TableRow 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="left"> 

       <TextView 
        android:id="@+id/pricelbl" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="1dp" 
        android:gravity="left" 
        android:text="Price:" 
        android:textColor="#000000" 
        android:textStyle="bold" /> 

       <TextView 
        android:id="@+id/detPPrice" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="1dp" 
        android:gravity="left" 
        android:textColor="#000000" /> 
      </TableRow> 

      <TableRow 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="left"> 

       <TextView 
        android:id="@+id/namelbl" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="1dp" 
        android:gravity="left" 
        android:text="Name:" 
        android:textColor="#000000" 
        android:textStyle="bold" /> 

       <TextView 
        android:id="@+id/detPName" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="1dp" 
        android:gravity="left" 
        android:textColor="#000000" /> 
      </TableRow> 

      <TableRow 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="left"> 

       <TextView 
        android:id="@+id/desclbl" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="1dp" 
        android:gravity="left" 
        android:text="Description:" 
        android:textColor="#000000" 
        android:textStyle="bold" /> 


       <TextView 
        android:id="@+id/detPDesc" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_centerVertical="true" 
        android:ellipsize="none" 
        android:lines="5" 
        android:maxLines="2" 
        android:scrollHorizontally="false" 
        android:singleLine="false" 
        android:textColor="#000000" /> 
      </TableRow> 
     </TableLayout> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/detProgress" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:visibility="gone" > 

     <ProgressBar 
      android:id="@+id/detHeaderProgress" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 
     </ProgressBar> 
    </LinearLayout> 

</RelativeLayout> 

的XML現在,當我試圖找回在活動的onCreate的進度,它返回一個空。

LinearLayout detHdrPgs = (LinearLayout) findViewById(R.id.detProgress); 

detHdrPgs是空

我也做了活動已經

setContentView(R.layout.activity_detail); 

的的setContentView而且還能夠檢索其他元素,如btnAddToCart

Button addCart = (Button)findViewById(R.id.btnAddToCart); 

然後爲什麼只有ProgressBar返回null?

的的onCreate函數代碼如下

super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_detail); 
    ActionBar actionBar = getSupportActionBar(); 
    actionBar.setHomeAsUpIndicator(R.drawable.ic_launcher); 
    actionBar.setDisplayHomeAsUpEnabled(true); 
    Button addCart = (Button)findViewById(R.id.btnAddToCart); 
    addCart.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     ..... 
     } 
    }); 
    LinearLayout detHdrPgs = (LinearLayout) findViewById(R.id.detHeaderProgress); detHdrPgs.setVisibility(View.VISIBLE); 

預先感謝所有幫助。

回答

0

ProgressBar detHdrPgs = (ProgressBar) findViewById(R.id.detHeaderProgress);

在你的onCreate您有:

LinearLayout detHdrPgs = (LinearLayout) findViewById(R.id.detHeaderProgress); 

你必須將它更改爲:

LinearLayout detHdrPgs = (LinearLayout) findViewById(R.id.detProgress); 
+0

對不起,這是一個錯誤,事實上我的代碼是LinearLayout detHdrPgs =(LinearLayout)findViewById(R.id.detProgress);這是我試過的東西,但沒有工作 –

+0

不,這個幫助。它仍然返回null。另外detHeaderProgress是ProgressBar。我正在嘗試獲取LinearLayout detProgress。 –

+0

你已經在你的問題中說過:現在,當我嘗試在活動的onCreate中檢索ProgressBar時,它返回null。那麼爲什麼只有ProgressBar返回null? – albertoqa

0
view.findViewById(R.id.detProgress) 

是什麼view申報?

將其更改爲

LinearLayout detHdrPgs = (LinearLayout) findViewById(R.id.detProgress); 

這篇幫助?

+0

對不起,這是一個錯誤[我糾正了這個帖子]。我實際上使用了LinearLayout detHdrPgs =(LinearLayout)findViewById(R.id.detProgress);但它返回null。 –

+0

您應該將「findViewById(R.id.detHeaderProgress)」更改爲「findViewById(R.id.detProgress)」。 – Sasi