gpt4 book ai didi

java - 无法实例化类;无空构造函数错误 - Android 切换 Activity

转载 作者:行者123 更新时间:2023-12-01 22:52:16 25 4
gpt4 key购买 nike

我知道有很多人有类似的问题,但我找不到适合我的正确答案。当我尝试运行我的应用程序时,出现此错误。当我尝试切换 Activity 时会发生这种情况,所以我相信我没有正确执行此操作。

第一个 Activity :

public class MainActivity extends Activity {

public static double scoreDouble;
TextView score;
EditText gpa;
EditText sat;
EditText act;
Button calc;

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

gpa = (EditText) findViewById(R.id.gpa);

sat = (EditText) findViewById(R.id.sat);

act = (EditText) findViewById(R.id.act);

score = (TextView) findViewById(R.id.score);
calc = (Button) findViewById(R.id.calc);

calc.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String gpaString = gpa.getText().toString();
if (gpaString.equals("")) {
gpaString = "0";
}
double gpaDouble = Double.parseDouble(gpaString);

String satString = sat.getText().toString();
if (satString.equals("")) {
satString = "0";
}
int satInt = Integer.parseInt(satString);

String actString = act.getText().toString();
if (actString.equals("")) {
actString = "0";
}
int actInt = Integer.parseInt(actString);
if (actInt / 36.0 < satInt / 2400.0) {
scoreDouble = (0.6 * gpaDouble * 25)
+ (0.4 * ((double) satInt / 2400.0) * 100.0);
} else {
scoreDouble = (0.6 * gpaDouble * 25)
+ (0.4 * ((double) actInt / 36.0) * 100.0);
}

Intent myIntent = new Intent(MainActivity.this, CollegeList.class);
MainActivity.this.startActivity(myIntent);

}

}
);
}

}

第二项 Activity :

public class CollegeList extends ListActivity {

ArrayList<CollegeList> collegeLists=new ArrayList<CollegeList>();
ArrayList<String> nameList = new ArrayList<String>();

Comparator<CollegeList> compareByScoreDistance = new Comparator<CollegeList>(){
public int compare(CollegeList a, CollegeList b){
return Double.compare(a.getScoreDistance(), b.getScoreDistance());
}
};


CollegeList michigan = new CollegeList(3.79,30,2020,"University of Michigan","Ann Arbor, Michigan");
CollegeList berkeley = new CollegeList(3.84,30,2040,"University of California Berkeley","Berkeley, California");
CollegeList stanford = new CollegeList(3.96,33,2215,"Stanford University","Stanford, California");



private double gpa;
private int act;
private int sat;
private String name;
private String location;
private double score;
private double scoreDistance;

public CollegeList(double gpa, int act, int sat, String name, String location){
this.gpa = gpa;
this.act = act;
this.sat = sat;
this.name = name;
this.location = location;
if(act/36.0>sat/2400.0){
this.score = 0.6*gpa*25.0+0.4*(act/36.0)*100.0;
}else{
this.score = 0.6*gpa*25.0+0.4*(sat/2400.0)*100.0;
}
this.scoreDistance = Math.abs(this.score-MainActivity.scoreDouble)/MainActivity.scoreDouble;

}

public double getGpa(){
return this.gpa;
}
public int getAct(){
return this.act;
}
public int getSat(){
return this.sat;
}
public String getName(){
return this.name;
}
public String getLocation(){
return this.location;
}
public double getScore(){
return this.score;
}
public double getScoreDistance(){
return this.scoreDistance;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent intent = getIntent();
collegeLists.add(michigan);
collegeLists.add(berkeley);
collegeLists.add(stanford);

Collections.sort(collegeLists, compareByScoreDistance);

for(CollegeList collegeList : collegeLists){
nameList.add(collegeList.getName());
}

setListAdapter(new ArrayAdapter<String>(CollegeList.this, android.R.layout.simple_list_item_1, nameList));

}

}

本质上,我希望在单击第一个 Activity 中的按钮后打开第二个 Activity 。当我单击此按钮时,会发生错误。我已将这两项 Activity 放入 Android list 中,所以我认为这不是问题。我必须做什么?

最佳答案

Activity 的任何子类都必须有一个无参数构造函数,正如错误消息所示。看来您正在尝试将应用程序的 View 和模型混合到一个类中。我强烈建议您学习模型- View - Controller 模式。特别是,您应该创建一个单独的普通 Java 类来保存将在 ListView 中显示的数据。

关于java - 无法实例化类;无空构造函数错误 - Android 切换 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24591962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com