2016-01-21 49 views
1

我知道它是無聊的問一個NullPointerException的答案,也有類似的問題。但是,我從其他問題找不到解決方案。Android:NullPointerException錯誤

我有2類:

  1. CreateContactActivityl.java:

通行證文本輸出的意圖RegexOCR1.java

  • 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

    In CreateContactActivityOCR.java

    下面的ar Ë該傳遞的文本輸出到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這條線,以收集從另一個類輸出的文本,並沒有錯誤。

    +0

    您需要驗證「getIntent()」非空,而「額外」的是在你的非空「的onCreate()」方法。它在某些情況下可能爲空。 –

    +1

    但你沒有通過startActivity傳遞兩個值Intent –

    +0

    你在哪裏設置了''TEXT'的值,在''passText''函數中,你錯過了它 –

    回答

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

    嘗試下面的代碼和並再次運行

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