第二个 Activity 为空

Second Activity is blank

第二个activity有一个ListView,但没有显示。

UserList中的Log.i表示activity已激活

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 但出现空白页。

UserList.java 中的 Log.i 表明调用了 activity。尝试设置 visibility

为什么我看不到 ListView

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

请像这样编辑 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>