在应用程序中获取null异常

时间:2014-04-01 07:35:56

标签: android listview textwatcher

在我的应用程序中,我想根据自动完成中输入的文本过滤名称和编号,但它返回我的空指针异常,我的应用程序关闭。我使用相同的适配器列表视图和autocompletetextview.how得到列表项以用户输入的文本开头。请提出建议....

MainActivity

public class MainActivity extends Activity implements OnItemClickListener,
        OnItemSelectedListener {
    SearchView mSearchView;
    AutoCompleteTextView searchText;
    ArrayList<String> name1 = new ArrayList<String>();
    ArrayList<String> phno1 = new ArrayList<String>();


    String[] cellArray = null;


    String contacts;

    static int check1;
    ListView lv;

    String phoneNumber, name;


    StringBuilder b = new StringBuilder();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActionBar actionBar = getActionBar();

        actionBar.setDisplayHomeAsUpEnabled(true);
        ColorDrawable colorDrawable = new ColorDrawable(
                Color.parseColor("#00aef0"));

        actionBar.setBackgroundDrawable(colorDrawable);
        setContentView(R.layout.get);
        // mStatusView = (TextView) findViewById(R.id.text1);

        getAllCallLogs(this.getContentResolver());
        searchText = (AutoCompleteTextView) findViewById(R.id.autocomplete);

        lv = (ListView) findViewById(R.id.lv);

        ma = new MyAdapter();

        searchText.setThreshold(1);
        searchText.setAdapter(ma);

        lv.setAdapter(ma);
        lv.setOnItemClickListener(this);
        lv.setItemsCanFocus(false);
        lv.setTextFilterEnabled(true);



    }



    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        switch (item.getItemId()) {

        case android.R.id.home:
            StringBuilder checkedcontacts = new StringBuilder();
            System.out.println(".............." + ma.mCheckStates.size());
            for (int i = 0; i < name1.size(); i++)

            {
                if (ma.mCheckStates.get(i) == true) {
                    // phno0.add(name1.get(i).toString());
                    phno0.add(phno1.get(i).toString());
                    checkedcontacts.append(name1.get(i).toString());
                    checkedcontacts.append("\n");

                } else {
                    System.out.println("..Not Checked......"
                            + name1.get(i).toString());
                }

            }

            Intent returnIntent = new Intent();
            returnIntent.putStringArrayListExtra("name", phno0);
            setResult(RESULT_OK, returnIntent);

            finish();

            break;

        case R.id.addPage:

            break;
        case R.id.action_search:

            searchText.setVisibility(View.VISIBLE);
            //String text_To_search= searchText.getText().toString().trim();
            searchText.addTextChangedListener(new TextWatcher() {

                public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub

                    ma.getFilter().filter(s);



                }

                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start,
                        int count, int after) {
                    // TODO Auto-generated method stub

                }

            });
        break;
        }
        return super.onOptionsItemSelected(item);
    }





            ma.toggle(position);

//
    }

    public void getAllCallLogs(ContentResolver cr) {
        mPeopleList.clear();
        Cursor phones = cr.query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                        + " ASC");
        while (phones.moveToNext()) {
            phoneNumber = phones
                    .getString(phones
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            name = phones
                    .getString(phones
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

            System.out.println(phoneNumber);

            name1.add(name);
            phno1.add(phoneNumber);

            // meMap.put("name1","phno1");
            // mPeopleList.add(name);
            // mPeopleList.add(phoneNumber);

        }

        phones.close();

    }

    class MyAdapter extends BaseAdapter implements  Filterable,
            CompoundButton.OnCheckedChangeListener {
        public SparseBooleanArray mCheckStates;
        // public ListFilter listFilter;
        LayoutInflater mInflater;
        TextView tv1, tv;
        CheckBox cb;

        MyAdapter() {
            mCheckStates = new SparseBooleanArray(name1.size());
            mInflater = (LayoutInflater) MainActivity.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }





        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub
            View vi = convertView;
            if (convertView == null)
                vi = mInflater.inflate(R.layout.row, null);
            tv = (TextView) vi.findViewById(R.id.textView1);
            tv1 = (TextView) vi.findViewById(R.id.textView2);
            cb = (CheckBox) vi.findViewById(R.id.checkBox1);
            tv.setText(name1.get(position));
            tv1.setText(phno1.get(position));
            cb.setTag(position);

            cb.setChecked(mCheckStates.get(position, false));
            cb.setOnCheckedChangeListener(this);

            return vi;

        }

        public boolean isChecked(int position) {

            return mCheckStates.get(position, false);
        }

        public void setChecked(int position, boolean isChecked) {
            mCheckStates.put(position, isChecked);
        }

        public void toggle(int position) {
            setChecked(position, !isChecked(position));
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub
            mCheckStates.put((Integer) buttonView.getTag(), isChecked);

        }



        @Override
        public Filter getFilter() {
            // TODO Auto-generated method stub
            return null;
        }
    }



}

logcat的

04-01 14:29:36.318: E/AndroidRuntime(12166): FATAL EXCEPTION: main
04-01 14:29:36.318: E/AndroidRuntime(12166): java.lang.NullPointerException
04-01 14:29:36.318: E/AndroidRuntime(12166):    at com.example.smscampaign.MainActivity$1.onTextChanged(MainActivity.java:193)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at android.widget.TextView.sendOnTextChanged(TextView.java:8366)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at android.widget.TextView.handleTextChanged(TextView.java:8428)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:10367)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:676)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:435)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:333)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at android.os.Looper.loop(Looper.java:137)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at android.app.ActivityThread.main(ActivityThread.java:5283)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at java.lang.reflect.Method.invokeNative(Native Method)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at java.lang.reflect.Method.invoke(Method.java:511)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
04-01 14:29:36.318: E/AndroidRuntime(12166):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

NPE:

ma.getFilter().filter(s);

并且您的getFilter()会返回null

@Override
public Filter getFilter() {
    // TODO Auto-generated method stub
    return null;
}

所以在filter()上调用null会导致NPE。

您必须实际实施未实现的方法。