【问题标题】:Second Activity is blank第二个活动是空白的
【发布时间】:2019-06-19 22:42:04
【问题描述】:

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

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

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 表明该活动被调用。尝试设置visibility 等。

为什么我看不到ListView

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 非常感谢。你的建议是正确的。 ListView 正在显示!
    • 好的。所以如果我的回答是正确的。检查我的答案正确答案。谢谢
    【解决方案2】:

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

    【讨论】:

    • 这是一个很好的答案!
    • 如果您觉得有用,请考虑勾选绿色勾号以帮助未来的求职者
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    相关资源
    最近更新 更多