简体   繁体   English

按钮的 setOnClickListener 使应用程序崩溃

[英]setOnClickListener for a button crashes the app

So, When I try to launch this application it crashes.因此,当我尝试启动此应用程序时,它崩溃了。 However, it only crashes when I try to use the setOnClickListener.但是,它只会在我尝试使用 setOnClickListener 时崩溃。 If I comment out that small section it runs just fine.如果我注释掉那个小部分,它运行得很好。

public class MainActivity extends ActionBarActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_main);
    final TextView textView = (TextView) findViewById(R.id.textView);
    final Button button = (Button)findViewById(R.id.button);
    super.onCreate(savedInstanceState);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //textView.setText("Boop!");
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);

OK Working code!好的工作代码! I had to move super.onCreate up, and declair the textview inside the findviewbyID section.我不得不将 super.onCreate 向上移动,并在 findviewbyID 部分中声明 textview。 However, now it works as I intended.但是,现在它按我的预期工作。 Thank you guys for the help!谢谢你们的帮助! I just wanted to make something very basic for a date.我只是想为约会做一些非常基本的事情。 xD you guys are a life saver. xD 你们是救命稻草。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView textView = (TextView) findViewById(R.id.textView);
            textView.setText("Boop!");
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

WORKING在职的

The error is coming because you are calling the super.onCreate(savedInstanceState);错误即将到来,因为您正在调用super.onCreate(savedInstanceState); after the setContentView and then using the variables.setContentView ,然后使用变量。

Reason that the error is not coming until you don't write onClicklister: The variables textView and button are already null or in inconsistent state, but when you are writing the click listener, the variables are used and you are facing error.直到您不编写 onClicklister 才会出现错误的原因:变量 textView 和 button 已经为空或处于不一致状态,但是当您编写点击侦听器时,使用了这些变量并且您面临错误。

You declared textView as final :您将textView声明为final

A variable can be declared final.变量可以声明为final。 A final variable may only be assigned to once.一个 final 变量只能赋值一次。

final TextView textView = (TextView) findViewById(R.id.textView);

so you cannot modify it.所以你不能修改它。

You must get the view inside onclick method:您必须在 onclick 方法中获取视图:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText("Boop!");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM