我的吸气剂和安装者不工作?返回nullobjectreference?

时间:2017-04-28 15:15:30

标签: java android methods static getter-setter

这里是Java / Android开发的新手。最近我一直在使用静态变量来访问另一个类中主活动的用户输入。我被告知要将此更改为getter / setter方法,因为使用静态变量不是java中的好代码(所以我听说过?)。因此,我创建了这些方法,并通过创建活动的实例(本例中为MainActivity),然后使用构造函数调用方法,在其他类中访问它们。无论如何,这是我的代码......

public class MainActivity extends AppCompatActivity {
//declare variables
EditText name;
EditText data;
EditText wlan;
EditText utility;
Button addservice;
ListView lv;
ListView lv2;
ListView lv3;
ListView lv4;
public ArrayList<String> servicenames;
public ArrayList<String> dimensions;
public ArrayList<Double> costDATA;
public ArrayList<Double> costWLAN;
public ArrayList<Double> costUTILITY;
ArrayAdapter<String> namesAdapter;
ArrayAdapter<Double> dataAdapter;
ArrayAdapter<Double> wlanAdapter;
ArrayAdapter<Double> utilityAdapter;

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

    //map the components to the variables
    name = (EditText) findViewById(R.id.servicename);
    data = (EditText) findViewById(R.id.data);
    wlan = (EditText) findViewById(R.id.wlan);
    utility = (EditText) findViewById(R.id.utility);
    addservice = (Button) findViewById(R.id.addservice);
    lv = (ListView) findViewById(R.id.lv);
    lv2 = (ListView) findViewById(R.id.lv2);
    lv3 = (ListView) findViewById(R.id.lv3);
    lv4 = (ListView) findViewById(R.id.lv4);

    //create arraylists for each component
    servicenames = new ArrayList<String>();
    dimensions = new ArrayList<String>();
    costDATA = new ArrayList<Double>();
    costWLAN = new ArrayList<Double>();
    costUTILITY = new ArrayList<Double>();

    //create adapters to pass on the arraylist
    namesAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, servicenames);
    dataAdapter = new ArrayAdapter<Double>(MainActivity.this, android.R.layout.simple_list_item_1, costDATA);
    wlanAdapter = new ArrayAdapter<Double>(MainActivity.this, android.R.layout.simple_list_item_1, costWLAN);
    utilityAdapter = new ArrayAdapter<Double>(MainActivity.this, android.R.layout.simple_list_item_1, costUTILITY);

    //display each arraylist in the listviews
    lv.setAdapter(namesAdapter);
    lv2.setAdapter(wlanAdapter);
    lv3.setAdapter(dataAdapter);
    lv4.setAdapter(utilityAdapter);
    namesAdapter.notifyDataSetChanged();
    dataAdapter.notifyDataSetChanged();
    wlanAdapter.notifyDataSetChanged();
    utilityAdapter.notifyDataSetChanged();
    dimensions.add("DATA");
    dimensions.add("WLAN");
    onClickBtn();
}

public void onClickBtn() { //when user clicks button, the user input is added to the listview, and cleared for the next service

    addservice.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String namesOfService = name.getText().toString(); //user input for service names
            String costOfData = data.getText().toString(); //user input for data costs
            String costOfWLAN = wlan.getText().toString(); //user input for wlan costs
            String costOfUtility = utility.getText().toString(); //user input for utility costs
            Double doubleWLAN = Double.parseDouble(costOfWLAN); //convert user input into double
            Double doubleData = Double.parseDouble(costOfData);
            Double doubleUtility = Double.parseDouble(costOfUtility);
            costDATA.add(doubleData); //add the double costs to each resource arraylist
            costWLAN.add(doubleWLAN);
            costUTILITY.add(doubleUtility);
            servicenames.add(namesOfService);
            dimensions.add(namesOfService);
            int dimensionSize = dimensions.size();
            namesAdapter.notifyDataSetChanged();
            dataAdapter.notifyDataSetChanged();
            wlanAdapter.notifyDataSetChanged();
            utilityAdapter.notifyDataSetChanged();
            name.setText(""); //empty the edit text fields when button is clicked
            wlan.setText("");
            data.setText("");
            utility.setText("");

        }
    });
}

public void nextButton(View view) //next button, onto the next activity
{
    Intent intent = new Intent(MainActivity.this, ParticleActivity.class);
    startActivity(intent);
}

public int getDimensions(){

    return dimensions.size();

}

public ArrayList<String> getElements(){ return servicenames;}

public ArrayList<Double> getCostDATA(){;return costDATA;}

public ArrayList<Double> getCostWLAN(){return costUTILITY;}

public ArrayList<Double> getCostUTILITY(){return costUTILITY;}
}

我需要访问其他类中的arraylists costData,costUtilities,costWlan和维度。这是我的另一课......

public class CustomUseCase extends Test {

MainActivity mainActivity = new MainActivity();
ParticleActivity particleActivity = new ParticleActivity();
public int numberOfDimensions = mainActivity.getDimensions();
private ArrayList<Double> costData = mainActivity.getCostDATA(); //costs that the user enters for each resource
private ArrayList<Double> costWlan = mainActivity.getCostWLAN();
private ArrayList<Double> costUtilities = mainActivity.getCostUTILITY();
private double batteryCost = particleActivity.getBatteryCost();
private int maxIter;
private int noParticles;

我在这里做错了什么?解释为什么我弄错了将是一个奖金!亲切的问候。

1 个答案:

答案 0 :(得分:0)

您的MainActivity类没有构造函数。这意味着您定义了所有变量,但在调用

时,它们都没有实际初始化
MainActivity mainActivity = new MainActivity();

new关键字在这里非常重要。请注意,这并不一定意味着您的应用无法运行,具体取决于它的设置,但测试用例肯定不会。

我认为在你的情况下你需要添加:

public MainActivity(){
  onCreate(savedInstance); //Bundle savedInstance
}