我目前正在构建一个用户界面来支持手机优化的PSO项目。我有一个列表视图,显示用户输入arraylist,但是当我点击我的UI上的“addservice”按钮时,listview没有显示任何内容?以下是我主要活动的代码......
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;
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
Services services = new Services();
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);
namesAdapter.notifyDataSetChanged();
dataAdapter.notifyDataSetChanged();
wlanAdapter.notifyDataSetChanged();
utilityAdapter.notifyDataSetChanged();
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 services = new Services();
services.addDataCost(doubleData);
services.addWlanCost(doubleWLAN);
services.addUtilityCost(doubleUtility);
services.addServiceName(namesOfService);
services.addDimensions(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);
}
更新:
当我尝试输入服务名称时,这是我的日志,以及数据,wlan和实用程序的成本......
E/AndroidRuntime: FATAL EXCEPTION: main
Process: ai69.psoui, PID: 2926
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at test_classes.Services.addDimensions(Services.java:30)
at ai69.psoui.MainActivity$1.onClick(MainActivity.java:103)
at android.view.View.performClick(View.java:5198)
at android.view.View.onKeyUp(View.java:9879)
at android.widget.TextView.onKeyUp(TextView.java:6248)
at android.view.KeyEvent.dispatch(KeyEvent.java:2664)
at android.view.View.dispatchKeyEvent(View.java:9234)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2395)
at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1727)
at android.app.Activity.dispatchKeyEvent(Activity.java:2725)
at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:541)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:319)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2310)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4127)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4089)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3820)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3981)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2253)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1874)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1865)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2230)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:323)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I/Process: Sending signal. PID: 2926 SIG: 9
Application terminated.
这也是我的服务类。这个课程是为了保存用户输入的所有信息,我在这里做错了什么?当用户为其添加值时,为什么我的arraylist不会更新?我是android studio / java的新手(3个月),我正试图以最OO的方式实现我的UI。
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 int numOfDimensions = dimensions.size();
private double solution;
private double battery;
public ArrayList<String> getServiceNames(){
return this.serviceNames;
}
public void addServiceName(String services) {
this.serviceNames.add(services);
}
public void addDimensions(String services) {
this.dimensions.add(services);
}
public int getNumDimensions(){
return this.numOfDimensions;
}
public ArrayList<String> getDimensions(){
return this.dimensions;
}
public void addDataCost(double dataCost){
this.costDATA.add(dataCost);
}
public void addWlanCost(double wlanCost){
this.costWLAN.add(wlanCost);
}
public void addUtilityCost(double utilityCost){
this.costUTILITY.add(utilityCost);
}
public ArrayList<Double> getCostData(){
return this.costDATA;
}
public ArrayList<Double> getData(ArrayList<Double> Data){
this.costDATA = Data;
return Data;
}
public ArrayList<Double> getWlan(ArrayList<Double> Wlan){
this.costWLAN = Wlan;
return Wlan;
}
public ArrayList<Double> getUtility(ArrayList<Double> Utility){
this.costUTILITY = Utility;
return Utility;
}
public ArrayList<String> getDim(ArrayList<String> dimensions){
this.dimensions = dimensions;
return dimensions;
}
public ArrayList<Double> getCostWlan(){
return this.costWLAN;
}
public ArrayList<Double> getCostUtility(){
return this.costUTILITY;
}
public void setSolution(double solution){
this.solution = solution;
}
public double getSolution(){
return this.solution;
}
public void setBattery(double battery){
this.battery = battery;
}
public double getBattery(){
return this.battery;
}
}