Java 尝试调用虚拟方法';void android.widget.ListView.clearTextFilter()';关于空对象引用

Java 尝试调用虚拟方法';void android.widget.ListView.clearTextFilter()';关于空对象引用,java,android,searchview,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; i

现在我在我的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;
    }
}
包intikom.streammobile.Customer;
导入android.app.ProgressDialog;
导入android.app.SearchManager;
导入android.content.Context;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.SearchView;
导入android.text.TextUtils;
导入android.view.KeyEvent;
导入android.view.Menu;
导入android.view.MenuInflater;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ListView;
导入android.widget.Toast;
导入org.json.simple.JSONArray;
导入org.json.simple.JSONObject;
导入org.json.simple.parser.JSONParser;
导入org.json.simple.parser.ParseException;
导入java.util.ArrayList;
导入java.util.List;
进口intikom.streammobile.Index;
进口intikom.streammobile.R;
导入intikom.streammobile.Utility.bigprocess;
导入intikom.streammobile.Utility.RESTClient;
导入intikom.streammobile.Workflow.MyRequest.ListRequestItem;
导入intikom.streammobile.Workflow.WorkflowActivity;
公共类ListCustomer扩展AppCompatActivity实现SearchView.OnQueryTextListener{
私有静态最终字符串ARG\u SECTION\u NUMBER=“SECTION\u NUMBER”;
私有静态字符串服务;
私有静态字符串行页面;
私有静态ListView lv;
私有静态SharedReferences优先;
私人名单;
私人名单2;
私有ListCustomerItem项目;
私有静态字符串customerID;
公共列表客户(){
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u list\u customer);
URLService=getString(R.string.URLService);
最终ListView lv=(ListView)findViewById(R.id.lvCustomerList);
lv.setTextFilterEnabled(真);
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
item=新的ListCustomerItem();
item=mItems.get(位置);
SharedPreferences pref=getApplicationContext().getSharedPreferences(“MyPref”,MODE_PRIVATE);
SharedReferences.Editor=pref.edit();
editor.putString(“CustomerID”,item.CustID);
editor.putString(“CustomerName”,item.CustName);
commit();
Intent i=新的Intent(getApplicationContext(),WorkflowActivity.class);
i、 putExtra(“客户名称”,item.CustName);
setResult(1000,i);
完成();
}
});
getCustomer();
getLocal();
if(mItems==null)
{
if(mItems2==null){
Toast.makeText(此“Internet连接似乎处于脱机状态”,
Toast.LENGTH_LONG).show();
}
其他的
{
lv.setAdapter(新的ListCustomerAdapter(this,mItems2));
}
}
其他的
{
lv.setAdapter(新的ListCustomerAdapter(this,mItems));
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.searchbox,菜单);
搜索管理器搜索管理器=
(SearchManager)getSystemService(Context.SEARCH\u服务);
搜索视图搜索视图=
(SearchView)menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
getSearchableInfo(getComponentName());
searchView.setSubmitButtonEnabled(true);
setOnQueryTextListener(这个);
返回true;
}
@凌驾
公共布尔值onQueryTextSubmit(字符串查询){
返回false;
}
@凌驾
公共布尔onQueryTextChange(字符串newText){
if(TextUtils.isEmpty(newText))
{
lv.clearTextFilter();
}
其他的
{
lv.setFilterText(newText.toString());
}
返回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中说:

尝试调用虚拟方法“void” 空对象引用上的android.widget.ListView.clearTextFilter()

你能告诉我哪里错了吗?

换行:

为此:

您已经将
listview
实例声明为
global
,并且再次将其定义为
local
变量,这就是为什么
global
实例总是在
null
中的原因

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