【问题标题】:My getter and setters aren't working? Returning nullobjectreference?我的 getter 和 setter 不起作用?返回空对象引用?
【发布时间】:2017-09-26 18:31:11
【问题描述】:

这里是 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;}
}

我需要访问其他类中的数组列表 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;

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

【问题讨论】:

  • @creativecreatoror也许我不知道。但是在我的 mainactivity 中,您可以看到当用户输入一个成本时,它会被添加到每个数组列表中。因此,当我的 UI 启动时,我输入了这些成本,但是我的 UI 没有执行我需要的操作,因为它说它试图访问的数组是空的
  • 旁注:“好”可以是相对的。在性能方面,Android 指南建议不要使用内部 getter/setter,甚至对常量的静态 final 有一点:developer.android.com/training/articles/perf-tips.html

标签: java android methods static getter-setter


【解决方案1】:

您的 MainActivity 类没有构造函数。这意味着您的所有变量都已定义,但在您调用时实际上没有一个变量被初始化

MainActivity mainActivity = new MainActivity();

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

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

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

【讨论】:

  • 啊,有道理,谢谢。我在哪里插入这个?在主要活动中?我只是试图在 mA 中添加它,但我得到(无法解析方法 onCreate())?
  • 该错误是因为您需要将Bundle 作为参数传递给onCreate() 方法。我不知道你从哪里得到这个。我忘了在回答中提到这一点;在那里更新。
  • 构造函数和onCreate方法是同一个类吗?
  • 哪个构造函数?你的意思是我在哪里创建一个 MainActivity 的实例?如果是,那么没有
  • 构造函数应该是MainActivity类的一部分,同样的地方onCreate(Bundle savedInstance)被覆盖了。
猜你喜欢
  • 1970-01-01
  • 2013-01-08
  • 2012-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-02
相关资源
最近更新 更多