按钮单击导致android应用程序java.lang.IllegalStateException崩溃

时间:2018-03-06 17:06:54

标签: java android

每当我在我的应用程序中按Enter键时,我的简单Android应用程序崩溃。每次按Enter键时都会出现此问题。它与java.lang.IllegalStateException崩溃根据MIUI Report Bug Feature.I提供了XML和Java代码。

对于解决此崩溃问题,我们将不胜感激。

XML文件

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".AskKey">

    <TextView
        android:id="@+id/random_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Please Enter Your Key"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/check_key"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberSigned" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_bright"
            android:onClick="readKey"
            android:text="Enter" />
    </LinearLayout>
</RelativeLayout>

JAVA CODE

package app.project.virtualdiary;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AskKey extends AppCompatActivity {

    private EditText mEditText;
    private String noteId,email,key;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ask_key);
        mEditText = findViewById(R.id.check_key);
        Intent intent = getIntent();
        noteId = intent.getStringExtra("noteId");
        email = intent.getStringExtra("email");
        key = intent.getStringExtra("key");
        TextView cat = findViewById(R.id.random_text);
        cat.setText(key); //Using this to check actually i am getting key value
    }
    public void readKey(View v){
        final String baby = mEditText.getText().toString().trim();
        if(TextUtils.isEmpty(baby)){
            Toast.makeText(getApplicationContext(),"Enter Key",Toast.LENGTH_SHORT).show();
            return;
        }
        int num = Integer.getInteger(baby);
        int code = Integer.getInteger(key); //App may be crashing somewhere near this line of code
        if (num == code){
            Intent intent = new Intent(getApplicationContext(), ShowNote.class);
            intent.putExtra("noteId", noteId);
            intent.putExtra("email",email);
            intent.putExtra("key",key);
            startActivity(intent);
        }
        else{
            Toast.makeText(getApplicationContext(),"Key Match Failed",Toast.LENGTH_SHORT).show();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您发布的代码存在两个问题。首先,您可能不想使用Integer.getInteger()

int num = Integer.getInteger(baby);
int code = Integer.getInteger(key);

getInteger()州的文档:

  

确定具有指定名称的系统属性的整数值。

我希望你想要Integer.parseInt()

其次,您不应使用应用程序上下文来启动新活动:

Intent intent = new Intent(getApplicationContext(), ShowNote.class);

如果Intent包含FLAG_ACTIVITY_NEW_TASK标志,那么应用程序上下文只能启动活动。通常,尝试使用活动上下文来启动新活动。由于您的readKey()方法是AskKey活动的一部分,因此您只需使用this作为上下文参数。

总之,你应该有这个代码:

int num = Integer.parseInt(baby);
int code = Integer.parseInt(key);
if (num == code){
    Intent intent = new Intent(this, ShowNote.class);
    ...
}

请注意,正如所写的那样,如果您的baby字符串没有输入数字,那么仍然会崩溃(例如,如果您键入类似&#34; hello world& #34;进入EditText)。

相关问题