Android - 奇怪的“java.lang.NullPointerException”

时间:2011-12-24 00:56:43

标签: android nullpointerexception

您好我正在尝试创建一个有搜索框的应用程序,我编写了一些代码但是,当我尝试在AutoCompleteTextView中键入第一个字母时,它会给我一个错误(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 01: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):在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):在android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889) 12-24 01:37:55.231:E / AndroidRuntime(15241):在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):在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):在android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:583) 12-24 01:37:55.231:E / AndroidRuntime(15241):在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(IInputConnectionWrapper.java:247) 12-24 01:37:55.231:E / AndroidRuntime(15241):at com.android.internal.view.IInputConnectionWrapper $ MyHandler.handleMessage(IInputConnectionWrapper.java:73) 12-24 01:37:55.231:E / AndroidRuntime(15241):在android.os.Handler.dispatchMessage(Handler.java:99) 12-24 01:37:55.231:E / AndroidRuntime(15241):在android.os.Looper.loop(Looper.java:123) 12-24 01:37:55.231:E / AndroidRuntime(15241):在android.app.ActivityThread.main(ActivityThread.java:4627) 12-24 01:37:55.231:E / AndroidRuntime(15241):at java.lang.reflect.Method.invokeNative(Native Method) 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) 12-24 01:37:55.231:E / AndroidRuntime(15241):at dalvik.system.NativeStart.main(Native Method)

感谢每个帮助我的人。

1 个答案:

答案 0 :(得分:0)

我猜测其中一个sort参数是null

OP评论确认,first_sortnull


根据this blog post,您需要:

  1. 将所有类变量的值(包括onSaveInstanceState(Bundle)中的静态变量保存到捆绑包中。
  2. null中恢复捆绑中的变量(如果不是onCreate(Bundle))。
  3. 另见:


    ...像这样(未经测试):

    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);
        }
    }