按下计算按钮时崩溃

问题描述 投票:-1回答:1

我已经完成了我的第一个应用程序的部分版本。我只想在Android设备上运行它,看看它是否可以正常工作。我的代码没有错误。我遇到了致命异常:主要故障消失。

但是,该功能无法正常运行。我不知道是什么问题。当我按下按钮时,应用崩溃。

MainActivity.java

package com.example.crickattemp;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
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 e1;
TextView t1;
Button b1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e1=(EditText)findViewById(R.id.data);
        t1=(TextView) findViewById(R.id.result);
        b1=(Button)findViewById(R.id.btn1);
        b1.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View view) {
               Float f1=Float.parseFloat(e1.toString());
               Float f2=f1+25;
                t1.setText(Float.toString(f2));
            }
        });

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <EditText
        android:id="@+id/data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/chirps_per_minute"
        android:inputType="number"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.454"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.389"
        android:importantForAutofill="no" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Calculate"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.453"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.531" />

    <TextView
        android:id="@+id/result"
        android:layout_width="214dp"
        android:layout_height="37dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.456"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.673" />
</androidx.constraintlayout.widget.ConstraintLayout>

LogCat输出:

2020-03-11 12:34:38.067 16113-16113/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2020-03-11 12:34:38.070 16113-16113/? E/Zygote: accessInfo : 1
2020-03-11 12:34:38.091 16113-16113/? I/ple.crickattem: Late-enabling -Xcheck:jni
2020-03-11 12:34:38.808 16113-16113/com.example.crickattemp W/ple.crickattem: Accessing hidden method Landroid/graphics/drawable/Drawable;->getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking)
2020-03-11 12:34:38.808 16113-16113/com.example.crickattemp W/ple.crickattem: Accessing hidden field Landroid/graphics/Insets;->left:I (light greylist, linking)
2020-03-11 12:34:38.808 16113-16113/com.example.crickattemp W/ple.crickattem: Accessing hidden field Landroid/graphics/Insets;->right:I (light greylist, linking)
2020-03-11 12:34:38.808 16113-16113/com.example.crickattemp W/ple.crickattem: Accessing hidden field Landroid/graphics/Insets;->top:I (light greylist, linking)
2020-03-11 12:34:38.808 16113-16113/com.example.crickattemp W/ple.crickattem: Accessing hidden field Landroid/graphics/Insets;->bottom:I (light greylist, linking)
2020-03-11 12:34:38.853 16113-16113/com.example.crickattemp I/DecorView: mWindow.mActivityCurrentConfig is null.
2020-03-11 12:34:38.865 16113-16113/com.example.crickattemp I/DecorView: createDecorCaptionView >> DecorView@923ef8a[], isFloating: false, isApplication: true, hasWindowDecorCaption: false, hasWindowControllerCallback: true
2020-03-11 12:34:39.177 16113-16113/com.example.crickattemp W/ple.crickattem: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2020-03-11 12:34:39.181 16113-16113/com.example.crickattemp W/ple.crickattem: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2020-03-11 12:34:39.232 16113-16113/com.example.crickattemp W/ple.crickattem: Accessing hidden method Landroid/widget/TextView;->getTextDirectionHeuristic()Landroid/text/TextDirectionHeuristic; (light greylist, linking)
2020-03-11 12:34:39.540 16113-16113/com.example.crickattemp D/OpenGLRenderer: Skia GL Pipeline
2020-03-11 12:34:39.554 16113-16113/com.example.crickattemp D/EmergencyMode: [EmergencyManager] android createPackageContext successful
2020-03-11 12:34:39.583 16113-16113/com.example.crickattemp D/InputTransport: Input channel constructed: fd=62
2020-03-11 12:34:39.584 16113-16113/com.example.crickattemp D/ViewRootImpl@60a44a2[MainActivity]: setView = DecorView@923ef8a[MainActivity] TM=true MM=false
2020-03-11 12:34:39.740 16113-16113/com.example.crickattemp D/ViewRootImpl@60a44a2[MainActivity]: Relayout returned: old=[0,0][720,1480] new=[0,0][720,1480] result=0x7 surface={true 3577739264} changed=true
2020-03-11 12:34:39.763 16113-16259/com.example.crickattemp I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2020-03-11 12:34:39.764 16113-16259/com.example.crickattemp I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2020-03-11 12:34:39.764 16113-16259/com.example.crickattemp I/OpenGLRenderer: Initialized EGL, version 1.4
2020-03-11 12:34:39.764 16113-16259/com.example.crickattemp D/OpenGLRenderer: Swap behavior 2
2020-03-11 12:34:39.823 16113-16259/com.example.crickattemp D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000,  [720x1480]-format:1
2020-03-11 12:34:39.823 16113-16259/com.example.crickattemp D/OpenGLRenderer: eglCreateWindowSurface = 0xe85fb9a0, 0xd53ff808
2020-03-11 12:34:40.077 16113-16113/com.example.crickattemp D/ViewRootImpl@60a44a2[MainActivity]: MSG_RESIZED: frame=[0,0][720,1480] ci=[0,48][0,96] vi=[0,48][0,96] or=1
2020-03-11 12:34:40.078 16113-16113/com.example.crickattemp D/ViewRootImpl@60a44a2[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1 1
2020-03-11 12:34:40.102 16113-16113/com.example.crickattemp D/InputMethodManager: prepareNavigationBarInfo() DecorView@923ef8a[MainActivity]
2020-03-11 12:34:40.102 16113-16113/com.example.crickattemp D/InputMethodManager: getNavigationBarColor() -855310
2020-03-11 12:34:40.122 16113-16113/com.example.crickattemp D/InputMethodManager: prepareNavigationBarInfo() DecorView@923ef8a[MainActivity]
2020-03-11 12:34:40.122 16113-16113/com.example.crickattemp D/InputMethodManager: getNavigationBarColor() -855310
2020-03-11 12:34:40.124 16113-16113/com.example.crickattemp V/InputMethodManager: Starting input: tba=com.example.crickattemp ic=null mNaviBarColor -855310 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
2020-03-11 12:34:40.124 16113-16113/com.example.crickattemp D/InputMethodManager: startInputInner - Id : 0
2020-03-11 12:34:40.124 16113-16113/com.example.crickattemp I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
2020-03-11 12:34:40.131 16113-16134/com.example.crickattemp D/InputTransport: Input channel constructed: fd=71
2020-03-11 12:34:40.137 16113-16113/com.example.crickattemp D/InputMethodManager: prepareNavigationBarInfo() DecorView@923ef8a[MainActivity]
2020-03-11 12:34:40.138 16113-16113/com.example.crickattemp D/InputMethodManager: getNavigationBarColor() -855310
2020-03-11 12:34:40.138 16113-16113/com.example.crickattemp V/InputMethodManager: Starting input: tba=com.example.crickattemp ic=null mNaviBarColor -855310 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
2020-03-11 12:34:40.138 16113-16113/com.example.crickattemp D/InputMethodManager: startInputInner - Id : 0
2020-03-11 12:34:41.308 16113-16113/com.example.crickattemp D/ViewRootImpl@60a44a2[MainActivity]: ViewPostIme pointer 0
2020-03-11 12:34:41.400 16113-16113/com.example.crickattemp D/ViewRootImpl@60a44a2[MainActivity]: ViewPostIme pointer 1
2020-03-11 12:34:41.429 16113-16113/com.example.crickattemp D/InputMethodManager: prepareNavigationBarInfo() DecorView@923ef8a[MainActivity]
2020-03-11 12:34:41.429 16113-16113/com.example.crickattemp D/InputMethodManager: getNavigationBarColor() -855310
2020-03-11 12:34:41.429 16113-16113/com.example.crickattemp V/InputMethodManager: Starting input: tba=com.example.crickattemp ic=com.android.internal.widget.EditableInputConnection@472126 mNaviBarColor -855310 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
2020-03-11 12:34:41.429 16113-16113/com.example.crickattemp D/InputMethodManager: startInputInner - Id : 0
2020-03-11 12:34:41.437 16113-16113/com.example.crickattemp I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
2020-03-11 12:34:41.441 16113-16113/com.example.crickattemp D/InputTransport: Input channel constructed: fd=74
2020-03-11 12:34:41.441 16113-16113/com.example.crickattemp D/InputTransport: Input channel destroyed: fd=71
2020-03-11 12:34:41.442 16113-16113/com.example.crickattemp D/InputMethodManager: SSI - flag : 0 Pid : 16113 view : com.example.crickattemp
2020-03-11 12:34:41.442 16113-16113/com.example.crickattemp D/InputMethodManager: prepareNavigationBarInfo() DecorView@923ef8a[MainActivity]
2020-03-11 12:34:41.442 16113-16113/com.example.crickattemp D/InputMethodManager: getNavigationBarColor() -855310
2020-03-11 12:34:41.451 16113-16113/com.example.crickattemp D/Editor: waiting for the right moment
2020-03-11 12:34:41.657 16113-16113/com.example.crickattemp D/ViewRootImpl@60a44a2[MainActivity]: MSG_RESIZED: frame=[0,0][720,1480] ci=[0,48][0,96] vi=[0,48][0,616] or=1
2020-03-11 12:34:43.265 16113-16113/com.example.crickattemp D/ViewRootImpl@60a44a2[MainActivity]: ViewPostIme pointer 0
2020-03-11 12:34:43.811 16113-16113/com.example.crickattemp D/ViewRootImpl@60a44a2[MainActivity]: ViewPostIme pointer 1
2020-03-11 12:34:43.823 16113-16113/com.example.crickattemp D/AndroidRuntime: Shutting down VM
2020-03-11 12:34:43.834 16113-16113/com.example.crickattemp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.crickattemp, PID: 16113
    java.lang.NumberFormatException: For input string: "androidx.appcompat.widget.AppCompatEditText{7ec578b VFED..CL. .F...... 136,441-556,532 #7f07004e app:id/data}"
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
        at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
        at java.lang.Float.parseFloat(Float.java:451)
        at com.example.crickattemp.MainActivity$1.onClick(MainActivity.java:28)
        at android.view.View.performClick(View.java:7339)
        at android.widget.TextView.performClick(TextView.java:14279)
        at android.view.View.performClickInternal(View.java:7305)
        at android.view.View.access$3200(View.java:846)
        at android.view.View$PerformClick.run(View.java:27787)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7081)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
2020-03-11 12:34:43.868 16113-16113/com.example.crickattemp I/Process: Sending signal. PID: 16113 SIG: 9

P.S .:我是Android开发的初学者。这是我第一次在stackoverflow上发帖,所以如果我犯了任何菜鸟错误,请原谅我。

android
1个回答
0
投票

您是在toString()本身而不是其文本上调用EditText

Float.parseFloat(e1.toString());

您需要致电getText()

Float.parseFloat(e1.getText().toString());

我可以从此错误中找出答案:

java.lang.NumberFormatException: For input string: "androidx.appcompat.widget.AppCompatEditText{7ec578b VFED..CL. .F...... 136,441-556,532 #7f07004e app:id/data}"
© www.soinside.com 2019 - 2024. All rights reserved.