gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:32:52 25 4
gpt4 key购买 nike

这是我的代码 fragment ,它根据在 EditText 框中输入的文本执行搜索操作,请看一下...

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 View 上写任何东西时,它会显示带有一些 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)

最佳答案

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

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

得到 Null Pointer Exception 因为列表 lv_arr[i] 的值返回一个 Null 值,这会导致运行时发生致命异常 java.lang. lang.NullPointerException.

所以当我检查我的代码时,我发现列表大小有一些冲突,在一个地方是 44,在另一个地方是 53,因此当 i 的值为 44 时,列表 lv_arr[i ]=null 并与 null 值进行比较会引发此异常...

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

关于android - 如何用 Java.lang.NullPointerException 解决 FATAL EXCEPTION?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4872164/

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