尝试在空对象引用上调用虚拟方法

尝试在空对象引用上调用虚拟方法

本文介绍了尝试在空对象引用上调用虚拟方法“void android.widget.ListView.clearTextFilter()"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我在我的列表视图中制作搜索视图

Now im making searchview in my listview

这是我的代码

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 布局菜单

and this is the XMl Layout menu

<?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 中说:

and in logcat says :

尝试调用虚方法'voidandroid.widget.ListView.clearTextFilter()' 在空对象引用上

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

Can you tell me where i am wrong?

推荐答案

更改这一行:

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

这个:

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

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

you already declaring your listview instance as global and again you are defining it as a local variable that is why your global instance in null always.

这篇关于尝试在空对象引用上调用虚拟方法“void android.widget.ListView.clearTextFilter()"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:20