Navdroid
Navdroid

Reputation: 4533

using custom layoutinflater?

I am trying to inflate more than one progress bar in a layout.Please tell me if there is any thing wrong with layouts. I have a xml code battery.xml:

<?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" >

and another file 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>

how could i inflate more than one batterylayout in battery.xml.Plz help.

My java code is:

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);

i am getting nullpointer at

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

Upvotes: 0

Views: 633

Answers (1)

a.ch.
a.ch.

Reputation: 8390

You get NPE because you try to access batteryHolder field which hasn't yet been initialized. You need to inflate your battery.xml first:

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

And after that access your batteryholder child:

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

By the way, why do you need such a complicated structure? Try to simplify it.

Upvotes: 1

Related Questions