我正在尝试在布局中夸大多个进度条。请告诉我布局是否有任何问题。我有一个xml代码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" >
和另一个文件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);
我正在
获得nullpointerbatteryHolder = (LinearLayout)batteryHolder.findViewById(R.id.batteryholder)
答案 0 :(得分:1)
您获得NPE是因为您尝试访问尚未初始化的batteryHolder
字段。您需要首先夸大battery.xml
:
LinearLayout oneMoreBatteryLayout = (LinearLayout)inflater.inflate(R.layout.battery, null, false);
然后访问您的batteryholder
孩子:
batteryHolder = (LinearLayout)oneMoreBatteryLayout.findViewById(R.id.batteryholder);
顺便问一下,为什么你需要这么复杂的结构呢?尽量简化它。