不幸停止了,可能是由于app中的错误

时间:2015-07-02 09:39:33

标签: java android xml

我编写代码并运行调试器后得到了:

  

"不幸的是App停止了#34;

在我做出一些改变之前它正在工作..然后它不起作用 而且我不知道为什么!

我的程序是GPA计算器

MainActivity.java

package com.mainuser.gpa.gpa;

import android.content.ContentResolver;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {
    Button mcalculate;
    TextView mshowResultTV;
    private EditText meditText1;
    private EditText meditText2;
    private EditText meditText3;
    private EditText meditText4;
    private EditText meditText5;
    private EditText meditText6;
    private EditText meditText7;
    private EditText meditText8;
    private EditText meditText9;
    private EditText meditText10;
    private EditText meditText11;
    private EditText meditText12;
    double  gradnum1,gradnum2, gradnum3, gradnum4, gradnum5,
            gradnum6, hournum1,hournum2,hournum3,hournum4,hournum5, hournum6 , pointsum=0, hoursum=0;
    double x, y, z;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initControls();
    }

    private void initControls() {

        meditText1 = (EditText)findViewById(R.id.editText1);
        meditText2 = (EditText)findViewById(R.id.editText2);
        meditText3 = (EditText)findViewById(R.id.editText3);
        meditText4 = (EditText)findViewById(R.id.editText4);
        meditText5 = (EditText)findViewById(R.id.editText5);
        meditText6 = (EditText)findViewById(R.id.editText6);
        meditText7 = (EditText)findViewById(R.id.editText7);
        meditText8 = (EditText)findViewById(R.id.editText8);
        meditText9 = (EditText)findViewById(R.id.editText9);
        meditText10 = (EditText)findViewById(R.id.editText10);
        meditText11 = (EditText)findViewById(R.id.editText11);
        meditText12 = (EditText)findViewById(R.id.editText12);
        mshowResultTV = (TextView) findViewById(R.id.showResultTV);
        mcalculate = (Button) findViewById(R.id.calculate);

        mcalculate.setOnClickListener(new Button.OnClickListener(){
            public void onClick (View v)
            {  GPAcalculate(); }});

    }


        private void GPAcalculate()
    {
        x = CalculatePoints();
        y = CalculateHours();
        z = x/y;
        mshowResultTV.setText(Double.toString(z));
    }

    private double CalculatePoints() { // multiplying each grade by hour the add to pointsum

        gradnum1 = Double.parseDouble(meditText1.getText().toString());

        if (isEmpty(meditText3)== true){ // if  the edittext empty set it to zero
            meditText3.setText("0");
            gradnum2 = Double.parseDouble(meditText3.getText().toString());
        }
        else  gradnum2 = Double.parseDouble(meditText3.getText().toString());


        if (isEmpty(meditText5)== true){
            meditText5.setText("0");
            gradnum3 = Double.parseDouble(meditText5.getText().toString());
        }
        else  gradnum3 = Double.parseDouble(meditText5.getText().toString());


        if (isEmpty(meditText7)== true){
            meditText7.setText("0");
            gradnum4 = Double.parseDouble(meditText7.getText().toString());
        }
        else  gradnum4 = Double.parseDouble(meditText7.getText().toString());


        if (isEmpty(meditText9)== true){
            meditText9.setText("0");
            gradnum5 = Double.parseDouble(meditText9.getText().toString());
        }
        else  gradnum5 = Double.parseDouble(meditText9.getText().toString());


        if (isEmpty(meditText11)== true){
            meditText11.setText("0");
            gradnum6 = Double.parseDouble(meditText11.getText().toString());
        }
        else  gradnum6 = Double.parseDouble(meditText11.getText().toString());

        hournum1 = Double.parseDouble(meditText2.getText().toString());
        hournum2 = Double.parseDouble(meditText4.getText().toString());
        hournum3  = Double.parseDouble(meditText6.getText().toString());
        hournum4  = Double.parseDouble(meditText8.getText().toString());
        hournum5  = Double.parseDouble(meditText10.getText().toString());

        if (isEmpty(meditText12)== true){
            meditText12.setText("0");
            hournum6  = Double.parseDouble(meditText12.getText().toString());
        }
        else   hournum6  = Double.parseDouble(meditText12.getText().toString());


        pointsum = (gradnum1*hournum1) + (gradnum2*hournum2) + (gradnum3*hournum3) + (gradnum4*hournum4) + (gradnum5*hournum5) + (gradnum6*hournum6);

        return pointsum;
    }


    private double CalculateHours() { // adding numb of hours

        hournum1 = Double.parseDouble(meditText2.getText().toString());
        hournum2 = Double.parseDouble(meditText4.getText().toString());
        hournum3  = Double.parseDouble(meditText6.getText().toString());
        hournum4  = Double.parseDouble(meditText8.getText().toString());
        hournum5  = Double.parseDouble(meditText10.getText().toString());


        if (isEmpty(meditText12)== true){
            meditText12.setText("0");
            hournum6  = Double.parseDouble(meditText12.getText().toString());
        }
        else   hournum6  = Double.parseDouble(meditText12.getText().toString());

        hoursum = hournum1 + hournum2 + hournum3 + hournum4 + hournum5 + hournum6;
        return hoursum;
    }

    private boolean isEmpty ( EditText x ) { // fuction to check if a certain edit text is empty or not
        return
                x.getText().toString().trim().length() == 0; }



}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mainuser.gpa.gpa" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon3"
        android:label="GPA Calculater"
        android:theme = "@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="Calculater"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

    </application>

