不幸的是,Simple应用已停止

时间:2017-04-03 05:47:27

标签: java xml android-studio

我是Android开发的新手,我正在制作一个简单的计算器应用程序...每当我在Android模拟器中运行代码时,它会向我显示不幸的是,SimpleApp已停止...我用Android Studio编写代码请帮帮我... 以下是我的代码......

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.myapp.simpleapp.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="337dp"
        android:layout_height="58dp"
        android:contextClickable="false"
        android:cursorVisible="false"
        android:fontFamily="serif"
        android:longClickable="true"
        android:text="Calculator"
        android:textAlignment="center"
        android:textAllCaps="true"
        android:textColor="@android:color/holo_red_dark"
        android:textSize="36sp"
        android:visibility="visible"
        tools:layout_editor_absoluteX="24dp"
        tools:layout_editor_absoluteY="31dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="194dp"
        android:layout_height="43dp"
        android:text="Number 1: "
        android:textColor="@android:color/black"
        android:textSize="25sp"
        tools:layout_editor_absoluteX="24dp"
        tools:layout_editor_absoluteY="140dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="194dp"
        android:layout_height="44dp"
        android:text="Number 2: "
        android:textColor="@android:color/black"
        android:textSize="25sp"
        tools:layout_editor_absoluteX="24dp"
        tools:layout_editor_absoluteY="217dp" />

    <EditText
        android:id="@+id/number2"
        android:layout_width="99dp"
        android:layout_height="43dp"
        android:ems="10"
        android:hint="Number 2"
        android:inputType="number|numberSigned|numberDecimal"
        android:singleLine="true"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textIsSelectable="true"
        android:textSize="20sp"
        tools:layout_editor_absoluteX="262dp"
        tools:layout_editor_absoluteY="217dp" />

    <EditText
        android:id="@+id/number1"
        android:layout_width="99dp"
        android:layout_height="43dp"
        android:ems="10"
        android:hint="Number 1"
        android:inputType="number|numberSigned|numberDecimal"
        android:linksClickable="false"
        android:longClickable="true"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="20sp"
        tools:layout_editor_absoluteX="262dp"
        tools:layout_editor_absoluteY="140dp" />

    <TextView
        android:id="@+id/result"
        android:layout_width="175dp"
        android:layout_height="40dp"
        tools:layout_editor_absoluteX="176dp"
        tools:layout_editor_absoluteY="430dp" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="103dp"
        android:layout_height="40dp"
        android:text="Result: "
        android:textColor="@android:color/black"
        android:textSize="25sp"
        tools:layout_editor_absoluteX="59dp"
        tools:layout_editor_absoluteY="433dp" />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="152dp"
        android:layout_height="49dp"
        android:text="+"
        android:textColor="@android:color/black"
        android:textSize="30sp"
        tools:layout_editor_absoluteX="24dp"
        tools:layout_editor_absoluteY="292dp" />

    <Button
        android:id="@+id/btnSub"
        android:layout_width="152dp"
        android:layout_height="49dp"
        android:text="-"
        android:textColor="@android:color/black"
        android:textIsSelectable="false"
        android:textSize="30sp"
        tools:layout_editor_absoluteX="216dp"
        tools:layout_editor_absoluteY="292dp" />

    <Button
        android:id="@+id/btnDiv"
        android:layout_width="152dp"
        android:layout_height="49dp"
        android:text="/"
        android:textColor="@android:color/black"
        android:textSize="25sp"
        tools:layout_editor_absoluteX="24dp"
        tools:layout_editor_absoluteY="362dp" />

    <Button
        android:id="@+id/btnMul"
        android:layout_width="152dp"
        android:layout_height="49dp"
        android:text="*"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="25sp"
        tools:layout_editor_absoluteX="216dp"
        tools:layout_editor_absoluteY="362dp" />
</android.support.constraint.ConstraintLayout>

MainActivity.java

package com.myapp.simpleapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText number1= (EditText) findViewById(R.id.number1);
    EditText number2= (EditText) findViewById(R.id.number2);

    Button add= (Button) findViewById(R.id.btnAdd);
    Button sub= (Button) findViewById(R.id.btnSub);
    Button div= (Button) findViewById(R.id.btnDiv);
    Button mul= (Button) findViewById(R.id.btnMul);


    TextView txtresult= (TextView) findViewById(R.id.result);

    double num1,num2,result;

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

        num1=Double.parseDouble(number1.getText().toString());
        num2=Double.parseDouble(number2.getText().toString());

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result=num1+num2;

                txtresult.setText(Double.toString(result));
            }
        });

        sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result=num1-num2;

                txtresult.setText(Double.toString(result));
            }
        });

        div.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result=num1/num2;

                txtresult.setText(Double.toString(result));
            }
        });

        mul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result=num1*num2;

                txtresult.setText(Double.toString(result));
            }
        });

    }
}

logcat的

04-03 12:25:38.913 3927-3927/? E/memtrack: Couldn't load memtrack module (No such file or directory)
04-03 12:25:38.913 3927-3927/? E/android.os.Debug: failed to load memtrack module: -2
04-03 12:25:39.372 3952-3952/? E/memtrack: Couldn't load memtrack module (No such file or directory)
04-03 12:25:39.373 3952-3952/? E/android.os.Debug: failed to load memtrack module: -2
04-03 12:25:49.078 4097-4097/? E/memtrack: Couldn't load memtrack module (No such file or directory)
04-03 12:25:49.078 4097-4097/? E/android.os.Debug: failed to load memtrack module: -2
04-03 12:25:49.647 4122-4122/? E/memtrack: Couldn't load memtrack module (No such file or directory)
04-03 12:25:49.648 4122-4122/? E/android.os.Debug: failed to load memtrack module: -2
04-03 12:25:58.059 3666-3666/com.myapp.simpleapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: com.myapp.simpleapp, PID: 3666
                                                                   java.lang.NumberFormatException: Invalid double: ""
                                                                       at java.lang.StringToReal.invalidReal(StringToReal.java:63)
                                                                       at java.lang.StringToReal.parseDouble(StringToReal.java:267)
                                                                       at java.lang.Double.parseDouble(Double.java:301)
                                                                       at com.myapp.simpleapp.MainActivity$4.onClick(MainActivity.java:70)
                                                                       at android.view.View.performClick(View.java:4780)
                                                                       at android.view.View$PerformClick.run(View.java:19866)
                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:135)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-03 12:25:58.079 1163-1163/? E/EGL_emulation: tid 1163: eglCreateSyncKHR(1299): error 0x3004 (EGL_BAD_ATTRIBUTE)
04-03 12:26:02.452 1810-2202/system_process E/InputDispatcher: channel '21dd8534 com.myapp.simpleapp/com.myapp.simpleapp.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
04-03 12:26:02.581 1175-1175/? E/audio_hw_generic: Error opening input stream format 1, channel_mask 0010, sample_rate 16000

2 个答案:

答案 0 :(得分:0)

在onCreate中使用findViewById,只有在edittexts中输入值后才使用Double.parse。

试试这段代码:

System.getenv("EXTERNAL_STORAGE")

答案 1 :(得分:0)

此问题的最常见原因是运行时出现 NullPointerException

要遵循的步骤:

  

观察Android Studio上的堆栈跟踪,如下所示:enter image description here

然后,您可以分析堆栈跟踪,并确定导致异常的原因以及导致异常的行。或者,您可以发布堆栈跟踪的屏幕截图,以便更容易回答问题。