有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android代码错误。应用程序意外停止

我的代码有问题。每当我运行它时,它都会显示错误application stopped unexpectedly。我认为这个问题是由Java类中的一些错误引起的。到底是什么导致了这个问题

这是我的XML文件:

<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:orientation="vertical"
安卓:layout_width="match_parent"
安卓:padding="10dp"
安卓:overScrollMode="always"
安卓:layout_height="match_parent"
安卓:background="#A52A2A"
tools:context="com.safeermalik.gpa.Start">
<TextView
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:textStyle="bold"
    安卓:textSize="18sp"
    安卓:text="Subject :"/>
<EditText
    安卓:layout_width="fill_parent"
    安卓:layout_height="wrap_content"
    安卓:hint="Enter Subject Name"
    安卓:id="@+id/subject"/>
<TextView
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:textStyle="bold"
    安卓:textSize="18sp"
    安卓:text="Marks :"/>
<EditText
    安卓:layout_width="fill_parent"
    安卓:layout_height="wrap_content"
    安卓:hint="Enter Subject Marks"
    安卓:id="@+id/marks"/>
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textStyle="bold"
安卓:textSize="18sp"
安卓:text="Credits :"/>
<EditText
    安卓:layout_width="fill_parent"
    安卓:layout_height="wrap_content"
    安卓:hint="Enter Subject Credits"
    安卓:id="@+id/credits"/>
<RadioGroup xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="fill_parent"
    安卓:layout_height="wrap_content"
    安卓:orientation="horizontal"
    安卓:padding="5dp">
    <TextView
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:textStyle="bold"
        安卓:textSize="18sp"
        安卓:text="Criteria :"/>
    <RadioButton 安卓:id="@+id/radio_old"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:text="Old Criteria"
        安卓:onClick="onRadioButtonClicked"/>
    <RadioButton 安卓:id="@+id/radio_new"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:text="New Criteria"
        安卓:onClick="onRadioButtonClicked"/>
</RadioGroup>
<Button
    安卓:padding="5dp"
    安卓:layout_width="fill_parent"
    安卓:layout_height="wrap_content"
    安卓:text="Add course"
    安卓:background="#696969"
    安卓:id="@+id/add"
    安卓:onClick="sendMessage"/>
<TableLayout
    安卓:layout_width="fill_parent"
    安卓:layout_height="wrap_content"
    安卓:padding="10dp"
    安卓:id="@+id/add_table"
   >
    <TableRow
        安卓:layout_height="wrap_content"
        安卓:layout_width="wrap_content"
        安卓:layout_gravity="center">
        <TextView
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:background="@drawable/border"
            安卓:textStyle="bold"
            安卓:textSize="18sp"
            安卓:text="Subject "/>
        <TextView
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:background="@drawable/border"
            安卓:textStyle="bold"
            安卓:textSize="18sp"
            安卓:text="Marks "/>
        <TextView
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:background="@drawable/border"
            安卓:textStyle="bold"
            安卓:textSize="18sp"
            安卓:text="Credits "/>
        <TextView
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:background="@drawable/border"
            安卓:textStyle="bold"
            安卓:textSize="18sp"
            安卓:text="Percentage "/>
        <TextView
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:background="@drawable/border"
            安卓:textStyle="bold"
            安卓:textSize="18sp"
            安卓:text="GPA "/>
    </TableRow>
   </TableLayout>
<TableLayout
    安卓:layout_width="fill_parent"
    安卓:layout_height="wrap_content"
    安卓:padding="10dp"
    >
    <TableRow
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:orientation="horizontal"
        >
        <TextView
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:textStyle="bold"
            安卓:textSize="18sp"
            安卓:text="Total Marks : "/>
        <EditText
            安卓:layout_width="fill_parent"
            安卓:layout_height="wrap_content"
            安卓:id="@+id/total_marks"
            安卓:hint="Total Marks "/>
    </TableRow>
    <TableRow>
    <TextView
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:textStyle="bold"
        安卓:textSize="18sp"
        安卓:text="S-GPA : "/>
    <EditText
        安卓:layout_width="fill_parent"
        安卓:layout_height="wrap_content"
        安卓:id="@+id/s_gpa"
        安卓:hint="GPA "/>
</TableRow>
    <TableRow>
        <TextView
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:textStyle="bold"
            安卓:textSize="18sp"
            安卓:text="PERCENTAGE : "/>
        <EditText
            安卓:layout_width="fill_parent"
            安卓:layout_height="wrap_content"
            安卓:id="@+id/final_percentage"
            安卓: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);
    }
}

共 (1) 个答案

  1. # 1 楼答案

    这里有一个逻辑错误和一个运行时错误。 为什么要在onCreate()方法中解析EditText的内容

    只需删除这两行:

        //a = Integer.parseInt(String.valueOf(mark));
        //b = Integer.parseInt(String.valueOf(credit));
    

    首先,您不需要在onCreate方法中解析它们。您可以在按钮的onClick()方法中解析内容。 这是逻辑错误。 第二:

    您的运行时错误是由于您试图获取valueOfEditText而不是字符串。因此mark应替换为mark.getText().toString(),而credit应替换为credit.getText().toString()