【问题标题】:costum layout list view app crash自定义布局列表视图应用程序崩溃
【发布时间】:2014-06-04 19:50:23
【问题描述】:

我想更改列表视图上的文本颜色,我发现我应该在 xml 中使用和 costumlayout 来完成。但是当我尝试它时,它使我的应用程序崩溃了。

这是我的活动:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical"
tools:context="com.example.wowquiz.Scoreboard"
tools:ignore="MergeRootFrame" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/abc_action_bar_title_text_size"
    android:layout_marginTop="@dimen/abc_action_bar_title_text_size"
    android:background="@drawable/vraagbackground"
    android:gravity="center"
    android:text="@string/highscore"
    android:textColor="@color/white"
    android:textSize="@dimen/sp" />

<RelativeLayout
    android:id="@+id/list"
    android:layout_width="wrap_content"
    android:layout_height="244dp" >

    <ListView
        android:id="@+id/listViewScores"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="@dimen/abc_action_bar_title_text_size"
        android:background="@drawable/vraagbackground"
        android:dividerHeight="1dp"
        android:gravity="center"
        android:textColor="@color/white" >

    </ListView>
</RelativeLayout>

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/abc_action_bar_title_text_size"
    android:background="@drawable/restartbutton"
    android:onClick="Back" />

这是我的.java

    public void readScore() { 
    final List<String> scores = db.getScoreNames();
    db.getMin();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.Costumlayout, android.R.id.text1, scores);
    final ListView listViewScores = (ListView) findViewById(R.id.listViewScores);
    listViewScores.setAdapter(adapter);
}

这是我的 costumlayout.xml:

    <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/white"
     />

我只希望颜色为白色以使其更具可读性。

提前致谢

【问题讨论】:

  • 你看过logcat的输出了吗?

标签: android view android-activity


【解决方案1】:

您为布局提供的android:id 不正确。

当您在此处创建 ArrayAdapter 时:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        R.layout.Costumlayout, android.R.id.text1, scores);

您是在告诉适配器查找 ID 为 android.R.id.text1

TextView

但是,在您的 XML 布局中,您为 TextView 指定了 ID R.id.listTextView,这将不匹配。

如果您将 XML 更改为以下内容,它应该可以工作。

<?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
 />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多