SachinKS
SachinKS
采纳率85.7%
2013-05-27 03:45 阅读 2.8k

android-空指针异常

Android应用计数c=a+b,一直报nullpointerexception。

代码:

public class Kreasi extends Activity {

    // Inisialisasi
    private EditText e1;
    private EditText e2;

    private Spinner s1;

    private Button b1;
    private Button b2;

    private TextView t1;
    private TextView t2;
    private TextView t3;
    private TextView t4;
    private TextView t5;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.kreasi);

        // INISIALISASI VARIABEL
        e1 = (EditText)findViewById(R.id.isiharga);
        e1 = (EditText)findViewById(R.id.isilaba);

        s1 = (Spinner)findViewById(R.id.spin);

        b1 = (Button)findViewById(R.id.process);
        b2 = (Button)findViewById(R.id.reset);

        t1 = (TextView)findViewById(R.id.hMax);
        t2 = (TextView)findViewById(R.id.angPokok);
        t3 = (TextView)findViewById(R.id.angSewa);
        t4 = (TextView)findViewById(R.id.angTotal);

        b1.setOnClickListener(new OnClickListener(){
            int a,b;
            @Override
            public void onClick(View v) {
                hideSoftKeyboard(b1);
                String aa,bb;
                aa = e1.getText().toString();
                bb = e2.getText().toString();
                a = Integer.valueOf(aa);
                b = Integer.valueOf(bb);
                int c = (int) (a + b);
                t1.setText(Integer.toString(c));
            }
        });
    }
    // Menyembunyikan Keyboard
    public void hideSoftKeyboard(Button btn) {
        btn.setInputType(0);
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(btn.getWindowToken(), 0);
    }
    /*
    // Fungsi saat tombol process ditekan
    public void tombolprocess(){
        b1.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                hideSoftKeyboard(b1);
                aa = e1.getText().toString();
                bb = e2.getText().toString();
                Toast.makeText(getBaseContext(),"a="+aa +" b=" +bb,Toast.LENGTH_SHORT).show();
            }
        });
    }
    */
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    loFataMer loFataMer 2013-05-27 05:43

    因为你没给e2赋值:
    将:

    e1 = (EditText)findViewById(R.id.isiharga);
    e1 = (EditText)findViewById(R.id.isilaba);
    

    改为:

    e1 = (EditText)findViewById(R.id.isiharga);
    e2 = (EditText)findViewById(R.id.isilaba);
    
    点赞 评论 复制链接分享
  • Jave_ JaveZh 2013-05-27 05:34

    代码是正确的,不知道你所说的空指针是咋哪里

    点赞 评论 复制链接分享

相关推荐