Java 按钮不存在';不要在android studio中单击

Java 按钮不存在';不要在android studio中单击,java,android,button,Java,Android,Button,} 大家好!我需要帮助!我的按钮不只是简单的clicl!!!!!嘿,帮帮忙!我只是想做一个简单的按钮,每次点击都会改变textview2!起初它可以工作,但现在开始不工作。是否将所选事实设置为文本视图?加 private Button mfactbutton; private TextView mfacttext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedIn

}


大家好!我需要帮助!我的按钮不只是简单的clicl!!!!!嘿,帮帮忙!我只是想做一个简单的按钮,每次点击都会改变textview2!起初它可以工作,但现在开始不工作。

是否将所选事实设置为文本视图?加

private Button mfactbutton;
private TextView mfacttext;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_fun_fact);

    Button mfactbutton = (Button) findViewById(R.id.button);
    TextView mfacttext = (TextView) findViewById(R.id.textView2);

    // now we need to make out button to click
    View.OnClickListener Listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String[] facts = {
                    "Ants stretch when they wake up in the morning.",
                    "Ostriches can run faster than horses.",
                    "Olympic gold medals are actually made mostly of silver.",
                    "You are born with 300 bones; by the time you are an adult you will have 206.",
                    "It takes about 8 minutes for light from the Sun to reach Earth.",
                    "Some bamboo plants can grow almost a meter in just one day.",
                    "The state of Florida is bigger than England.",
                    "Some penguins can leap 2-3 meters out of the water.",
                    "On average, it takes 66 days to form a new habit.",
                    "Mammoths still walked the earth when the Great Pyramid was being built." };

            String fact = "";

            // randomly select a fact

            Random randomGenerator = new Random();
            int randomNumber = randomGenerator.nextInt(facts.length);
            fact = facts[randomNumber] + "";
        }
    };
    mfactbutton.setOnClickListener(Listener);


}
在选择随机事实之后

mfacttext.setText(fact);
用这个

Random randomGenerator = new 
int randomNumber = randomGenerator.nextInt(facts.length);
fact = facts[randomNumber] + "";

mfacttext.setText(fact);

您的
按钮
单击正确,但主要是您没有将
事实
值设置为
文本视图

#正如您在
onCreate()
外部声明的
按钮和
TextView
一样,无需在
onCreate()
内部再次声明它

使用:

  mfactbutton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
// Your code goes here
    }
});
而不是:

    mfactbutton = (Button) findViewById(R.id.button);
    mfacttext = (TextView) findViewById(R.id.textView2);
    // TextView
    mfacttext.setText(fact);

    // Toast
    Toast.makeText(getApplicationContext(), "Fact: " + fact, Toast.LENGTH_SHORT).show();
#.
onClick()
方法中,在
TextView
或show
Toast
消息上显示
事实
值:

    Button mfactbutton = (Button) findViewById(R.id.button);
    TextView mfacttext = (TextView) findViewById(R.id.textView2);
以下是工作代码:

    mfactbutton = (Button) findViewById(R.id.button);
    mfacttext = (TextView) findViewById(R.id.textView2);
    // TextView
    mfacttext.setText(fact);

    // Toast
    Toast.makeText(getApplicationContext(), "Fact: " + fact, Toast.LENGTH_SHORT).show();
输出:

    mfactbutton = (Button) findViewById(R.id.button);
    mfacttext = (TextView) findViewById(R.id.textView2);
    // TextView
    mfacttext.setText(fact);

    // Toast
    Toast.makeText(getApplicationContext(), "Fact: " + fact, Toast.LENGTH_SHORT).show();

嗨,我刚刚创建了一个简单的项目。如果单击该按钮,它将在textview2上生成随机文本

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Button mfactbutton;
    private TextView mfacttext;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mfactbutton = (Button) findViewById(R.id.button);
        mfacttext = (TextView) findViewById(R.id.textView2);

        // now we need to make out button to click
        View.OnClickListener Listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String[] facts = {
                        "Ants stretch when they wake up in the morning.",
                        "Ostriches can run faster than horses.",
                        "Olympic gold medals are actually made mostly of silver.",
                        "You are born with 300 bones; by the time you are an adult you will have 206.",
                        "It takes about 8 minutes for light from the Sun to reach Earth.",
                        "Some bamboo plants can grow almost a meter in just one day.",
                        "The state of Florida is bigger than England.",
                        "Some penguins can leap 2-3 meters out of the water.",
                        "On average, it takes 66 days to form a new habit.",
                        "Mammoths still walked the earth when the Great Pyramid was being built." };

                String fact = "";

                Random randomGenerator = new Random();
                int randomNumber = randomGenerator.nextInt(facts.length);

                fact = facts[randomNumber] + "";

                mfacttext.setText(fact);
                Toast.makeText(getApplicationContext(), "Fact: " + fact, Toast.LENGTH_SHORT).show();
            }
        };

        mfactbutton.setOnClickListener(Listener);
    }
}
}

和activity_main.xml

public class MainActivity extends AppCompatActivity {
TextView secondTextView;
String[] Textlist = { "Hello man  ", "sonam", "tashi","i am man","hellow world"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    secondTextView = (TextView) findViewById(R.id.textView2);

    Button mybtn = (Button) findViewById(R.id.btn);
    mybtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            changeTextView_two_text();

        }
    });
}

private void changeTextView_two_text() {

    Random random = new Random();
    String rand = Textlist[random.nextInt(Textlist.length)];
  //  String randomText = TextList[random.nextInt(TextList.length)];

    secondTextView.setText(rand);


}


希望这对你有帮助

因为您没有使用
mfacttext
执行任何操作,所以在这一行之后添加
mfacttext.setText(fact)
fact=facts[randomNumber]+”@Pavneet_Singh