2011-02-02 91 views
3

這裏是我的代碼片段,其執行基於在的EditText框輸入的文本搜索操作,請在其上具有看看...如何用Java.lang.NullPointerException解決FATAL EXCEPTION?

ed.addTextChangedListener(new TextWatcher() { 

     public void afterTextChanged(Editable s) { 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, 
     int after) { 
     } 

     public void onTextChanged(CharSequence s, int start, int before, 
     int count) { 
     b4.setVisibility(b4.VISIBLE); 
     textlength=ed.getText().length(); 
     arr_sort.clear(); 
     for(int i=0;i<lv_arr.length;i++) 
     { 
      if(textlength<=lv_arr[i].length()) 
      { 
      boolean b=ed.getText().toString().equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength)); 
      if(b) 
       { 
        arr_sort.add(lv_arr[i]); 
        //Log.i("In If:",arr_sort.toString()); 
       } 
      } 
     } 
     lv1.setAdapter(new ArrayAdapter<String>(BasicsActivity.this,R.layout.rowlayout2 ,R.id.label, arr_sort)); 
     //Log.i("Step6-","ok"); 
     } 
     }); 

現在的問題是,當我寫上的EditText查看什麼而運行它顯示了一些NullPointerException..Please運行時FATALException建議我一些某種方式離開這裏.. 的Excpetions如下..

02-02 13:29:48.577: ERROR/AndroidRuntime(541): FATAL EXCEPTION: main 
02-02 13:29:48.577: ERROR/AndroidRuntime(541): java.lang.NullPointerException 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at com.android.desitranslation.BasicsActivity$1.onTextChanged(BasicsActivity.java:65) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.widget.TextView.sendOnTextChanged(TextView.java:6295) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.widget.TextView.handleTextChanged(TextView.java:6336) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6485) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:195) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:132) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.widget.TextView.doKeyDown(TextView.java:4433) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.widget.TextView.onKeyDown(TextView.java:4267) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.view.KeyEvent.dispatch(KeyEvent.java:1256) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.view.View.dispatchKeyEvent(View.java:3855) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1687) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1120) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.app.Activity.dispatchKeyEvent(Activity.java:2073) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1663) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2560) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2535) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1867) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.os.Looper.loop(Looper.java:123) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at android.app.ActivityThread.main(ActivityThread.java:3647) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at java.lang.reflect.Method.invoke(Method.java:507) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
02-02 13:29:48.577: ERROR/AndroidRuntime(541):  at dalvik.system.NativeStart.main(Native Method) 
+3

,如果你告訴我們這行是65行(就是這樣產生的行這將有助於空指針異常)。你在onTextChanged()中使用了很多外部變量。可能其中的一個,或者其中一個數組槽是'null'。 – 2011-02-02 08:27:12

回答

1

在這裏,在號線65,這不過是一個如果(條件)即;

if(textlength<=lv_arr[i].length()) 
{..} 

獲取Null Pointer Exception由於的列表lv_arr[i]的值被返回一個空值導致具有java.lang.NullPointerException運行時致命的異常。

因此,當我檢查我的代碼時,我發現列表大小存在一些衝突,在一個地方44和另一個地方是53因此當我的值是44的列表lv_arr[i]=null和比較null值會產生這種異常...

我成功清除它在我的代碼感謝所有的你..

相關問題