我正在尝试在我的ListFragment中添加搜索功能。 我已经在标准Activity中完成了此操作,但现在我想将其移植到片段中。
目前我正在做的是:
我的main.xml文件如下所示:
当我运行应用程序时,我点击editText。在我尝试输入一个字母的瞬间,应用程序崩溃,并显示错误日志:
我有一个Main.java,它托管片段。
我有一个ListFrag.java,它用R.id.frag_list托管列表数组List.TERM
我的Main.java像这样实现addTextChangedListener:
et.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s,
int start, int count, int after)
{
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s,
int start, int before, int count)
{
textlength = et.getText().length();
array_sort.clear();
for (int i = 0; i < List.TERM.length; i++)
{
if (textlength <= List.TERM[i].length())
{
if(et.getText().toString().equalsIgnoreCase(
(String)
List.TERM[i].subSequence(0,
textlength)))
{array_sort.add(List.TERM[i]);
}
}
}
lv.setAdapter(new ArrayAdapter<String>
(Main.this,
R.layout.main,R.id.frag_list, array_sort));
}
});
我的main.xml文件如下所示:
<LinearLayout
android:layout_width="fill_parent"
android:background="@drawable/searchbar"
android:layout_height="60dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
>
<EditText android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="@string/prompt">
</EditText>
</LinearLayout>
<fragment
android:id="@+id/frag_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="50dp"
android:layout_marginTop="90dp"
android:layout_marginBottom="40dp"
class="com.bernard.ListFrag" />
当我运行应用程序时,我点击editText。在我尝试输入一个字母的瞬间,应用程序崩溃,并显示错误日志:
08-11 12:59:57.090: E/AndroidRuntime(20220): FATAL EXCEPTION: main
08-11 12:59:57.090: E/AndroidRuntime(20220): java.lang.NullPointerException
08-11 12:59:57.090: E/AndroidRuntime(20220): at com.bernard.Main$1.onTextChanged(Main.java:54)
08-11 12:59:57.090: E/AndroidRuntime(20220): at android.widget.TextView.sendOnTextChanged(TextView.java:8105)
08-11 12:59:57.090: E/AndroidRuntime(20220): at android.widget.TextView.handleTextChanged(TextView.java:8152)
08-11 12:59:57.090: E/AndroidRuntime(20220): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8532)
08-11 12:59:57.090: E/AndroidRuntime(20220): at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:892)
08-11 12:59:57.090: E/AndroidRuntime(20220): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
08-11 12:59:57.090: E/AndroidRuntime(20220): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:266)
08-11 12:59:57.090: E/AndroidRuntime(20220): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:443)
08-11 12:59:57.090: E/AndroidRuntime(20220): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:420)
08-11 12:59:57.090: E/AndroidRuntime(20220): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:29)
08-11 12:59:57.090: E/AndroidRuntime(20220): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:669)
08-11 12:59:57.090: E/AndroidRuntime(20220): at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:431)
08-11 12:59:57.090: E/AndroidRuntime(20220): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:369)
08-11 12:59:57.090: E/AndroidRuntime(20220): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
08-11 12:59:57.090: E/AndroidRuntime(20220): at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 12:59:57.090: E/AndroidRuntime(20220): at android.os.Looper.loop(Looper.java:156)
08-11 12:59:57.090: E/AndroidRuntime(20220): at android.app.ActivityThread.main(ActivityThread.java:5025)
08-11 12:59:57.090: E/AndroidRuntime(20220): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 12:59:57.090: E/AndroidRuntime(20220): at java.lang.reflect.Method.invoke(Method.java:511)
08-11 12:59:57.090: E/AndroidRuntime(20220): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-11 12:59:57.090: E/AndroidRuntime(20220): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-11 12:59:57.090: E/AndroidRuntime(20220): at dalvik.system.NativeStart.main(Native Method)
我猜测这种错误可能是:
- 我应该在片段下实现editText和text监听器,而不是在Main.java下实现。
- 我没有从Main.java调用新列表,因此会出现NullPointerException。
有没有人有修复这个问题的建议?谢谢 :)