Android:NullPointerException错误 [英] Android: NullPointerException error

查看:56
本文介绍了Android:NullPointerException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道询问NullPointerException的答案很乏味,并且那里也有类似的问题.但是,我只是无法从其他问题中找到解决问题的方法.

I know it is dull to ask answers for a NullPointerException, and there are similar questions out there. However, I just cannot find a solution for my problem from the other questions.

我有2个课程:

  1. CreateContactActivityl.java:

将文本输出的意图传递给RegexOCR1.java

Passes intent of a text output to RegexOCR1.java

  1. RegexOCR1.java:

接收文本输出,将文本输出传递给此类中的方法

Receives the text output, pass text output into a method that is within this class

如logcat所述,该错误在RegexOCR1.java中发生:

The error occurs in RegexOCR1.java as stated by the logcat:

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.l33902.contactmanagment1512/com.example.l33902.contactmanagment.RegexOCR1}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3155)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3263)
       at android.app.ActivityThread.access$1000(ActivityThread.java:197)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1687)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:145)
       at android.app.ActivityThread.main(ActivityThread.java:6897)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
       at com.example.l33902.contactmanagment.RegexOCR1.onCreate(RegexOCR1.java:32)
       at android.app.Activity.performCreate(Activity.java:6550)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3108)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3263)
       at android.app.ActivityThread.access$1000(ActivityThread.java:197)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1687)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:145)
       at android.app.ActivityThread.main(ActivityThread.java:6897)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

我还实现了Crashlytics进行调试,如下面的链接所示:

I have also implemented Crashlytics for debugging as shown in the link below:

崩溃结果

CreateContactActivityOCR.java 中:

下面是将文本输出的意图传递给RegexOCR1.java的代码

Below are the codes that passes the intent of text output to RegexOCR1.java

private void passText(){
        //Log.i(TAG, "PassText");
        Intent intent = new Intent(this, RegexOCR1.class);
        startActivity(intent);
    }

在RegexOCR1.java中:

In RegexOCR1.java:

下面是我用来接收文本输出然后传递给方法EmailValidator()的代码:

Below are the codes that I used to receives text output and then passes in the method EmailValidator():

public class RegexOCR1 extends Activity {

    private Pattern pattern;
    private Matcher matcher;

    private String recognizedText, textToUse;
    private String mFromLang, mCurrentLang;

    private static final String EMAIL_PATTERN =
            "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                    + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

    private static final String PHONE_PATTERN =
            "^[89]\\d{7}$";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        // Getting the path of the image from another class
        Bundle extras = this.getIntent().getExtras();
        recognizedText = extras.getString("TEXT");
        textToUse = recognizedText;

        // Getting the language used for text recognition
        mFromLang = extras.getString("LANG");
        mCurrentLang = mFromLang;
        //Log.i(TAG, mFromLang);

        EmailValidator();
    }


    public String EmailValidator() {

        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(textToUse);
        if (matcher.find()) {
            String email = textToUse.substring(matcher.start(), matcher.end());

        } else {
            // TODO handle condition when input doesn't have an email address
        }

        return textToUse;
    }

    public boolean validate(final String hex) {

        matcher = pattern.matcher(hex);
        return matcher.matches();

    }

    private void showText(){
        //Log.i(TAG, "ShowText");
        Intent intent = new Intent(this, CreateContactActivityOCR.class);
        startActivity(intent);
    }
} 

我无法确定RegexOCR1.java中哪个进程发生错误.

I can't figure at which process within RegexOCR1.java does the error occurs.

根据Crashlytics,它是第32行:recognizedText = extras.getString("TEXT");

According to Crashlytics, it is line 32: recognizedText = extras.getString("TEXT");

但是,我在CreateContactActivityOCR.java中使用了这一行来收集来自另一个类的文本输出,而没有错误.

However, I have used this line in CreateContactActivityOCR.java to collect the text output from another class with no error.

推荐答案

private void passText(){
    //Log.i(TAG, "PassText");
    Intent intent = new Intent(this, RegexOCR1.class);
    intent.putExtra("TEXT", "your text here");
    startActivity(intent);
}

这篇关于Android:NullPointerException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