尝试在空对象引用上调用虚拟方法 'void android.widget.ListView.clearTextFilter()'
Attempt to invoke virtual method 'void android.widget.ListView.clearTextFilter()' on a null object reference
现在我正在我的列表视图中创建搜索视图
这是我的代码
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 中说:
Attempt to invoke virtual method 'void
android.widget.ListView.clearTextFilter()' on a null object reference
你能告诉我哪里错了吗?
更改此行:
final ListView lv = (ListView)findViewById(R.id.lvCustomerList);
用这个:
lv = (ListView)findViewById(R.id.lvCustomerList);
您已经将您的 listview
实例声明为 global
,并且您再次将其定义为 local
变量,这就是为什么您的 global
实例在 null
总是。
现在我正在我的列表视图中创建搜索视图
这是我的代码
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 中说:
Attempt to invoke virtual method 'void android.widget.ListView.clearTextFilter()' on a null object reference
你能告诉我哪里错了吗?
更改此行:
final ListView lv = (ListView)findViewById(R.id.lvCustomerList);
用这个:
lv = (ListView)findViewById(R.id.lvCustomerList);
您已经将您的 listview
实例声明为 global
,并且您再次将其定义为 local
变量,这就是为什么您的 global
实例在 null
总是。