我想在我的ListFragment中添加一個搜索功能。我在一個標準的Activity中完成了它,但是試圖將它移植到碎片上。 什麼我目前做的是這樣的:安卓在ListFragment中實現搜索
我有承載片段一個Main.java。
我有一個主機與R.id.frag_list列表陣列List.TERM一個ListFrag.java
我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和文字聽衆和我試着鍵入一個字母的瞬間,應用程序錯誤日誌崩潰不是Main.java。
- 我不是從Main.java調用新的列表,因此是一個NullPointerException。
任何人有建議來解決這個問題?謝謝:)
你能指出Main.java中哪行是#54行嗎?粗略看一下onTextChanged就會發現有幾個地方可能會拋出NullPointerException,所以如果沒有這些信息,很難確定哪一個實際上是從你的堆棧跟蹤中拋出一個。 – Nick 2012-08-11 05:23:23
它指的是:lv.setAdapter(new ArrayAdapter(...); –
bernlim
2012-08-11 05:31:21