以编程方式设置ScrollView背景颜色

时间:2017-05-05 06:57:32

标签: android android-scrollview

我对Android很新。 在使用下面的代码为ScrollView设置背景颜色时,我得到了" java.lang.NullPointerException:尝试调用虚方法' void android.view.View.setBackgroundColor(int)'在null对象引用"

        ScrollView scroll = (ScrollView) mRootView.findViewById(R.id.content_scroll);
    scroll.setBackgroundColor(color);

然而,当我在TextView中使用它时,它工作正常。

    int color = ContextCompat.getColor(mContext,mSetColor);

    TextView textContent = (TextView) mRootView.findViewById(R.id.content);
    textContent.setText(mCurrentContent.getmTextContent());
    textContent.setBackgroundColor(color);
    textContent.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);

以下是完整的代码,似乎我在基本的东西上缺失了。

package com.example.android.tourguide;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;


public class ContentAdapter {

private Content mCurrentContent;
public View mRootView;
private int mSetColor;
private Context mContext;

public ContentAdapter(Content content, View rootView, int setColor,Context context) {
    mRootView = rootView;
    mCurrentContent = content;
    mSetColor = setColor;
    mContext= context;

}

public View setContent() {


    int color = ContextCompat.getColor(mContext,mSetColor);

    ImageView imageView = (ImageView) mRootView.findViewById(R.id.image);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setImageResource(mCurrentContent.getmImageResourceID());

    TextView imageText = (TextView) mRootView.findViewById(R.id.image_description);
    imageText.setText(mCurrentContent.getmImageDescriptionText());

    TextView textContent = (TextView) mRootView.findViewById(R.id.content);
    textContent.setText(mCurrentContent.getmTextContent());
    textContent.setBackgroundColor(color);
    textContent.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);

    ScrollView scrollView = (ScrollView) mRootView.findViewById(R.id.content_scroll);
    scrollView.setBackgroundColor(color);

    return mRootView;

    }

}

完整的XML代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/contentView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primary_light"
android:orientation="vertical">

<ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="4"
    android:adjustViewBounds="true" />

<TextView
    android:id="@+id/image_description"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:text="abc"
    android:textAlignment="center"
    android:textAppearance="@android:style/TextAppearance.Material.Display1"
    tools:targetApi="lollipop" />

<View
    android:layout_width="fill_parent"
    android:layout_height="2dp"
    android:background="#c0c0c0" />


<ScrollView
    android:id="@+id/content_scroll"
    android:layout_width="match_parent"
    android:layout_weight="8"
    android:layout_height="0dp"
    android:background="@color/colorAccent"
    >

        <TextView
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="fill"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:text="abc"
            android:textAppearance="@android:style/TextAppearance.Material.Small"
            tools:targetApi="lollipop"
            />

    </ScrollView>


</LinearLayout>

输出 Need to fill the white space below green background

2 个答案:

答案 0 :(得分:1)

如果我使用ScrollView时你就是我,我的布局xml将是这样的:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="8"
    android:id="@+id/content_scroll"
    >
            <LinearLayout
                android:id="@+id/layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">


    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="fill"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
android:textAppearance="@android:style/TextAppearance.Material.Small"
        tools:targetApi="lollipop"
        android:text="abc"
        />
 </LinearLayout>
</ScrollView>

ScrollView必须具有子布局。然后我将根据您的情况设置LinearLayout的颜色:

layout.setBackgroundColor(your color);

答案 1 :(得分:0)

public ContentAdapter(Content content, View rootView, int setColor,Context context) {mSetColor = setColor; }

一旦你将Activity从Activity发送到 ContentAdapter ,那么构造函数集的颜色变为null,因此你得到nullpointerException

相关问题