我点击添加按钮后我的应用程序就崩溃了

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

我创建了一个用于添加两个数字的应用程序,但由于一些不确定的错误,每次我单击“添加”按钮进行加法时,它都会崩溃

我已经检查了我的字符串资源和所有内容,但它没有解决

请告诉我这些 libmigui.so 文件是什么,因为它没有打开(参考:logcat)

这是我的 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"
    android:fitsSystemWindows="true"
    android:background="#C6F36C6C"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editTextNumber2"
        android:layout_width="265dp"
        android:layout_height="64dp"
        android:autofillHints="0"
        android:ems="10"
        android:inputType="number"
        android:textAlignment="center"
        android:textColor="#000000"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.493"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.325"
        tools:ignore="LabelFor,SpeakableTextPresentCheck,DuplicateSpeakableTextCheck,TextContrastCheck" />

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        tools:ignore="MissingConstraints" />

    <EditText
        android:id="@+id/editTextNumber"
        android:layout_width="268dp"
        android:layout_height="53dp"
        android:autofillHints=""
        android:inputType="number"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.195"
        tools:ignore="LabelFor,SpeakableTextPresentCheck,TextContrastCheck" />

    <Button
        android:id="@+id/button"
        android:layout_width="131dp"
        android:layout_height="84dp"
        android:layout_marginBottom="276dp"
        android:background="@drawable/cylinder_button"
        android:text="@string/Add"
        android:textAllCaps="false"
        android:textColor="#EF6262"
        android:textSize="28sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextNumber2"
        app:layout_constraintVertical_bias="1.0"
        tools:ignore="TextContrastCheck,TextSizeCheck" />

    <TextView
        android:id="@+id/OutPut"
        android:layout_width="340dp"
        android:layout_height="52dp"
        android:gravity="center"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.492"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button"
        app:layout_constraintVertical_bias="0.446"
        tools:ignore="TextContrastCheck" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是MainActivity文件

package com.example.calculator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    Button button;
    EditText tv , tv2;
    TextView OutPut;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
        tv = findViewById(R.id.editTextNumber);
        tv2 = findViewById(R.id.editTextNumber2);
        OutPut = findViewById(R.id.OutPut);
        button.setOnClickListener((View v) -> {
            String s = tv.getText().toString();
            String s2 = tv2.getText().toString();
            int is = 0;
            is = Integer.parseInt(s);
            int is2 = 0;
            is2 = Integer.parseInt(s2);
            int output = 1234;
            OutPut.setText(output);
            Toast.makeText(MainActivity.this, "Addition Done !", Toast.LENGTH_SHORT).show();

        });


    }
}

这里是日志猫

2023-07-29 19:23:18.856 26102-26367 AdrenoGLES-0            com.example.calculator               I  Build Config                     : S P 10.0.7 AArch64
2023-07-29 19:23:18.856 26102-26367 AdrenoGLES-0            com.example.calculator               I  Driver Path                      : /vendor/lib64/egl/libGLESv2_adreno.so
2023-07-29 19:23:18.872 26102-26367 AdrenoGLES-0            com.example.calculator               I  PFP: 0x016ee190, ME: 0x00000000
2023-07-29 19:23:18.894 26102-26367 hw-ProcessState         com.example.calculator               D  Binder ioctl to enable oneway spam detection failed: Invalid argument
2023-07-29 19:23:18.918 26102-26102 libc                    com.example.calculator               I  Requested dump for tid 26102 (mple.calculator)
2023-07-29 19:23:19.170 26102-26367 mple.calculato          com.example.calculator               E  open libmigui.so failed! dlopen - dlopen failed: library "libmigui.so" not found
2023-07-29 19:23:19.174 26102-26102 Looper                  com.example.calculator               W  PerfMonitor doFrame : time=350ms vsyncFrame=0 latency=1ms procState=-1 historyMsgCount=1 (msgIndex=1 wall=350ms seq=14 running=36ms runnable=2ms io=3ms late=1ms h=android.view.Choreographer$FrameHandler c=android.view.Choreographer$FrameDisplayEventReceiver)
2023-07-29 19:23:19.179 26102-26102 Looper                  com.example.calculator               W  PerfMonitor doFrame : time=5ms vsyncFrame=0 latency=346ms procState=-1 historyMsgCount=4
2023-07-29 19:23:19.188 26102-26102 DecorView[]             com.example.calculator               D  onWindowFocusChanged hasWindowFocus true
2023-07-29 19:23:21.308 26102-26102 Compatibil...geReporter com.example.calculator               D  Compat change id reported: 163400105; UID 10447; state: ENABLED
2023-07-29 19:23:21.309 26102-26102 InputMethodManager      com.example.calculator               D  showSoftInput() view=androidx.appcompat.widget.AppCompatEditText{6f95288 VFED..CL. .F.P..ID 172,422-909,568 #7f0800ae app:id/editTextNumber aid=1073741825} flags=0 reason=SHOW_SOFT_INPUT
2023-07-29 19:23:21.319 26102-26102 AssistStructure         com.example.calculator               I  Flattened final assist data: 1592 bytes, containing 1 windows, 8 views
2023-07-29 19:23:21.493 26102-26102 InsetsController        com.example.calculator               D  show(ime(), fromIme=true)
2023-07-29 19:23:22.600 26102-26102 InputMethodManager      com.example.calculator               D  showSoftInput() view=androidx.appcompat.widget.AppCompatEditText{3f1b67a VFED..CL. .F.P..ID 173,693-902,869 #7f0800af app:id/editTextNumber2 aid=1073741824} flags=0 reason=SHOW_SOFT_INPUT
2023-07-29 19:23:22.609 26102-26102 IInputConnectionWrapper com.example.calculator               W  requestCursorAnchorInfo on inactive InputConnection
2023-07-29 19:23:22.644 26102-26102 InsetsController        com.example.calculator               D  show(ime(), fromIme=true)
2023-07-29 19:23:22.672 26102-26102 InsetsController        com.example.calculator               D  show(ime(), fromIme=true)
2023-07-29 19:23:23.362 26102-26102 mple.calculato          com.example.calculator               E  Invalid ID 0x000004d2.
2023-07-29 19:23:23.363 26102-26102 AndroidRuntime          com.example.calculator               D  Shutting down VM
2023-07-29 19:23:23.365 26102-26102 AndroidRuntime          com.example.calculator               E  FATAL EXCEPTION: main
                                                                                                    Process: com.example.calculator, PID: 26102
                                                                                                    android.content.res.Resources$NotFoundException: String resource ID #0x4d2
                                                                                                        at android.content.res.Resources.getText(Resources.java:463)
                                                                                                        at android.content.res.MiuiResources.getText(MiuiResources.java:106)
                                                                                                        at android.widget.TextView.setText(TextView.java:6513)
                                                                                                        at com.example.calculator.MainActivity.lambda$onCreate$0$com-example-calculator-MainActivity(MainActivity.java:31)
                                                                                                        at com.example.calculator.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
                                                                                                        at android.view.View.performClick(View.java:7745)
                                                                                                        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1211)
                                                                                                        at android.view.View.performClickInternal(View.java:7722)
                                                                                                        at android.view.View.access$3700(View.java:854)
                                                                                                        at android.view.View$PerformClick.run(View.java:29111)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:938)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:210)
                                                                                                        at android.os.Looper.loop(Looper.java:299)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8319)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1038)
