第二活动为空

时间:2019-01-26 07:13:02

标签: android android-intent android-listview android-wrap-content

第二个活动有一个ListView,并且不显示它。

Log.i中的UserList表明该活动处于活动状态。

MainActivity.java:

public void makeUserListIntent() {
    Intent userListIntent = new 
      Intent(getApplicationContext(),UserList.class);
    startActivity(userListIntent);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if ( ParseUser.getCurrentUser() != null ) makeUserListIntent();
}

UserList.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_list);

    Log.i("Second Activity: ", "onCreate");
    ListView listViewUserList = findViewById(R.id.listViewUserList);
    ArrayList<String> arrayList = new ArrayList<String>();
    arrayList.add("Test User");
    arrayList.add("Test User");
    ArrayAdapter arrayAdapter = new 
       ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList);
    listViewUserList.setAdapter(arrayAdapter);
    listViewUserList.setVisibility(View.VISIBLE);
}

activiy_user_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".UserList">

<ListView
    android:id="@+id/listViewUserList"
    android:layout_width="395dp"
    android:layout_height="715dp"
    android:visibility="visible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

我应该看到ListView,但出现空白页。

Log.i中的UserList.java表明活动被调用。尝试设置visibility等。

为什么我看不到ListView

2 个答案:

答案 0 :(得分:0)

这里的问题是您将高度设置为恒定值715dp,因此前几项在较小的设备上不可见。要解决此问题,请将您的高度更改为match_parent,并在顶部添加一些边距(如果这是您希望的输出)

答案 1 :(得分:0)

请像这样编辑activiy_user_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".UserList">

<ListView
    android:id="@+id/listViewUserList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="visible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>