其他函数中的按钮初始化而不是OnCreate()方法

时间:2016-02-11 05:32:31

标签: java android

我们可以在OnCreate方法中定义一个函数,该函数可用于初始化按钮和所有其他小部件。我尝试了以下但是我得到了

  

java.lang.NullPointerException:尝试调用虚方法' void   android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)'在...上   空对象引用错误

package com.conceptkillers.www.electromania1;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;

/**
 * Created by Nikesh on 11/02/2016.
 */
public class OhmsLawActivity extends AppCompatActivity {

    int checkedCount = 0;
    LinearLayout vlayout,ilayout,rlayout,playout;
    CheckBox checkBox1,checkBox2,checkBox3,checkBox4;
    Spinner spinnerV,spinnerI,spinnerR,spinnerP;
    EditText edittextv,edittexti,edittextr,edittextp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setTitle("Ohms Law");
        InitializeAllWidgets();

    }

    public void InitializeAllWidgets() {

        checkBox1 = (CheckBox) findViewById(R.id.checkbox_v);
        checkBox2 = (CheckBox) findViewById(R.id.checkbox_i);
        checkBox3 = (CheckBox) findViewById(R.id.checkbox_r);
        checkBox4 = (CheckBox) findViewById(R.id.checkbox_p);

        edittextv = (EditText) findViewById(R.id.edittext_v);
        edittexti = (EditText) findViewById(R.id.edittext_i);
        edittextr = (EditText) findViewById(R.id.edittext_r);
        edittextp = (EditText) findViewById(R.id.edittext_p);

        String[] valuesVoltageSpinner = {"YV","ZV","EV","PV","TV","GV","MV","KV"
                ,"V","mV","uV","nV","pV","fV","aV","zV","yV"};
        String[] valuesCurrentSpinner = {"YA","ZA","EA","PA","TA","GA","MA","KA"
                ,"A","mA","uA","nA","pA","fA","aA","zA","yA"};
        String[] valuesResistanceSpinner = {"YOhm","ZOhm","EOhm","POhm","TOhm","GOhm","MOhm","KOhm"
                ,"Ohm","mOhm","uOhm","nOhm","pOhm","fOhm","aOhm","zOhm","yOhm"};
        String[] valuesPowerSpinner = {"YW","ZW","EW","PW","TW","GW","MW","KW"
                ,"W","mW","uW","nW","pW","fW","aW","zW","yW"};

        spinnerV = (Spinner) findViewById(R.id.spinner_v);
        spinnerI = (Spinner) findViewById(R.id.spinner_i);
        spinnerR = (Spinner) findViewById(R.id.spinner_r);
        spinnerP = (Spinner) findViewById(R.id.spinner_p);

        ArrayAdapter<String> voltageAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, valuesVoltageSpinner);
        voltageAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spinnerV.setAdapter(voltageAdapter);

        ArrayAdapter<String> currentAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, valuesCurrentSpinner);
        currentAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spinnerI.setAdapter(currentAdapter);

        ArrayAdapter<String> resistanceAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, valuesResistanceSpinner);
        resistanceAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spinnerR.setAdapter(resistanceAdapter);

        ArrayAdapter<String> powerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, valuesPowerSpinner);
        powerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spinnerP.setAdapter(powerAdapter);

        checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                handleCheckBox1();
            }
        });

        checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                handleCheckBox2();
            }
        });

        checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                handleCheckBox3();
            }
        });

        checkBox4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                handleCheckBox4();
            }
        });
    }

    public void onButtonClicked(View v) {

        switch(v.getId()) {
            case R.id.simple_voltage:
                checkBox1.setVisibility(v.INVISIBLE);
                checkBox2.setVisibility(v.VISIBLE);
                checkBox3.setVisibility(v.VISIBLE);
                checkBox4.setVisibility(v.VISIBLE);
                checkedCount = 0;
                break;
            case R.id.simple_current:
                checkBox1.setVisibility(v.VISIBLE);
                checkBox2.setVisibility(v.INVISIBLE);
                checkBox3.setVisibility(v.VISIBLE);
                checkBox4.setVisibility(v.VISIBLE);
                checkedCount = 0;
                break;
            case R.id.simple_resistance:
                checkBox1.setVisibility(v.VISIBLE);
                checkBox2.setVisibility(v.VISIBLE);
                checkBox3.setVisibility(v.INVISIBLE);
                checkBox4.setVisibility(v.VISIBLE);
                checkedCount = 0;
                break;
            case R.id.simple_power:
                checkBox1.setVisibility(v.VISIBLE);
                checkBox2.setVisibility(v.VISIBLE);
                checkBox3.setVisibility(v.VISIBLE);
                checkBox4.setVisibility(v.INVISIBLE);
                checkedCount = 0;
                break;
        }
    }

    private void handleCheckBox1() {

        if(checkedCount < 2) {

            if (checkBox1.isChecked()) {
                checkedCount++;
                edittextv.setVisibility(View.VISIBLE);
                spinnerV.setVisibility(View.VISIBLE);
            } else {
                checkedCount--;
                edittextv.setVisibility(View.INVISIBLE);
                spinnerV.setVisibility(View.INVISIBLE);
            }

        }
    }

    private void handleCheckBox2() {

        if(checkedCount < 2) {
            if (checkBox2.isChecked()) {
                checkedCount++;
                edittexti.setVisibility(View.VISIBLE);
                spinnerI.setVisibility(View.VISIBLE);
            } else {
                checkedCount--;
                edittexti.setVisibility(View.INVISIBLE);
                spinnerI.setVisibility(View.INVISIBLE);
            }
        }
    }

    private void handleCheckBox3() {

        if(checkedCount < 2) {
            if (checkBox3.isChecked()) {
                checkedCount++;
                edittextr.setVisibility(View.VISIBLE);
                spinnerR.setVisibility(View.VISIBLE);
            } else {
                checkedCount--;
                edittextr.setVisibility(View.INVISIBLE);
                spinnerR.setVisibility(View.INVISIBLE);
            }
        }
    }

    private void handleCheckBox4(){

        if(checkedCount < 2) {
            if (checkBox4.isChecked()) {
                checkedCount++;
                edittextp.setVisibility(View.VISIBLE);
                spinnerP.setVisibility(View.VISIBLE);
            } else {
                checkedCount--;
                edittextp.setVisibility(View.INVISIBLE);
                spinnerP.setVisibility(View.INVISIBLE);
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您错过了setContentView

setContentView(R.layout.main)

答案 1 :(得分:0)

您尚未为活动设置任何布局。在代码中使用setContentView(int layoutId),它将附加这些小部件(微调器)所属的布局。

其余代码似乎很好。我的意思是你可以有一个初始化小部件的方法。

答案 2 :(得分:0)

更改您的代码并添加您的布局

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    setContectView(R.layout.view);

        this.setTitle("Ohms Law");
        InitializeAllWidgets();

    }