尝试调用虚方法' void android.widget.ListView.clearTextFilter()'在null对象引用上

时间:2016-05-12 08:56:37

标签: java android searchview

现在我在listview中制作searchview

这是我的代码

package intikom.streammobile.Customer;

import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.util.ArrayList;
import java.util.List;

import intikom.streammobile.Index;
import intikom.streammobile.R;
import intikom.streammobile.Utility.BigProccess;
import intikom.streammobile.Utility.RESTClient;
import intikom.streammobile.Workflow.MyRequest.ListRequestItem;
import intikom.streammobile.Workflow.WorkflowActivity;

public class ListCustomer extends AppCompatActivity implements SearchView.OnQueryTextListener {
    private static final String ARG_SECTION_NUMBER = "section_number";
    private static String URLService;
    private static String rowsPerPage;
    private static ListView lv;
    private static SharedPreferences pref;
    private List<ListCustomerItem> mItems;
    private List<ListCustomerItem> mItems2;
    private ListCustomerItem item;
    private static String customerID;


    public ListCustomer() {
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_customer);

        URLService = getString(R.string.URLService);
        final ListView lv = (ListView)findViewById(R.id.lvCustomerList);
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                item = new ListCustomerItem();
                item = mItems.get(position);
                SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
                SharedPreferences.Editor editor = pref.edit();
                editor.putString("CustomerID", item.CustID);
                editor.putString("CustomerName", item.CustName);
                editor.commit();


                Intent i = new Intent(getApplicationContext(),WorkflowActivity.class);
                i.putExtra("CustomerName", item.CustName);
                setResult(1000, i);
                finish();

            }
        });

        getCustomer();
        getLocal();
        if (mItems==null)
        {
            if(mItems2==null) {
                Toast.makeText(this, "Internet connection appears to be offline",
                        Toast.LENGTH_LONG).show();
            }
            else
            {
                lv.setAdapter(new ListCustomerAdapter(this, mItems2));

            }
        }
        else
        {
            lv.setAdapter(new ListCustomerAdapter(this, mItems));

        }

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.searchbox, menu);

        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));

        searchView.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(this);

        return true;
    }


    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        if (TextUtils.isEmpty(newText))
        {
            lv.clearTextFilter();
        }
        else
        {
            lv.setFilterText(newText.toString());
        }


        return true;
    }
}

这是XMl布局菜单

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="collapseActionView|always"
        android:title="Search"
        app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>

并在logcat中说:

  

尝试调用虚方法&#39; void   android.widget.ListView.clearTextFilter()&#39;在空对象引用上

你能告诉我我错在哪里吗?

1 个答案:

答案 0 :(得分:2)

更改此行:

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

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

您已将listview个实例声明为global,并再次将其定义为local变量,这就是global null实例中create table bi_test_tr ( filed1 int, filed2 int, filed3 int ) GO create table test ( datechange datetime, sum1 int, sum2 int, sum3 int ) GO CREATE TRIGGER test_tr ON bi_test_tr AFTER INSERT, UPDATE, DELETE AS BEGIN INSERT INTO test (datechange, sum1, sum2, sum3) SELECT GETDATE(), SUM([filed1]), SUM([filed2]), SUM([filed3]) FROM bi_test_tr END GO insert into bi_test_tr values (1,1,1); insert into bi_test_tr values (2,2,2); insert into bi_test_tr values (3,3,3); insert into bi_test_tr values (4,4,4); insert into bi_test_tr values (5,5,5); select * from test 实例的原因