无法实例化类;没有空的构造函数错误 - Android切换活动

时间:2014-07-06 01:33:43

标签: java android android-intent android-activity instantiation

我知道有很多人有类似的问题,但我找不到正确的答案。我尝试运行我的应用程序时收到此错误。它发生在我尝试切换活动时,所以我相信我没有正确地做到这一点。

第一项活动:

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

}

}

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

2 个答案:

答案 0 :(得分:3)

Activity的任何子类都必须有一个no-arg构造函数,就像错误消息所说的那样。您似乎正在尝试将应用的视图和模型混合到一个类中。我强烈建议您了解模型 - 视图 - 控制器模式。特别是,你应该创建一个单独的普通ol&#39;保存将在ListView

中显示的数据的Java类

答案 1 :(得分:0)

首先,您必须为模型CollegeItem创建一个类,如下所示 并更改代码如下所述

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;
}
}
}
相关问题