我经常来这里寻找参考和帮助,以完成我的学校作业。我是Eclipse和Android的Java编程初学者。但是我有C ++,PL / SQL和HTML程序,所以我对基本的编程规则有所了解。到目前为止,程序运行但是当我创建intent并传递数据来计算GPA时,它会崩溃。我几乎花了几个小时寻找它,我似乎无法找到问题所在。任何帮助将不胜感激。提前谢谢!
package com.example.gpacalculator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class SecondActivity extends Activity implements OnClickListener {
private Button buttonGoback;
private double gpa=0.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle secondBundle = getIntent().getExtras();
buttonGoback = (Button) findViewById(R.id.buttonReturn);
buttonGoback.setOnClickListener(this);
int idGrades[] = getLetterIds();
String grade;
int hour;
String[] grades = new String[2];
int[] hours = new int[2];
for (int i=0; i < 1; i++)
{
grade = secondBundle.getString("grade_"+i);
grades[i]=grade;
hour = secondBundle.getInt("hourse_"+i,3);
hours[i] =hour;
}
gpa = calculateGPA(grades, hours);
}
public void onClick(View v)
{
TextView results = (TextView) findViewById(R.id.textview_output);
String temp = String.valueOf(gpa);
results.setText(temp);
}
double calculateGPA(String[] grades,int[] hours)
{
double gpa=0.0d;
int totalHours=0;
for (int i=0; i < grades.length; i++)
{
totalHours += hours[i];
if(grades[i].toUpperCase().equals("A"))
gpa += 4.0 * hours[i];
if(grades[i].toUpperCase().equals("B"))
gpa += 3.0 * hours[i];
if(grades[i].toUpperCase().equals("C"))
gpa += 2.0 * hours[i];
if(grades[i].toUpperCase().equals("D"))
gpa += 1.0 * hours[i];
if(grades[i].toUpperCase().equals("D"))
gpa += 0.0 * hours[i]; //redundant!
}
gpa = gpa /totalHours;
return gpa;
}
public int[] getLetterIds()
{
int[] LetterIds = new int[10];
LetterIds[0] = R.id.edittext_course1;
LetterIds[1] = R.id.edittext_course2;
LetterIds[2] = R.id.edittext_course3;
LetterIds[3] = R.id.edittext_course4;
LetterIds[4] = R.id.edittext_course5;
LetterIds[5] = R.id.edittext_course6;
LetterIds[6] = R.id.edittext_course7;
LetterIds[7] = R.id.edittext_course8;
LetterIds[8] = R.id.edittext_course9;
LetterIds[9] = R.id.edittext_course10;
return LetterIds;
}
}
以下是第一项活动的代码:
package com.example.gpacalculator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button buttonStart;
private int GPA_CODE=1001;
public int[] getLetterIds() {
int[] LetterIds = new int[10];
LetterIds[0] = R.id.edittext_course1;
LetterIds[1] = R.id.edittext_course2;
LetterIds[2] = R.id.edittext_course3;
LetterIds[3] = R.id.edittext_course4;
LetterIds[4] = R.id.edittext_course5;
LetterIds[5] = R.id.edittext_course6;
LetterIds[6] = R.id.edittext_course7;
LetterIds[7] = R.id.edittext_course8;
LetterIds[8] = R.id.edittext_course9;
LetterIds[9] = R.id.edittext_course10;
return LetterIds;
}
public int[] getHoursIds() {
int[] HoursIds = new int[10];
HoursIds[0] = R.id.edittext_hrs1;
HoursIds[1] = R.id.edittext_hrs2;
HoursIds[2] = R.id.edittext_hrs3;
HoursIds[3] = R.id.edittext_hrs4;
HoursIds[4] = R.id.edittext_hrs5;
HoursIds[5] = R.id.edittext_hrs6;
HoursIds[6] = R.id.edittext_hrs7;
HoursIds[7] = R.id.edittext_hrs8;
HoursIds[8] = R.id.edittext_hrs9;
HoursIds[9] = R.id.edittext_hrs10;
return HoursIds;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonStart = (Button) findViewById(R.id.buttonstart);
buttonStart.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(this, "Test", Toast.LENGTH_LONG).show();
Bundle firstBundle = new Bundle();
int letterarray[]= getLetterIds();
int hourarray[]= getHoursIds();
String gradestr;
int hours;
for (int i = 0; i < 1; i++)
{
gradestr = ((EditText) findViewById(letterarray[i])).getText().toString();
hours = Integer.parseInt(((EditText) findViewById(hourarray[i]))
.getText().toString());
firstBundle.putString("grade_" + i, gradestr);
firstBundle.putInt("hours_" + i, hours);
}
Intent firstIntent = new Intent(this, SecondActivity.class);
firstIntent.putExtras(firstBundle);
startActivity(firstIntent);
}
}
错误日志:
09-26 17:22:38.350: E/AndroidRuntime(29097): FATAL EXCEPTION: main
09-26 17:22:38.350: E/AndroidRuntime(29097): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gpacalculator/com.example.gpacalculator.SecondActivity}: java.lang.NullPointerException
09-26 17:22:38.350: E/AndroidRuntime(29097): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
09-26 17:22:38.350: E/AndroidRuntime(29097): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
09-26 17:22:38.350: E/AndroidRuntime(29097): at android.app.ActivityThread.access$600(ActivityThread.java:132)
09-26 17:22:38.350: E/AndroidRuntime(29097): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
09-26 17:22:38.350: E/AndroidRuntime(29097): at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 17:22:38.350: E/AndroidRuntime(29097): at android.os.Looper.loop(Looper.java:137)
09-26 17:22:38.350: E/AndroidRuntime(29097): at android.app.ActivityThread.main(ActivityThread.java:4575)
09-26 17:22:38.350: E/AndroidRuntime(29097): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 17:22:38.350: E/AndroidRuntime(29097): at java.lang.reflect.Method.invoke(Method.java:511)
09-26 17:22:38.350: E/AndroidRuntime(29097): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
09-26 17:22:38.350: E/AndroidRuntime(29097): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
09-26 17:22:38.350: E/AndroidRuntime(29097): at dalvik.system.NativeStart.main(Native Method)
09-26 17:22:38.350: E/AndroidRuntime(29097): Caused by: java.lang.NullPointerException
09-26 17:22:38.350: E/AndroidRuntime(29097): at com.example.gpacalculator.SecondActivity.calculateGPA(SecondActivity.java:66)
09-26 17:22:38.350: E/AndroidRuntime(29097): at com.example.gpacalculator.SecondActivity.onCreate(SecondActivity.java:46)
09-26 17:22:38.350: E/AndroidRuntime(29097): at android.app.Activity.performCreate(Activity.java:4465)
09-26 17:22:38.350: E/AndroidRuntime(29097): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-26 17:22:38.350: E/AndroidRuntime(29097): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
09-26 17:22:38.350: E/AndroidRuntime(29097): ... 11 more
答案 0 :(得分:0)
在
for (int i=0; i < 1; i++)
{
grade = secondBundle.getString("grade_"+i);
grades[i]=grade;
hour = secondBundle.getInt("hourse_"+i,3);
hours[i] =hour;
}
等级[1]未初始化,
在第66行,等级[1]为空。
尝试将第35行更改为(int i = 0; i&lt; grades.length; i ++)