2011-12-24 86 views
1

嗨我試圖做一個應用程序,其中有一個搜索框,我寫了一些代碼,但是,當我嘗試鍵入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(本機方法)

感謝每個人誰幫助我。

+0

什麼行代碼是'Search.java'行114? – 2011-12-24 01:01:28

+0

data_first [i] = first.get(i); – Valerio 2011-12-24 01:05:25

+0

查看我的回答編輯。 – 2011-12-24 01:24:12

回答

0

我猜sort參數之一是null

經OP評論確認,first_sortnull


按照this blog post,您需要:

  1. 保存您的所有類變量的值,包括在onSaveInstanceState(Bundle)到束靜態變量。
  2. onCreate(Bundle)中恢復捆綁中的變量(如果它不是null)。

參見:


...這樣的事情(未測試):

public class Search extends Activity { 
    // snip... 

    public void onCreate(Bundle savedInstanceState) { 
     // snip... 

     if (first_sort == null) { 
      first_sort = savedInstanceState.getParcelableArrayList("first_sort"); 
     } 

     if (second_sort == null) { 
      second_sort = savedInstanceState.getParcelableArrayList("second_sort"); 
     } 
    } 

    // snip... 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putParcelableArrayList("first_sort", first_sort); 
     outState.putParcelableArrayList("second_sort", second_sort); 
    } 
} 
+0

謝謝你的答案,這是我的第一個應用程序,我不像你可以想象的那麼好。你能幫我寫出缺失的代碼或給出更好的線索嗎?再次感謝你。 P.S.我在Manifest中使用android:configChanges =「keyboard | keyboardHidden | orientation」而不是onSave和onRestore。它的工作也很好。 也感謝其他幫助。 – Valerio 2011-12-24 01:30:52

+0

當我說如果你能幫我編寫代碼,我的意思是如何解決NullPointerException,如果可以的話。 – Valerio 2011-12-24 01:39:19

+0

我已經修改了這個示例代碼[link](http://android-helper.blogspot.com/2011/07/android-search-in-custom-listview.html?showComment=1324684325255#c535218314476375289)但我不明白爲什麼我的代碼不起作用。我有兩個String [],而不是像例子中的String []和int []。也許這是什麼原因導致錯誤? – Valerio 2011-12-26 22:47:33