修复Android:致命异常主要

时间:2014-05-24 16:48:49

标签: android

我是Android开发的初学者。这是我第一次在stackoverflow上发帖,所以如果我犯了任何noob错误,请原谅我。我已经完成了我的第一个应用程序的部分版本。我只是想在Android设备上运行它,看看它是否正常工作。我的代码中没有错误。我得到了致命的例外:主要是离开。

但是,功能无法正常工作。我不知道问题是什么。我想在单击按钮时在CE TextView中显示CE1。编写的代码没有这样做。

这是MainActivity.java:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText course1 = (EditText)findViewById(R.id.Course1);
    final EditText credit1 = (EditText)findViewById(R.id.editText7);
    final EditText grade1 = (EditText)findViewById(R.id.editText13);
    final Button b = (Button)findViewById(R.id.button1);
    final TextView ce = (TextView)findViewById(R.id.textView5);
    String grade_1 = grade1.getText().toString();
    double credit_1 = Double.parseDouble(credit1.getText().toString());
    double CE1=0;
    if (grade_1 == "A")
        CE1 = 4*credit_1;
    final String CE1s = Double.toString(CE1);
    b.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
        ce.setText(CE1s);
        }

    });
    }


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }


    }
}

这是AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rutgersgpacalculator"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.rutgersgpacalculator.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

这是logcat

05-24 12:41:55.513: E/AndroidRuntime(24785): FATAL EXCEPTION: main
05-24 12:41:55.513: E/AndroidRuntime(24785): Process: com.example.rutgersgpacalculator, PID: 24785
05-24 12:41:55.513: E/AndroidRuntime(24785): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rutgersgpacalculator/com.example.rutgersgpacalculator.MainActivity}: java.lang.NumberFormatException: Invalid double: ""
05-24 12:41:55.513: E/AndroidRuntime(24785):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
05-24 12:41:55.513: E/AndroidRuntime(24785):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
05-24 12:41:55.513: E/AndroidRuntime(24785):    at android.app.ActivityThread.access$900(ActivityThread.java:161)
05-24 12:41:55.513: E/AndroidRuntime(24785):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
05-24 12:41:55.513: E/AndroidRuntime(24785):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-24 12:41:55.513: E/AndroidRuntime(24785):    at android.os.Looper.loop(Looper.java:157)
05-24 12:41:55.513: E/AndroidRuntime(24785):    at android.app.ActivityThread.main(ActivityThread.java:5356)
05-24 12:41:55.513: E/AndroidRuntime(24785):    at java.lang.reflect.Method.invokeNative(Native Method)
05-24 12:41:55.513: E/AndroidRuntime(24785):    at java.lang.reflect.Method.invoke(Method.java:515)
05-24 12:41:55.513: E/AndroidRuntime(24785):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
05-24 12:41:55.513: E/AndroidRuntime(24785):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
05-24 12:41:55.513: E/AndroidRuntime(24785):    at dalvik.system.NativeStart.main(Native Method)
05-24 12:41:55.513: E/AndroidRuntime(24785): Caused by: java.lang.NumberFormatException: Invalid double: ""
05-24 12:41:55.513: E/AndroidRuntime(24785):    at java.lang.StringToReal.invalidReal(StringToReal.java:63)
05-24 12:41:55.513: E/AndroidRuntime(24785):    at java.lang.StringToReal.parseDouble(StringToReal.java:248)
05-24 12:41:55.513: E/AndroidRuntime(24785):    at java.lang.Double.parseDouble(Double.java:295)
05-24 12:41:55.513: E/AndroidRuntime(24785):    at com.example.rutgersgpacalculator.MainActivity.onCreate(MainActivity.java:31)
05-24 12:41:55.513: E/AndroidRuntime(24785):    at android.app.Activity.performCreate(Activity.java:5426)
05-24 12:41:55.513: E/AndroidRuntime(24785):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
05-24 12:41:55.513: E/AndroidRuntime(24785):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
05-24 12:41:55.513: E/AndroidRuntime(24785):    ... 11 more

3 个答案:

答案 0 :(得分:2)

错误在堆栈跟踪中很明显。您正在尝试将String解析为双精度版并且失败,因为要解析的String""。这可能是因为你试图解析一个空的`EditText。

答案 1 :(得分:1)

credit1在onCreate中为空,因此对parseDouble的调用会传递一个空字符串,它不喜欢它。首先使用TextUtils.isEmpty()检查文本是否为空,然后再解析它。

String text = credit1.getText().toString();
if (!TextUtils.isEmpty(text)) {
    // call parseDouble in here
}

答案 2 :(得分:0)

如果您想比较字符串,则必须使用if (grade_1.equals ("A"))代替if (grade_1 == 1)