我希望在循环内使用一个TextView来扩展动态LinearLayout。这是有信息的页面。我尝试过这段代码:
父页面的xml为:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="em.example.InfoActivity">
<LinearLayout
android:id="@+id/infolayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:layout_marginTop="50dp">
</LinearLayout>
</RelativeLayout>
子页面的xml为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#55934d"
android:padding="5dp">
<TextView
android:id="@+id/TextAddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TEXT ADDRESS:"
android:textSize="10sp"
android:textColor="#fff"
/>
</LinearLayout>
这是主要活动:
public void showInfo(){
LayoutInflater layoutInfralte=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.infolayout);
List views=new ArrayList();
Iterator<latlng>ite=NavigationContext.getInstance().getArray().iterator();
latlng temp;
while(ite.hasNext()){
temp=ite.next();
View view=layoutInfralte.inflate(R.layout.infoitem, null);
TextView tv=(TextView)findViewById(R.id.TextAddress);
tv.setText(temp.address);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
views.add(view);
}
for(int i = 0; i<views.size(); i++)
linearLayout.addView((View) views.get(i));
}
错误:
08-05 17:40:59.941: E/AndroidRuntime(3163): FATAL EXCEPTION: main
08-05 17:40:59.941: E/AndroidRuntime(3163): java.lang.RuntimeException: Unable to start activity ComponentInfo{em.example/em.example.InfoActivity}: java.lang.NullPointerException
08-05 17:40:59.941: E/AndroidRuntime(3163): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-05 17:40:59.941: E/AndroidRuntime(3163): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-05 17:40:59.941: E/AndroidRuntime(3163): at android.app.ActivityThread.access$600(ActivityThread.java:141)
答案 0 :(得分:2)
您在获取textView
的引用时遇到问题public void showInfo(){
LayoutInflater layoutInfralte=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.infolayout);
List views=new ArrayList();
Iterator<latlng>ite=NavigationContext.getInstance().getArray().iterator();
latlng temp;
while(ite.hasNext()){
temp=ite.next();
View view=layoutInfralte.inflate(R.layout.infoitem, null);
// Edit
TextView tv=(TextView)view.findViewById(R.id.TextAddress);
tv.setText(temp.address);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
views.add(view);
}
for(int i = 0; i<views.size(); i++)
linearLayout.addView((View) views.get(i));
}