2023-07-29 19:23:23.361 26102-26102 mple.calculator         com.example.calculator               W  type=1400 audit(0.0:622224): avc: denied { search } for name="mqsas" dev="sda16" ino=208 scontext=u:r:untrusted_app:s0:c191,c257,c512,c768 tcontext=u:object_r:mqsas_data_file:s0 tclass=dir permissive=0 app=com.example.calculator
2023-07-29 19:23:23.374 26102-26102 OOMEventManagerFK       com.example.calculator               W  Failed to mkdir /data/mqsas/hprof/
2023-07-29 19:23:23.361 26102-26102 mple.calculator         com.example.calculator               W  type=1400 audit(0.0:622225): avc: denied { search } for name="mqsas" dev="sda16" ino=208 scontext=u:r:untrusted_app:s0:c191,c257,c512,c768 tcontext=u:object_r:mqsas_data_file:s0 tclass=dir permissive=0 app=com.example.calculator
2023-07-29 19:23:23.361 26102-26102 mple.calculator         com.example.calculator               W  type=1400 audit(0.0:622226): avc: denied { search } for name="mqsas" dev="sda16" ino=208 scontext=u:r:untrusted_app:s0:c191,c257,c512,c768 tcontext=u:object_r:mqsas_data_file:s0 tclass=dir permissive=0 app=com.example.calculator
2023-07-29 19:23:23.361 26102-26102 mple.calculator         com.example.calculator               W  type=1400 audit(0.0:622227): avc: denied { search } for name="mqsas" dev="sda16" ino=208 scontext=u:r:untrusted_app:s0:c191,c257,c512,c768 tcontext=u:object_r:mqsas_data_file:s0 tclass=dir permissive=0 app=com.example.calculator
2023-07-29 19:23:23.361 26102-26102 mple.calculator         com.example.calculator               W  type=1400 audit(0.0:622228): avc: denied { getattr } for path="/data/mqsas" dev="sda16" ino=208 scontext=u:r:untrusted_app:s0:c191,c257,c512,c768 tcontext=u:object_r:mqsas_data_file:s0 tclass=dir permissive=0 app=com.example.calculator
2023-07-29 19:23:23.417 26102-26102 Process                 com.example.calculator               I  Sending signal. PID: 26102 SIG: 9

java android-studio crash
1个回答
0
投票

始终检查

getText()
是否返回 null,为了方便起见,您可以使用
TextUtils.isEmpty(string)

此外,

Integer.parseInt(string)
可能会抛出异常,应该用this之类的东西来处理,或者只是用
Toast
显示错误。

public static int parseWithDefault(String number, int defaultVal) {
  try {
    return Integer.parseInt(number);
  } catch (NumberFormatException e) {
    return defaultVal;
  }
}

编辑

仔细看看,似乎实际问题是您正在使用

.setText(int)
,但
int
指的是资源ID,而不是您想要显示的值。

int output = 1234;
OutPut.setText(output);

要显示值,它应该始终是

String
。使用
String.valueOf(…)
将项目转换为其
String
表示形式。

int output = 1234;
OutPut.setText(String.valueOf(output));
© www.soinside.com 2019 - 2024. All rights reserved.