2015-06-15 185 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