有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java更改背景颜色,但始终获取应用程序已停止的错误

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void 安卓.widget.RelativeLayout.setBackgroundColor(int)' on a null object reference.

这就是我运行程序时得到的结果,我不知道该怎么办。 我读了别人的帖子,但仍然不知道问题出在哪里。我将“public void onClick(视图v)”中的“v”替换为“findviewbyd”,但仍然不起作用

package com.example.gia.applumini;

import 安卓.graphics.Color;
import 安卓.graphics.drawable.ColorDrawable;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.RelativeLayout;



public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout bgElement = (RelativeLayout) findViewById(R.layout.activity_main);
        bgElement.setBackgroundColor(Color.RED);
        myButtonListenerMethod();
    }

    public void myButtonListenerMethod() {
        Button button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RelativeLayout bgElement = (RelativeLayout) findViewById(R.layout.activity_main);
                int color = ((ColorDrawable) bgElement.getBackground()).getColor();
                if (color == Color.RED) {
                    bgElement.setBackgroundColor(Color.BLUE);
                }
                else {
                    bgElement.setBackgroundColor(Color.RED);
                }
            }
        });
    }
}

共 (3) 个答案

  1. # 1 楼答案

    替换
    RelativeLayout bgElement=(RelativeLayout)findViewById(R.layout.activity_main); RelativeLayout bgElement=(RelativeLayout)findViewById(R.id.activity_main)

    并确保相对布局的id为activity_main

  2. # 2 楼答案

    另一种更简单的处理方法是使用布局的XML代码。只需将以下属性添加到标记中:

    android:background="#ff0000"
    

    请注意,上面的颜色是红色,您可以随意修改RGB值

  3. # 3 楼答案

    尝试将代码调整为如下所示:

    public class MainActivity extends AppCompatActivity {
    
    RelativeLayout bgElement;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            bgElement = (RelativeLayout) findViewById(R.id.RELATIVELAYOUTID);
            bgElement.setBackgroundColor(Color.RED);
            myButtonListenerMethod();
        }
    
        public void myButtonListenerMethod() {
            Button button = (Button) findViewById(R.id.button);
    
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int color = ((ColorDrawable) bgElement.getBackground()).getColor();
                    if (color == Color.RED) {
                        bgElement.setBackgroundColor(Color.BLUE);
                    }
                    else {
                        bgElement.setBackgroundColor(Color.RED);
                    }
                }
            });
        }
    }