嗨我試圖做一個應用程序,其中有一個搜索框,我寫了一些代碼,但是,當我嘗試鍵入AutoCompleteTextView中的第一個字母時,它給我一個錯誤(java.lang.NullPointerException),沒有任何引起它的原因。Android - 奇怪的「java.lang.NullPointerException」
我mains.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<AutoCompleteTextView
android:id="@+id/acTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Scrivi">
<requestFocus />
</AutoCompleteTextView>
<ListView android:id="@+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fadingEdge="none"
android:fastScrollEnabled="true">
</ListView>
</LinearLayout>
我listitem_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:text="TextView"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent">
</TextView>
<TextView
android:text="TextView"
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
我的代碼:
public class Search extends Activity {
AutoCompleteTextView acTV;
ListView lview;
String[] first = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"};
String[] second = {"Uno", "Due", "Tre", "Quattro", "Cinque", "Sei", "Sette", "Otto", "Nove", "Dieci"};
int textlength = 0;
ArrayList<String> first_sort = new ArrayList<String>();
ArrayList<String> second_sort = new ArrayList<String>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mains);
acTV = (AutoCompleteTextView) findViewById(R.id.acTV);
lview = (ListView) findViewById(R.id.list);
lview.setAdapter(new MyCustomAdapter(first, second));
lview.setClickable(true);
lview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
Intent intent = new Intent(Search.this, Details.class);
startActivity(intent);
}
{
}
});
lview.setTextFilterEnabled(true);
acTV.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) {
textlength = acTV.getText().length();
first_sort.clear();
second_sort.clear();
for (int i = 0; i < first.length; i++) {
if (textlength <= first[i].length()) {
if (acTV.getText().toString().equalsIgnoreCase((String) first[i].subSequence(0, textlength))) {
first_sort.add(first[i]);
second_sort.add(first[i]);
}
}
}
lview.setAdapter(new MyCustomAdapter(first_sort, second_sort));
}
});
}
class MyCustomAdapter extends BaseAdapter {
String[] data_first;
String[] data_second;
{
}
MyCustomAdapter(String[] first, String[] second) {
data_first = first;
data_second = second;
}
MyCustomAdapter(ArrayList<String> first, ArrayList<String> second) {
for (int i = 0; i < first.size(); i++) {
data_first[i] = first.get(i);
data_second[i] = second.get(i);
}
}
public int getCount() {
return data_first.length;
}
public String getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.listitem_row, parent, false);
TextView textview = (TextView) row.findViewById(R.id.textView1);
TextView textview1 = (TextView) row.findViewById(R.id.textView2);
textview.setText(data_first[position]);
textview1.setText(data_second[position]);
return (row);
}
}
}
}
這是我收到的時候我嘗試輸入錯誤一封信:
12-24 0 1:37:55.231:E/AndroidRuntime(15241):致命例外:主 12-24 01:37:55.231:E/AndroidRuntime(15241):java.lang.NullPointerException 12-24 01:37:55.231:E/AndroidRuntime(15241):at it.gogle.com.Search $ MyCustomAdapter。(Search.java:114) 12-24 01:37:55.231:E/AndroidRuntime(15241):at it.gogle.com.Search $ 2 .onTextChanged(Search.java:87) 12-24 01:37:55.231:E/AndroidRuntime(15241):at android.widget.TextView.sendOnTextChanged(TextView.java:6240) 12-24 01:37:55.231 :E/AndroidRuntime(15241):在android.widget.TextView.handleTextChanged(TextView.java:6281) 12-24 01:37:55.231:E/AndroidRuntime(15241):在android.widget.TextView $ ChangeWatcher.onTextChanged (TextView.java:6456) 12-24 01:37:55.231:E/AndroidRuntime(15241):at android.text.SpannableStringBuilder.sendTextCha nge(SpannableStringBuilder.java:889) 12-24 01:37:55.231:E/AndroidRuntime(15241):at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352) 12-24 01:37:55.231: E/AndroidRuntime(15241):在android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269) 12-24 01:37:55.231:E/AndroidRuntime(15241):在android.text.SpannableStringBuilder.replace(SpannableStringBuilder。 java/432) 12-24 01:37:55.231:E/AndroidRuntime(15241):at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409) 12-24 01:37:55.231:E/AndroidRuntime 15241):android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28) 12-24 01:37:55.231:E/AndroidRuntime(15241):at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java: 583) 12-24 01:37:55.231:E/AndroidRuntime(15241):at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:174) 12-24 01:37:55.231:E/AndroidRuntime(15241) :at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:120) 12-24 01:37:55.231:E/AndroidRuntime(15241):at com.android.internal.view.IInputConnectionWrapper.executeMessage( EInputConnectionWrapper.java:247) 12-24 01:37:55.231:E/AndroidRuntime(15241) 37:55.231:E/AndroidRuntime(15241):at android.os.Handler.dispatchMessage(Handler.java:99) 12-24 01:37:55.231:E/AndroidRuntime(15241):at android.os.Looper。循環(Looper.java:123) 12-24 01:37:55.231:E/AndroidRuntime(15241):at android.app.ActivityThread.main(ActivityThread.java:4627) 12-24 01:37:55.231:E/AndroidRuntime(15241):at java.lang.reflect.Method.invokeNative(Native方法) 12-24 01:37:55.231:E/AndroidRuntime(15241):at java.lang.reflect.Method.invoke(Method.java:521) 12-24 01:37:55.231:E/AndroidRuntime 15241):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:858) 12-24 01:37:55.231:E/AndroidRuntime(15241):at com.android.internal.os。ZygoteInit.main(ZygoteInit.java:616) 1月12日至24日:37:55.231:E/AndroidRuntime(15241):在dalvik.system.NativeStart.main(本機方法)
感謝每個人誰幫助我。
什麼行代碼是'Search.java'行114? – 2011-12-24 01:01:28
data_first [i] = first.get(i); – Valerio 2011-12-24 01:05:25
查看我的回答編輯。 – 2011-12-24 01:24:12