android - 如何在循环内动态布局充气机

标签 android loops android-linearlayout

我想在循环内使用一个 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>

这是主要 Activity :

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)

最佳答案

您在获取对 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));
  }

关于android - 如何在循环内动态布局充气机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25145372/

相关文章:

android - 如何停止父 Activity 的 Activity ?

android - 使用 usageStats.getTotalTimeInForeground() 获取设备中每个应用程序在前台花费的时间

windows - 在 Windows 批处理中计算 Var

java - 添加新元素时,ArrayList 的所有元素都会更改吗?

java - 奇数/偶数程序不打印任何内容

java - 使用自定义类对象填充线性布局

android - 如何在 android 中动态添加和删除 View ?即使我关闭了应用程序,这也是商店

android - 检查游标是否有结果

android - 将 View 添加到底部的垂直 LinearLayout(以编程方式)

android - 华为登录后黑屏,由InputRealNameActivitys引起