如何用Java.lang.NullPointerException解决FATAL EXCEPTION?

时间:2011-02-02 08:22:34

标签: android android-edittext

以下是My Code Snippet,它根据在EditText Box中输入的文本执行搜索操作,请查看它...

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的RunTime FATALException。请告诉我一些不在这里的方法.. 豁免如下..

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)

1 个答案:

答案 0 :(得分:1)

这里是第65行,它只是一个If(条件),即;

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

获取Null Pointer Exception因为List lv_arr[i]的值返回Null值,导致运行时FATAL EXCEPTION为java.lang.NullPointerException

所以当我查看我的代码时,我发现列表大小中有一些冲突在一个地方44而在另一个地方它是53,因此当i的值为44时,列表lv_arr[i]=null和与null值进行比较会产生此例外...

感谢大家,我在我的代码中成功清除了它..