2016-09-28 94 views
0

我開始學習代碼和java.The提交的項目由一個單一的.java文件,標題爲ReportCard.java。我不想要一個layout.xml文件。我想創建一個班級。我不明白我的錯誤在哪裏。非常感謝您的幫助:)致命例外:main;面向對象;運行時異常在JAVA

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)  
+0

刪除不需要的代碼形式的類 – Sanoop

回答

2

Android應用程序與控制檯應用程序不同。 Android應用程序總是需要啓動活動形式的圖形入口點,以便平臺知道從何處開始執行代碼。把它看作普通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對象,並使用其宣稱的領域和方法,你需要聲明你的應用程序的入口點。要做到這一點,請創建一個新班級,並將其稱爲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); 
    } 
} 

您應該創建my_empty_layout.xml文件中/res/layout文件夾,因爲是你可以離開所產生的內容:

<?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> 

你需要做的最後一件事是指定的平臺,活動應該是您的應用程序的入口點。即使你只聲明瞭一個,你仍然需要指定它。這在位於/app/manifests的Android Manifest文件中完成。這裏將會有xml的內容,其中包括<application></application>標籤。在此元素中,添加一個活動子節點(<activity>``</activity>),您可以在其中指定已聲明活動的完整路徑,並使用intent-filter將其標記爲應用程序入口點。那麼活動節點應該是這個樣子:

<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應用程序的內容,以及應該如何創建一個Android應用程序。您提出的問題表明,對平臺是什麼以及如何以及如何做以及應該如何做這些問題缺乏理解。

+0

非常感謝!是的,我需要更努力工作...... – Jenni

0

看這句話:

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

如果不定義一些自定義的構造函數 - 創建的java一個給你(默認沒有參數) 但是,如果你這樣做 - 那麼你應該定義每個需要的手冊。

+0

你好,我看到它之前,但當我被添加「無零參數構造函數」我 接收錯誤 – Jenni

0

我也有一個錯誤語句,說「沒有零參數構造函數」。發生了什麼事是程序試圖從我的DatabaseOpenHelper類啓動程序,而不是從主類開始。這是因爲我先創建了DatabaseOpenHelper類,然後創建了MainActivity類。
該解決方案需要一段時間才能找到,但結果相當簡單。我不得不進入清單文件並找到包含「DatabaseOpenHelper」的行,它告訴程序從那裏開始,然後將其從「DatabaseOpenHelper」更改爲「MainActivity」。

相關問題