gpt4 book ai didi

java - 安卓:NullPointerException 错误

转载 作者:行者123 更新时间:2023-11-29 04:52:26 26 4
gpt4 key购买 nike

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

我有 2 个类:

  1. CreateContactActivityl.java:

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

  1. RegexOCR1.java:

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

错误发生在 RegexOCR1.java 中,如 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,如下面的链接所示:

Crashlytics Results

CreateContactActivityOCR.java中:

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

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

在 RegexOCR1.java 中:

下面是我用来接收文本输出然后传入方法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 中的哪个进程。

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

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

最佳答案

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

关于java - 安卓:NullPointerException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34917586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com