Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用2个类访问带有用户输入的ArrayList?_Java_Android_Object_Arraylist_Getter Setter - Fatal编程技术网

Java 如何使用2个类访问带有用户输入的ArrayList?

Java 如何使用2个类访问带有用户输入的ArrayList?,java,android,object,arraylist,getter-setter,Java,Android,Object,Arraylist,Getter Setter,我有一个名为“服务”的对象类,如下所示 public class Services { private ArrayList<Double> costDATA = new ArrayList<>(); private ArrayList<Double> costWLAN = new ArrayList<>(); private ArrayList<Double> costUTILITY = new ArrayList<>()

我有一个名为“服务”的对象类,如下所示

public class Services {

private ArrayList<Double> costDATA = new ArrayList<>();
private ArrayList<Double> costWLAN = new ArrayList<>();
private ArrayList<Double> costUTILITY = new ArrayList<>();
private ArrayList<String> dimensions = new ArrayList<>();
private ArrayList<String> serviceNames = new ArrayList<>();
private double solution;
private double battery;

public void addServiceName(String services) {
    this.serviceNames.add(services);
}

public void addDimensions(String services) {
    this.dimensions.add(services);
}

public int getDimensionSize(){
    return dimensions.size();
}

public void addDataCost(double dataCost){
    costDATA.add(dataCost);
}

public void addWlanCost(double wlanCost){
    costWLAN.add(wlanCost);
}

public void addUtilityCost(double utilityCost){
    costUTILITY.add(utilityCost);
}

public void setSolution(double solution){
    this.solution = solution;
}

public double getSolution(){
    return solution;
}

public void setBattery(double battery){
    this.battery = battery;
}

public double getBattery() {
    return battery;
}


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

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

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

public ArrayList<String> getServiceNames() {
    return serviceNames;
}
}
公共类服务{
private ArrayList costDATA=new ArrayList();
private ArrayList costWLAN=new ArrayList();
private ArrayList costUTILITY=new ArrayList();
私有ArrayList维度=新建ArrayList();
private ArrayList serviceNames=new ArrayList();
私人双重解决方案;
私人双电池;
public void addServiceName(字符串服务){
this.serviceNames.add(服务);
}
公共void addDimensions(字符串服务){
本.维度.添加(服务);
}
public int getDimensionSize(){
返回维度。size();
}
公共void addDataCost(双倍数据成本){
costDATA.add(数据成本);
}
公共无效附加成本(双重成本){
成本wlan.add(wlanCost);
}
public void addUtilityCost(双重utilityCost){
costUTILITY.add(utilityCost);
}
公共无效设置解决方案(双重解决方案){
这个解决方案=解决方案;
}
公共双getSolution(){
回流液;
}
公共电池(双电池){
这个。电池=电池;
}
公共双电池(){
回流电池;
}
公共阵列列表getCostDATA(){
返回成本数据;
}
公共阵列列表getCostWLAN(){
返回成本;
}
公共阵列列表getCostUTILITY(){
返回成本效用;
}
公共ArrayList getServiceNames(){
返回服务名称;
}
}
我使用这个类来存储来自UI的MainActivity的用户输入数据。这是我的主要活动

public class MainActivity extends AppCompatActivity {
//declare variables
private EditText name;
private EditText data;
private EditText wlan;
private EditText utility;
private Button addservice;
ListView lv;
ListView lv2;
ListView lv3;
ListView lv4;

Services services = new Services();
public ArrayList<String> servicenames;
public ArrayList<String> dimensions;
private ArrayAdapter<String> namesAdapter;
private ArrayAdapter<Double> dataAdapter;
private ArrayAdapter<Double> wlanAdapter;
private ArrayAdapter<Double> utilityAdapter;
private ArrayList<Double> costDATA;
private ArrayList<Double> costWLAN;
private ArrayList<Double> costUTILITY;

@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 =  services.getServiceNames();
    costDATA = services.getCostDATA();
    costWLAN = services.getCostWLAN();
    costUTILITY = services.getCostUTILITY();

    //create adapters to pass on the arraylist
    namesAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, servicenames);
    dataAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, costDATA);
    wlanAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, costWLAN);
    utilityAdapter = new ArrayAdapter<>(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);
    services.addDimensions("DATA");
    services.addDimensions("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);

            services.addDataCost(doubleData);
            services.addWlanCost(doubleWLAN);
            services.addUtilityCost(doubleUtility);
            services.addDimensions(namesOfService);
            services.addServiceName(namesOfService);


            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类MainActivity扩展了AppCompatActivity{
//声明变量
私有文本名;
私有文本数据;
专用文本无线局域网;
私有编辑文本实用程序;
私人按钮服务;
ListView lv;
ListView lv2;
ListView lv3;
ListView lv4;
服务=新服务();
公共ArrayList服务名称;
公共阵列列表维度;
专用阵列适配器名称适配器;
专用阵列适配器数据适配器;
专用阵列适配器wlanAdapter;
专用阵列适配器实用程序适配器;
私有ArrayList costDATA;
私人ArrayList costWLAN;
私有ArrayList实用程序;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//将组件映射到变量
name=(EditText)findViewById(R.id.servicename);
数据=(EditText)findViewById(R.id.data);
wlan=(EditText)findviewbyd(R.id.wlan);
实用工具=(EditText)findViewById(R.id.utility);
addservice=(按钮)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);
//为每个组件创建ArrayList
servicenames=services.getServiceNames();
costDATA=services.getCostDATA();
costWLAN=services.getCostWLAN();
costUTILITY=services.getCostUTILITY();
//创建要在arraylist上传递的适配器
NameAdapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple\u list\u item\u 1,servicenames);
dataAdapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple\u list\u item\u 1,costDATA);
wlanAdapter=newarrayadapter(MainActivity.this,android.R.layout.simple\u list\u item\u 1,costWLAN);
utilityAdapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple\u list\u item\u 1,costUTILITY);
//在列表视图中显示每个arraylist
lv.setAdapter(名称适配器);
lv2.设置适配器(wlanAdapter);
lv3.setAdapter(dataAdapter);
lv4.设置适配器(实用适配器);
服务。添加维度(“数据”);
服务。添加维度(“WLAN”);
onClickBtn();
}
public void onClickBtn(){//当用户单击按钮时,用户输入将添加到listview,并为下一个服务清除
addservice.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
String namesOfService=name.getText().toString();//服务名称的用户输入
String costOfData=data.getText().toString();//用户输入数据成本
String CostOfLAN=wlan.getText().toString();//用户输入的wlan成本
String costOfUtility=utility.getText().toString();//用户输入的公用事业成本
double doubleWLAN=double.parseDouble(costOfWLAN);//将用户输入转换为双精度
double doubleData=double.parseDouble(costOfData);
double doubleUtility=double.parseDouble(costOfUtility);
//add(doubleData);//将双倍成本添加到每个资源arraylist
//costWLAN.add(doubleWLAN);
//添加(双重效用);
服务。addDataCost(doubleData);
addWlanCost(doubleWLAN);
服务。addUtilityCost(doubleUtility);
服务。添加维度(服务名称);
服务。addServiceName(服务名称);
namesAdapter.notifyDataSetChanged();
dataAdapter.notifyDataSetChanged();
wlanAdapter.notifyDataSetChanged();
utilityAdapter.notifyDataSetChanged();
name.setText(“”;//单击按钮时清空编辑文本字段
wlan.setText(“”);
data.setText(“”);
utility.setText(“”);
}
});
}
public void nextButton(查看)//下一个按钮,进入下一个活动
{
意向意向=新意向(MainActivity.this、ParticleActivity.class);
星触觉(意向);
}
现在,我的mainActivity工作正常,UI按我需要的方式显示用户输入成本和服务名称。完成后,用户进入一个名为“ParticleActivity”的活动,然后执行另外两个名为“CustomUseCase”和“CustomService”的java类。在我的“CustomUseCase”中类,我总是得到nullpointerexception,它表示我的arraylist为空。我在这里做错了什么?我正在尝试从服务对象访问arraylist,因为这是w
public class CustomServiceSelection implements Goodness {

Services services;
private ArrayList<Double> costData = services.getCostDATA();
private ArrayList<Double> costWlan = services.getCostWLAN();
private ArrayList<Double> costUtilities = services.getCostUTILITY();
private double batteryCost = services.getBattery();

public CustomServiceSelection(double costOfBattery, ArrayList<Double> costOfData, ArrayList<Double> costOfWlan,
                              ArrayList<Double> costOfUtilities) {

    if (costUtilities == null || costUtilities.size() < 1 || costData.size() < 1 || costWlan.size() < 1) {
        throw new RuntimeException("Please add atleast 1 cost to Data, WLAN and Utility");
    }
    if (batteryCost < 1) {
        throw new RuntimeException("Please enter a battery cost more than 1");
    }

    this.batteryCost = costOfBattery;
    this.costData = costOfData;
    this.costWlan = costOfWlan;
    this.costUtilities = costOfUtilities;
}
Services services; // null 

// These are all null references 
private ArrayList<Double> costData =  services.getCostDATA();
private ArrayList<Double> costWlan = services.getCostWLAN();
private ArrayList<Double> costUtilities = services.getCostUTILITY();
private double batteryCost = services.getBattery();