2012-01-19 21 views
0

我想在佈局中爲多個進度條充氣。請告訴我佈局是否有任何問題。我有一個XML代碼battery.xml:使用自定義layoutinflater?

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="horizontal" android:background="#FFFFFF"> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 

     android:orientation="vertical" 
     android:id="@+id/batteryholder"> 

     <TextView android:text="Test ID: 137" android:gravity="left" 
      android:id="@+id/test_id" android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:textSize="14dip" 
      android:paddingLeft="30dip" android:paddingTop="10dip" 
      android:textColor="#000000" /> 

     <HorizontalScrollView 
      android:id="@+id/scroll_battery" 
      android:layout_width="420dip" 
      android:layout_height="200dip" 
      android:layout_marginTop="0dip" > 

和另一個文件batterylayout.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/empty_battery" /> 

    <ProgressBar 
     android:id="@+id/vertical_progressbar" 
     style="@style/Widget.ProgressBar.Vertical" 
     android:layout_width="88dp" 
     android:layout_height="150dp" 
     android:layout_x="2dp" 
     android:layout_y="23dp" /> 

</AbsoluteLayout> 


<AbsoluteLayout 
    android:id="@+id/scrolllinearlayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

     </AbsoluteLayout> 

     </HorizontalScrollView> 
</LinearLayout> 

我怎麼能在膨脹幫助battery.xml.Plz不止一個batterylayout。

我的Java代碼:

setContentView(R.layout.battery); 
batteryHolder = (LinearLayout)batteryHolder.findViewById(R.id.batteryholder); 
((TextView) findViewById(R.id.test_id)).setText(Html.fromHtml("<b>Test ID:</b>"+Student_record.getString(3))); 
scrollview_battery = (HorizontalScrollView)scrollview_battery.findViewById(R.id.scroll_battery); 
scrolllayout_battery = (AbsoluteLayout)scrolllayout_battery.findViewById(R.id.scrolllinearlayout); 

LayoutInflater inflater = getLayoutInflater(); 
LinearLayout battery = (LinearLayout)inflater.inflate(R.layout.batterylayout, scrolllayout_battery, false); 
ProgressBar bar= (ProgressBar)findViewById(R.id.vertical_progressbar); 
bar.setMax(100); 
bar.setProgress(percentage[i]); 
scrolllayout_battery.addView(battery); 

我得到空指針在

batteryHolder = (LinearLayout)batteryHolder.findViewById(R.id.batteryholder) 
+0

你如何誇大它們? 「佈局中的多個進度欄」是什麼意思? –

+0

我已編輯它,請看現在! – Navdroid

回答

1

你得到NPE因爲您嘗試訪問batteryHolder領域還未被初始化。您需要虛增您battery.xml第一:

LinearLayout oneMoreBatteryLayout = (LinearLayout)inflater.inflate(R.layout.battery, null, false); 

以及訪問您的batteryholder孩子後:

batteryHolder = (LinearLayout)oneMoreBatteryLayout.findViewById(R.id.batteryholder); 

順便說一句,你爲什麼需要這樣一個複雜的結構?嘗試簡化它。

+0

我已經初始化它,只是沒有顯示在這裏! – Navdroid

+0

不,你沒有:NPE在這個特定的代碼行沒有更多的理由。 –

+0

如何簡化它! – Navdroid