Android 在单个onclick上调用两个对象的方法

Android 在单个onclick上调用两个对象的方法,android,Android,惊人事实的主要活动 package com.example.user.amazingfacts; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout;

惊人事实的主要活动

package com.example.user.amazingfacts;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private TextView showtext;
private Button showbutton;
private RelativeLayout relativeLayout;
private Amazingnote Amazingnote=new Amazingnote();
private Colorwheel colorwheel=new Colorwheel();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showtext=(TextView)findViewById(R.id.showtext);
        showbutton=(Button)findViewById(R.id.showbutton);
        showbutton.setOnClickListener(new  View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
String fact= Amazingnote.getFact();
showtext.setText(fact);

int color=colorwheel.getColor();

  relativeLayout.setBackgroundColor(color);
showbutton.setTextColor(color);


            }
        });
    }
}
第二个惊人事实活动-惊人音符

package com.example.user.amazingfacts;

import java.util.Random;

/**
 * Created by User on 09-10-2017.
 */

public class Amazingnote {
    public String getFact() {
        String[] facts = {"In 1889, the Queen of Italy, Margherita Savoy, ordered the first pizza delivery",
                " You can buy eel flavored ice cream in Japan",
                "Although the bobcat is rarely seen, it is the most common wildcat in North America",
                " A cat's tail contains nearly 10 percent of all the bones in its body",
                "The term astronaut comes from Greek words that mean star and sailor",
                "The calcium in our bones and the iron in our blood come from ancient explosions of giant stars",
                "The Nile crocodile can hold its breath underwater for up to 2 hours while waiting for prey",
                "Jellyfish, or jellies as scientists call them, are not fish. They have no brain, no heart, and no bones",
                " Some people used to believe that kissing a donkey could relieve a toothache.",
                " Because the speed of Earth's rotation changes over time, a day in the age of dinosaurs was just 23 hours long",
                "Hummingbirds' wings can beat 200 times a second.",
                "There are more than 1,200 water parks in North America.",
                "A seahorse can move its eyes in opposite directions—all the better to scan the water for food and predators.",
                " To cook an egg, a sidewalk needs to be 158°F."

        };

        Random randomGenerator=new Random();
        int randomnumber=randomGenerator.nextInt(facts.length);
        return facts[randomnumber];

    }
}
神奇事实-彩色车轮的第三项活动

package com.example.user.amazingfacts;

import android.graphics.Color;

import java.util.Random;

/**
 * Created by User on 10-10-2017.
 */

public class Colorwheel
{
  //Fields or Member Variables -Properties abou the object
    private String[] colors = {
          "#39add1",//light blue
          "#3079ab",//dark blue
          "#c25975",//mauve
          "#e15258",//red


    };
    //Methods-Actions the object can take
    int getColor()
    {

    Random randomGenerator=new Random();
    int randomnumber=randomGenerator.nextInt(colors.length);
     int color= Color.parseColor(colors[randomnumber]) ;
    return color ;

}
}
这是一个名为Azing facts的小应用程序。它使用两个
文本视图
和一个按钮。 第一个
文本视图
没有“你知道吗?”那么重要。这是第一个
文本视图
。 当用户点击按钮时,第二个
文本视图的内容就会改变,应用程序的背景颜色也会改变。这是我的想法。

我使用build函数成功构建了应用程序,但我尝试在android手机上安装该应用程序。当我单击按钮时,该应用程序正在停止。我了解到单击时的
属性中存在一些问题。我完全删除了第二类“色轮”从单击的
*
上的
*
中,应用程序正在工作。为什么应用程序没有在单击的
中加载两个对象的方法?为什么我不能同时更改文本视图和背景颜色?

将主要活动的第一部分更改为

public class MainActivity extends AppCompatActivity {
private TextView showtext;
private Button showbutton;
private RelativeLayout relativeLayout;
private Amazingnote Amazingnote=new Amazingnote();
private Colorwheel colorwheel=new Colorwheel();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    relativeLayout = (RelativeLayout) findViewById(R.layout.main_activity); 
        .
        .
        .

布局未初始化,因此您正在尝试更改尚不存在的对象的颜色

您的
relativeLayout
未初始化此时您无法进行初始化,因为您的活动未设置内容视图Yet此更新应能正常工作。谢谢@0xDEADC0DE。“完全错过了那个。”哈里克里希纳贾亚钱德兰没问题,很乐意帮忙。