致命异常:主要;面向对象; JAVA 中的运行时异常

FATAL EXCEPTION: main ; OOP ; RuntimeException in JAVA

我开始学习代码,java.The 提交的项目包含一个名为 ReportCard.java 的 .java 文件。而且我不想要 layout.xml 文件。我只想创建一个 class。我不明白我的错误在哪里。非常感谢您的帮助:)

package com.example.android.reportcard; public class ReportCard {

// variable initializations and 
// necessary setters and getter functions

private String setGrade(int math, int science, int socialStudies) {
    String grade;
    mSum = math + science + socialStudies;
    mPercentage = mSum / TOTAL;

    if (mPercentage >= 90.0) {
        grade = "A";
    } else if (mPercentage < 90.0 && mPercentage >= 80.0) {
        grade = "B";
    } else if (mPercentage < 80.0 && mPercentage >= 70.0) {
        grade = "C";
    } else if (mPercentage < 70.0 && mPercentage >= 60.0) {
        grade = "D";
    } else if (mPercentage < 60.0) {
        grade = "Fail";
    } else {
        grade = "error";
    }
    return grade;
}


/**
 * Create new report card object.
 *
 * @param schoolName
 * @param teacherName
 * @param year
 * @param studentName
 * @param mathGrade
 * @param scienceGrade
 * @param socialStudiesGrade
 */

public ReportCard(String schoolName, String teacherName, String year, String studentName,
                  int mathGrade, int scienceGrade, int socialStudiesGrade) {
    mSchoolName = schoolName;
    mTeacherName = teacherName;
    mYear = year;
    mStudentName = studentName;
    this.mMathGrade = mathGrade;
    this.mScienceGrade = scienceGrade;
    this.mSocialStudiesGrade = socialStudiesGrade;
}

public String toString() {
    return "School: " + getSchoolName() + '\n' +
            "Student Name: " + getStudentName() + '\n' +
            "Teacher Name: " + getTeacherName() + '\n' +
            "Year: " + getYear() + '\n' +
            "Math Grade: " + mMathGrade + '\n' +
            "Science Grade: " + mScienceGrade + '\n' +
            "Social Studies Grade: " + mSocialStudiesGrade + '\n' +
            "Grade: " + setGrade(mMathGrade, mScienceGrade, mSocialStudiesGrade);
    }
}

错误:

Process: com.example.android.reportcard, PID: 27731
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.reportcard/com.example.android.reportcard.ReportCard}: java.lang.InstantiationException: java.lang.Class<com.example.android.reportcard.ReportCard> has no zero argument constructor
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    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)
 Caused by: java.lang.InstantiationException: java.lang.Class<com.example.android.reportcard.ReportCard> has no zero argument constructor
    at java.lang.Class.newInstance(Native Method)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    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) 

看看这一行:

Caused by: java.lang.InstantiationException: java.lang.Class<com.example.android.reportcard.ReportCard> has no zero argument constructor

如果您没有定义一些自定义构造函数 - java 会为您创建一个(默认没有参数) 但如果你这样做 - 那么你应该手动定义每个必需的。

Android 应用程序与控制台应用程序不同。 Android 应用程序始终需要启动 Activity 形式的图形入口点,以便平台知道从哪里开始执行代码。将其视为常规 Java 应用程序中的 main 方法。

来自Android Activity Documentation:

An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View).

为了创建 ReportClass 对象并使用其声明的字段和方法,您需要声明应用程序的入口点。为此,创建一个新的 class 并将其命名为 MyMainActivity

public class MyMainActivity extends Activity
{


}

再次,从Android Activity Documentation转述:

The onCreate(Bundle) method will be implemented by almost all of the classes extending Activity. This method is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.

下一步是实现文档中提到的onCreate方法。鉴于您不打算与用户界面交互,您可以使用空布局简单地调用 setContentView()。暂时不要关注 Bundle 参数,因为它只会让您感到困惑。

public class MyMainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
         super(savedInstanceState);
         setContentView(R.layout.my_empty_layout);
    }
}

您应该在 /res/layout 文件夹中创建 my_empty_layout.xml 文件,您可以保留生成的内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

</LinearLayout>

您需要做的最后一件事是向平台指定 Activity 应该是您应用程序的入口点。即使您只声明了一个,您仍然需要指定它。这是在位于 /app/manifests 的 Android 清单文件中完成的。那里已经有 xml 内容,包括 <application> </application> 标签。在此元素内,添加一个 activity 子节点 (<activity>``</activity>),您可以在其中指定声明的 activity 的完整路径,并使用 [=] 将其标记为您的应用程序入口点26=]。 activity 节点应该看起来像这样:

<activity android:name="path.to.the.activity.MyMainActivity">

     <intent-filter>
          <action android:name="android.intent.action.MAIN"/>
          <category android:name="android.intent.category.LAUNCHER"/>
     </intent-filter>

</activity>

现在您终于有了 Android 应用程序的入口点,您可以开始创建 ReportCard 对象并开始摆弄它们:

public class MyMainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
         super(savedInstanceState);
         setContentView(R.layout.my_empty_layout);

         ReportCard myFirstReportCard = new ReportCard("SO College", "Mr. Kooijman", "2016", "aylin", 80, 72, 55);
         int socialStudiesGrade = myFirstReportCard.getSocialStudiesGrade();
    }
}

但是,我强烈建议您阅读更多有关 Android 应用程序是什么以及应该如何创建应用程序的内容。您提出的问题表明您对平台是什么、它如何以及如何使您能够做什么以及您应该如何做非常缺乏理解。

我也有一个错误声明说 "has no zero argument constructor"。发生的事情是该程序试图从我的 DatabaseOpenHelper class 启动程序,而不是从主 class 启动程序。这样做是因为我首先创建了 DatabaseOpenHelper class,然后创建了 MainActivity class.
解决方案花了一段时间才找到,但事实证明相当简单。我必须进入清单文件并找到包含 "DatabaseOpenHelper" 的行,它告诉程序从那里开始,然后将其从 "DatabaseOpenHelper" 更改为 "MainActivity".