我的代码出了问题。每当我运行它时,它都会显示错误错误application stopped unexpectedly
。我认为这个问题是由Java类中的一些错误引起的。究竟是什么导致了这个问题?
这是我的XML文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="10dp"
android:overScrollMode="always"
android:layout_height="match_parent"
android:background="#A52A2A"
tools:context="com.safeermalik.gpa.Start">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Subject :"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Subject Name"
android:id="@+id/subject"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Marks :"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Subject Marks"
android:id="@+id/marks"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Credits :"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Subject Credits"
android:id="@+id/credits"/>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Criteria :"/>
<RadioButton android:id="@+id/radio_old"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Old Criteria"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Criteria"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
<Button
android:padding="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add course"
android:background="#696969"
android:id="@+id/add"
android:onClick="sendMessage"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="@+id/add_table"
>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="Subject "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="Marks "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="Credits "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="Percentage "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="GPA "/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Total Marks : "/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/total_marks"
android:hint="Total Marks "/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="S-GPA : "/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/s_gpa"
android:hint="GPA "/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="PERCENTAGE : "/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/final_percentage"
android:hint="Percentage "/>
</TableRow>
</TableLayout>
</LinearLayout>
这是Java类:
public class Start extends ActionBarActivity {
EditText sub;
EditText mark;
EditText credit;
Button Adds;
int a=0,b=0,hour=0,x=0,counter=0;
String grade="";
String subject="";
EditText SUM,percent,GPA;
double gpa,gpa_multiple;
TableLayout table;
CalcGPA cal=new CalcGPA();
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radio_new:
if (checked)
x=1;
break;
case R.id.radio_old:
if (checked)
x=0;
break;
} }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
sub=(EditText)findViewById(R.id.subject);
subject=sub.getText().toString();
mark=(EditText)findViewById(R.id.marks);
a=Integer.parseInt(String.valueOf(mark));
credit=(EditText)findViewById(R.id.credits);
b=Integer.parseInt(String.valueOf(credit));
Adds=(Button)findViewById(R.id.add);
table=(TableLayout)findViewById(R.id.add_table);
SUM=(EditText)findViewById(R.id.total_marks);
GPA=(EditText)findViewById(R.id.s_gpa);
percent=(EditText)findViewById(R.id.final_percentage);
Adds.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sub.setText("");
mark.setText("");
credit.setText("");
counter++;
hour +=b;
if(x==0){
gpa=cal.subgpa(a);
grade=cal.Grade(a);
}
else if(x==1){
gpa=cal.ChangedGpa(a);
grade=cal.ChangedGrade(a);
}
add_row(subject,a,b,gpa,grade);
gpa_multiple+=(gpa*b);
SUM.setText(""+(counter*a));
GPA.setText(""+(gpa_multiple/hour));
percent.setText(""+((counter*a)/(counter*100)*100));
}
} );
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.start, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void add_row(String sub, int crdt, int marks, double sgpa, String subgrade ){
TableLayout tb = (TableLayout) findViewById(R.id.add_table);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView subject_name=new TextView(this);
subject_name.setText(counter+""+sub);
subject_name.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
subject_name.setBackgroundResource(R.drawable.border);
TextView subject_marks=new TextView(this);
subject_marks.setText(""+marks);
subject_marks.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
subject_marks.setBackgroundResource(R.drawable.border);
TextView subject_credits=new TextView(this);
subject_credits.setText(""+crdt);
subject_credits.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
subject_credits.setBackgroundResource(R.drawable.border);
TextView subject_gpa=new TextView(this);
subject_gpa.setText(""+sgpa);
subject_gpa.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
subject_gpa.setBackgroundResource(R.drawable.border);
TextView subject_grade=new TextView(this);
subject_grade.setText(""+subgrade);
subject_grade.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
subject_grade.setBackgroundResource(R.drawable.border);
}
}
答案 0 :(得分:0)
这里有逻辑错误和运行时错误。
为什么要在EditText
()方法中解析onCreate
的内容?
删除这两行:
//a = Integer.parseInt(String.valueOf(mark));
//b = Integer.parseInt(String.valueOf(credit));
首先,您不需要在onCreate
方法中解析它们。您解析Button的onClick
()方法中的内容。
这是逻辑错误。
第二:
您的运行时错误是由于您尝试获取valueOf
EditText
而不是字符串。所以mark
应由mark.getText().toString()
和credit
替换为credit.getText().toString()