</manifest>

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:background="@android:color/holo_blue_bright">

    <TextView
        android:text="Please insert the following .. "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:id="@+id/viewhello"/>
    <Button
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:layout_below="@+id/viewhello"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="70dp"
        android:id="@+id/Sub1"
        android:background="@android:color/white"
        android:text="Subject 1"
        android:textColor="@android:color/holo_blue_dark"/>
    <Button
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:layout_below="@+id/Sub1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp"
        android:id="@+id/Sub2"
        android:background="@android:color/white"
        android:text="Subject 2"
        android:textColor="@android:color/holo_blue_dark"/>
    <Button
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:layout_below="@+id/Sub2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp"
        android:id="@+id/Sub3"
        android:background="@android:color/white"
        android:text="Subject 3"
        android:textColor="@android:color/holo_blue_dark"/>
    <Button
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:layout_below="@+id/Sub3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp"
        android:id="@+id/Sub4"
        android:background="@android:color/white"
        android:text="Subject 4"
        android:textColor="@android:color/holo_blue_dark"/>
    <Button
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:layout_below="@+id/Sub4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp"
        android:id="@+id/Sub5"
        android:background="@android:color/white"
        android:text="Subject 5"
        android:textColor="@android:color/holo_blue_dark"/>
    <Button
    android:layout_width="90dp"
    android:layout_height="30dp"
    android:layout_below="@+id/Sub5"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="10dp"
    android:id="@+id/Sub6"
    android:background="@android:color/white"
    android:text="Subject 6"
    android:textColor="@android:color/holo_blue_dark"
    />

    <Button
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:id="@+id/calculate"
        android:background="@android:color/white"
        android:text="Calculate"
        android:textColor="@android:color/holo_blue_dark"
        android:layout_below="@+id/editText11"
        android:layout_marginTop="30dp"
        android:layout_alignLeft="@+id/editText11"
        android:layout_alignStart="@+id/editText11"
        />

    <EditText
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText1"
        android:layout_alignTop="@+id/Sub1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:textSize="12sp"
        android:textColor="@android:color/black"

        android:selectAllOnFocus="true"/>
    <EditText
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText2"
        android:layout_alignTop="@+id/editText1"
        android:layout_toRightOf="@+id/editText1"
        android:layout_toEndOf="@+id/editText1"
        android:textSize="12sp"
        android:textColor="@android:color/black"

        android:selectAllOnFocus="true"/>

       <EditText
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText3"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:textSize="12sp"
        android:textColor="@android:color/black"

        android:selectAllOnFocus="true"

        />

    <EditText
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText4"
        android:layout_alignTop="@+id/editText3"
        android:layout_alignLeft="@+id/editText2"
        android:layout_alignStart="@+id/editText2"
        android:textSize="12sp"
        android:textColor="@android:color/black"

        android:selectAllOnFocus="true"/>


    <EditText
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText5"
        android:layout_below="@+id/editText3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:textSize="12sp"
        android:textColor="@android:color/black"

        android:selectAllOnFocus="true"
        />
    <EditText
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText6"
        android:layout_alignTop="@+id/editText5"
        android:layout_toRightOf="@+id/editText5"
        android:layout_toEndOf="@+id/editText1"
        android:textSize="12sp"
        android:textColor="@android:color/black"
        android:selectAllOnFocus="true"/>

    <EditText
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText7"
        android:layout_below="@+id/editText5"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:textSize="12sp"
        android:textColor="@android:color/black"
        android:selectAllOnFocus="true"
        />
    <EditText
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText8"
        android:layout_alignTop="@+id/editText7"
        android:layout_toRightOf="@+id/editText7"
        android:layout_toEndOf="@+id/editText1"
        android:textSize="12sp"
        android:textColor="@android:color/black"
        android:selectAllOnFocus="true"/>

    <EditText
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText9"
        android:layout_below="@+id/editText7"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:textSize="12sp"
        android:textColor="@android:color/black"
        android:selectAllOnFocus="true"/>
    <EditText
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText10"
        android:layout_alignTop="@+id/editText9"
        android:layout_toRightOf="@+id/editText9"
        android:layout_toEndOf="@+id/editText1"
        android:textSize="12sp"
        android:textColor="@android:color/black"
        android:selectAllOnFocus="true"/>

    <EditText
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText11"
        android:layout_below="@+id/editText9"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:textSize="12sp"
        android:textColor="@android:color/black"

        android:selectAllOnFocus="true"
        />
    <EditText
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText12"
        android:layout_alignTop="@+id/editText11"
        android:layout_toRightOf="@+id/editText11"
        android:layout_toEndOf="@+id/editText1"
        android:textSize="12sp"
        android:textColor="@android:color/black"

        android:selectAllOnFocus="true"

        />
    <Button
    android:layout_width="90dp"
    android:layout_height="30dp"
    android:id="@+id/GPA"
    android:background="@android:color/white"
    android:text="SEM GPA"
    android:textColor="@android:color/holo_blue_dark"
    android:layout_below="@+id/calculate"
    android:layout_alignLeft="@+id/editText11"
    android:layout_alignStart="@+id/editText11"
    android:layout_marginTop="30dp" />

    <Button
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:id="@+id/Hours"
        android:background="@android:color/white"
        android:text="Hours"
        android:textColor="@android:color/holo_blue_dark"
        android:layout_above="@+id/editText1"
        android:layout_toRightOf="@+id/editText1"
        android:layout_toEndOf="@+id/editText1" />

    <Button
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:id="@+id/Grade"
        android:background="@android:color/white"
        android:text="Grade"
        android:textColor="@android:color/holo_blue_dark"
        android:layout_alignTop="@+id/Hours"
        android:layout_toLeftOf="@+id/Hours"
        android:layout_toStartOf="@+id/Hours" />
    <TextView
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:ems="10"
        android:id="@+id/showResultTV"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        android:layout_alignTop="@+id/GPA"
        android:layout_alignLeft="@+id/editText12"
        android:layout_alignStart="@+id/editText12"
        android:layout_marginLeft="19dp"
        android:layout_marginStart="19dp"
        android:gravity="center_vertical" />




