我正在尝试在我的 ListFragment 中添加搜索功能。我已经在标准活动中完成了它,但我试图将它移植到片段中。我目前正在做的是这样的:
我有一个承载片段的 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)
我对这种错误的猜测是:
- 我应该在片段而不是 Main.java 下实现 editText 和文本侦听器。
- 我不是从 Main.java 调用新列表,因此是 NullPointerException。
有人有解决此问题的建议吗?谢谢 :)