制作一个按钮OnClickListener无法正常工作?

时间:2016-04-15 19:53:10

标签: java android

我收到错误" btnCalculate.setOnClickListener(new View.OnClickListener()"说

  

"方法调用' btnCalculate.setOnClickListener(new   View.OnClickListener(){@ Override ...'可能会产生   '显示java.lang.NullPointerException"

这是什么意思,我该如何解决?

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity
{
    TextView totalTextView;
    EditText edtPercentage;
    EditText edtNumber;

    float percentage;
    float number;
    float total;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        percentage = 0;
        number = 0;
        total = 0;

        Button btnCalculate = (Button) findViewById(R.id.btnCalculate);
        btnCalculate.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                percentage = Float.parseFloat(edtPercentage.getText().toString());
                number = Float.parseFloat(edtNumber.getText().toString());
                total = number * percentage/100;
                totalTextView.setText(String.valueOf(total));
            }
        });
    }

    @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);
    }
}

3 个答案:

答案 0 :(得分:0)

btnCalculate在布局中找不到时可能为null - 要删除可能添加的警告:

 assert(btnCalculate!=null)
警告前

答案 1 :(得分:0)

你没有连接&#39;使用edtNumber方法findReferenceByID() onCreate..... setContentView(R.layout.activity_main()); totalTextView = (TextView)findViewByID(R.id.<IdOfTextView>); edtPercentage = (EditText)findViewByID(R.id.<IdOfEditText>); edtNumber = (EditText)findViewByID(R.id.<IdOfEditText>); ..... 。在<?php echo form_open('esthetique/upload_img'); ?> 方法中执行

<?php echo form_open_multipart('esthetique/upload_img'); ?>

答案 2 :(得分:0)

msg很清楚:

此对象TextView totalTextView必须在尝试设置文本之前初始化(通常在onCreate方法中),但是您忘了这样做,您的应用程序将尽快崩溃你点击按钮......

修复它!

这样做:

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
       totalTextView = (TextView) findViewById(R.id.toolbar); //init the text view....
       ....