</RelativeLayout>

logcat的

07-02 07:27:09.970    1193-1193/com.mainuser.gpa.gpa W/ActivityThread﹕ Application com.mainuser.gpa.gpa is waiting for the debugger on port 8100...
07-02 07:27:10.020    1193-1193/com.mainuser.gpa.gpa I/System.out﹕ Sending WAIT chunk
07-02 07:27:14.140    1193-1199/com.mainuser.gpa.gpa I/dalvikvm﹕ Debugger is active
07-02 07:27:14.340    1193-1193/com.mainuser.gpa.gpa I/System.out﹕ Debugger has connected
07-02 07:27:14.340    1193-1193/com.mainuser.gpa.gpa I/System.out﹕ waiting for debugger to settle...
07-02 07:27:14.560    1193-1193/com.mainuser.gpa.gpa I/System.out﹕ waiting for debugger to settle...
07-02 07:27:14.780    1193-1193/com.mainuser.gpa.gpa I/System.out﹕ waiting for debugger to settle...
07-02 07:27:14.980    1193-1193/com.mainuser.gpa.gpa I/System.out﹕ waiting for debugger to settle...
07-02 07:27:15.180    1193-1193/com.mainuser.gpa.gpa I/System.out﹕ waiting for debugger to settle...
07-02 07:27:15.800    1193-1193/com.mainuser.gpa.gpa I/System.out﹕ waiting for debugger to settle...
07-02 07:27:16.000    1193-1193/com.mainuser.gpa.gpa I/System.out﹕ waiting for debugger to settle...
07-02 07:27:16.220    1193-1193/com.mainuser.gpa.gpa I/System.out﹕ waiting for debugger to settle...
07-02 07:27:17.840    1193-1193/com.mainuser.gpa.gpa I/System.out﹕ debugger has settled (1632)
07-02 07:27:20.890    1193-1193/com.mainuser.gpa.gpa D/AndroidRuntime﹕ Shutting down VM
07-02 07:27:20.890    1193-1193/com.mainuser.gpa.gpa W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1ae8ba8)
07-02 07:27:21.980    1193-1193/com.mainuser.gpa.gpa E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mainuser.gpa.gpa, PID: 1193
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mainuser.gpa.gpa/com.mainuser.gpa.gpa.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
            at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
            at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
            at com.mainuser.gpa.gpa.MainActivity.onCreate(MainActivity.java:36)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

错误logcat

07-02 07:27:21.980    1193-1193/com.mainuser.gpa.gpa E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mainuser.gpa.gpa, PID: 1193
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mainuser.gpa.gpa/com.mainuser.gpa.gpa.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
            at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
            at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
            at com.mainuser.gpa.gpa.MainActivity.onCreate(MainActivity.java:36)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

首先,请将您的方法命名为Java命名约定(从小写开始,'因为大写是针对类)。

然后你在EditText上的检查条件是否为空,应该是这样的:

{{1}}

答案 1 :(得分:0)

因为你使用新的谷歌方法,使用AppCompat主题,只需去Manifest,找到标记并放置一个有效的主题 - @android:style / Theme.AppCompat。