Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:ListView存在TextView标题问题_Java_Android_Layout - Fatal编程技术网

Java Android:ListView存在TextView标题问题

Java Android:ListView存在TextView标题问题,java,android,layout,Java,Android,Layout,我的主要活动是: public class TestActivity extends Activity{ List<String> users; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.usersview); sh

我的主要活动是:

public class TestActivity  extends Activity{
    List<String> users;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.usersview);
        showList();

    }
    private void showList() {
        users=new ArrayList<String>();
        users.add("User1");
        users.add("User2");
        users.add("User3");

        ListView lv=(ListView)findViewById(R.id.listv);
//      TextView tv=(TextView)findViewById(R.id.welcomeMsg);
//      lv.addHeaderView(tv);
        lv.setAdapter( new ListArrAdapter(this, R.layout.userlist, users));
    }
在TestActivity中,我得到以下异常

09-16 19:45:10.680: ERROR/AndroidRuntime(31044): Caused by: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
一致

lv.setAdapter( new ListArrAdapter(this, R.layout.userlist, users));
我想显示顶部有消息头的用户列表。但我有这些问题。问题在哪里,解决方案是什么。

请参阅此


它使用带有图标Textview的ListAdapter。您可以理解无法获得预期输出的原因。

在用户视图布局中-

 <TextView
    android:text="@string/header"
    android:id="@+id/welcomeMsg"
    android:textSize="30dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></TextView>

这对我有用。希望能有帮助

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

    <TextView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="top"
    />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
    >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:layout_marginTop="50dp"
    />
    </LinearLayout>
</FrameLayout>


异常看起来像是由于“匹配父项”。尝试使用“包装内容”。另外,在第xml行中使用“wrap\u content”代替“fill\u parent”。还是没有解决办法。我试着改变布局。但不是解决方案。标题文本视图仍然显示“@+id/welcomeMsg”,即使我这样做了:TextView tv=new TextView(此);tv.setText(“标题文本”);
09-16 19:45:10.680: ERROR/AndroidRuntime(31044): Caused by: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
lv.setAdapter( new ListArrAdapter(this, R.layout.userlist, users));
 <TextView
    android:text="@string/header"
    android:id="@+id/welcomeMsg"
    android:textSize="30dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></TextView>
TextView tv = new TextView(this);
tv.setText("Header Text");
listView.addHeaderView(tv);
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

    <TextView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="top"
    />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
    >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:layout_marginTop="50dp"
    />
    </LinearLayout>
</FrameLayout>