有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java不能实例化类;切换活动时没有空构造函数错误

我知道有很多人有类似的问题,但我找不到正确的答案。当我尝试运行我的应用程序时,我遇到了这个错误。它发生在我尝试切换活动时,因此我认为我没有正确地执行此操作

第一项活动:

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);

        }

    }
);
}

}

第二项活动:

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, 安卓.R.layout.simple_list_item_1, nameList));

}

}

基本上,我希望在单击第一个活动中的按钮后打开第二个活动。当我单击此按钮时会发生错误。我已经在Android清单中加入了这两个活动,所以我认为这不是问题所在。我该怎么办


共 (2) 个答案

  1. # 1 楼答案

    正如错误消息所说,活动的任何子类都必须有一个no-arg构造函数。看起来你正试图将应用程序的视图和模型混合到一个类中。我强烈建议您了解模型-视图-控制器模式。特别是,您应该创建一个单独的纯Java类来保存将在ListView中显示的数据

  2. # 2 楼答案

    首先,你必须为下面的示范大学项目做一个课程 并更改代码,如下所述

    public class CollegeList extends ListActivity {
    
    ArrayList<CollegeItem> collegeLists=new ArrayList<CollegeItem>();
    ArrayList<String> nameList = new ArrayList<String>();
    
    Comparator<CollegeItem> compareByScoreDistance = new Comparator<CollegeItem>(){
        public int compare(CollegeItem a, CollegeItem b){
            return Double.compare(a.getScoreDistance(), b.getScoreDistance());
        }
    };
    
    
    CollegeItem michigan = new CollegeItem(3.79,30,2020,"University of Michigan","Ann Arbor, Michigan");
    CollegeItem berkeley = new CollegeItem(3.84,30,2040,"University of California Berkeley","Berkeley, California");
    CollegeItem stanford = new CollegeItem(3.96,33,2215,"Stanford University","Stanford, California");
    
    
    @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(CollegeItem collegeList : collegeLists){
            nameList.add(collegeList.getName());
        }
    
        setListAdapter(new ArrayAdapter<String>(CollegeList.this, android.R.layout.simple_list_item_1, nameList));
    
    
    
    }
    private class CollegeItem {
     private double gpa;
    private int act;
    private int sat;
    private String name;
    private String location;
    private double score;
    private double scoreDistance;
    
    public CollegeItem(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;
    }
    }
    }