简体   繁体   English

为自定义BaseAdapter实现SearchView的NullPointer异常

[英]NullPointer Exception implementing SearchView for Custom BaseAdapter

I have a listview of installed apps with a custom baseadapter class. 我有一个带有自定义baseadapter类的已安装应用程序的列表视图。

在此处输入图片说明

I am attempting to add the searchview function when I press the search icon so an edittext pops up to filter which apps I want to search for. 当我按下搜索图标时,我试图添加searchview功能,这样会弹出一个编辑文本来过滤我要搜索的应用程序。

I am aiming for something like this: 我的目标是这样的: 在此处输入图片说明

I keep on producing a nullpointer exception for my searchview; 我继续为我的searchview产生一个nullpointer异常; however, I believe my xml and java implementation is correct. 但是,我相信我的xml和java实现是正确的。

I have done much research on the subject and still no luck. 我对该主题做了很多研究,但仍然没有运气。

NullPointer Exception While Doing Search Feature for ListView With BaseAdapter 使用BaseAdapter为ListView执行搜索功能时出现NullPointer异常

How to add a SearchWidget to the ActionBar? 如何将SearchWidget添加到ActionBar?

Android SearchView Filter ListView Android SearchView筛选器ListView

No matter what I do I produce a nullpointer exception in some way or another. 无论我做什么,都会以某种方式产生nullpointer异常。

This is where it is produced. 这是被产生。

SearchView searchView; //this is what is null. This is in my blockactivity fragment class.



    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

Here is my full baseadapter class: 这是我完整的基础适配器类:

package com.ibc.android.demo.appslist.app;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.Filterable;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Filter;
import com.spicycurryman.getdisciplined10.app.R;

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

//

public class ApkAdapter extends BaseAdapter implements Filterable {

    //Pastebin link:  http://pastebin.com/LGRicg4U , http://pastebin.com/c4WfmhMK , http://pastebin.com/gFuuM4dY, http://pastebin.com/4Q7EP9G4
    // http://pastebin.com/Te2g072w,  http://pastebin.com/NLT5iUiA ,

    SharedPreferences sharedPrefs;
    SharedPreferences sharedPrefsapp;

    List<PackageInfo> packageList;
    TextView appnamestyle;

    Activity context;
    PackageManager packageManager;
    boolean[] itemChecked;
    HashSet checked;
    Filter mFilter;

    String PACKAGE_NAME;

    TextView appname;

    public ApkAdapter(Activity context, List<PackageInfo> packageList,
                      PackageManager packageManager) {
        super();
        this.context = context;

        this.packageList = packageList;
        this.packageManager = packageManager;
        itemChecked = new boolean[packageList.size()];






    }
    private class ViewHolder {
        TextView apkName;
        CheckBox ck1;
        TextView packageName;



    }




    public int getCount() {
        return packageList.size();
    }

    public Object getItem(int position) {
        return packageList.get(position);
    }

    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        LayoutInflater inflater = context.getLayoutInflater();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.installed_apps, null);
            holder = new ViewHolder();

            holder.apkName = (TextView) convertView
                    .findViewById(R.id.appname);
            holder.apkName.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/raleway-medium.otf"));

            holder.ck1= (CheckBox)convertView
                    .findViewById(R.id.checkBox1);
            holder.packageName = (TextView) convertView.findViewById(R.id.app_package);



            convertView.setTag(holder);
            //holder.ck1.setTag(packageList.get(position));

        } else {

            holder = (ViewHolder) convertView.getTag();
        }









        // ViewHolder holder = (ViewHolder) convertView.getTag();
        final PackageInfo packageInfo = (PackageInfo) getItem(position);



        Drawable appIcon = packageManager
                .getApplicationIcon(packageInfo.applicationInfo);



        // Make sure to define it again!
        PACKAGE_NAME = packageInfo.packageName;


        final String appName = packageManager.getApplicationLabel(
                packageInfo.applicationInfo).toString();
        appIcon.setBounds(0, 0, 80, 80);
        holder.apkName.setCompoundDrawables(appIcon, null, null, null);
        holder.apkName.setCompoundDrawablePadding(15);
        holder.apkName.setText(appName);
        //holder.packageName.setText(PACKAGE_NAME);


        holder.ck1.setChecked(false);


        if (itemChecked[position])
            holder.ck1.setChecked(true);
        else
            holder.ck1.setChecked(false);




        // CHANGE UP EVERYTHING! MAKE THIS SHIT WORK, TIGGA!





        checked = new HashSet();

            PACKAGE_NAME = packageInfo.packageName;
            //Log.d("just here: ", PACKAGE_NAME);

            sharedPrefs = context.getSharedPreferences(context.getApplicationContext().getPackageName(), Context.MODE_PRIVATE);
            sharedPrefsapp = context.getSharedPreferences("appdb", Context.MODE_PRIVATE);





        holder.ck1.setChecked(sharedPrefs.getBoolean(PACKAGE_NAME,false));







        holder.ck1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {



                SharedPreferences.Editor editor = context.getSharedPreferences(context.getApplicationContext().getPackageName(), Context.MODE_PRIVATE).edit();
                SharedPreferences.Editor editorapp = context.getSharedPreferences("appdb", Context.MODE_PRIVATE).edit();

                if (holder.ck1.isChecked()) {

                    itemChecked[position] = true;
                    holder.ck1.setChecked(true);
                    editor.putBoolean(packageInfo.packageName, true);
                    editorapp.putString(packageInfo.packageName, packageInfo.packageName);



                    editor.apply();
                    editorapp.apply();

                   // sharedPrefs = context.getSharedPreferences(context.getApplicationContext().getPackageName(), Context.MODE_PRIVATE);


                } else {
                    itemChecked[position] = false;
                    holder.ck1.setChecked(false);
                    editor.putBoolean(packageInfo.packageName, false);
                    editorapp.remove(packageInfo.packageName);


                    editor.apply();
                    editorapp.apply();
                    //sharedPrefs = context.getSharedPreferences(context.getApplicationContext().getPackageName(), Context.MODE_PRIVATE);




                }

            }



        });





        return convertView;

    }


    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ItemsFilter();
        }
        return mFilter;
    }

    private class ItemsFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            // TODO Auto-generated method stub

            List<PackageInfo> packageList_2 = packageList;
            String prefixString = prefix.toString().toLowerCase();
            FilterResults results = new FilterResults();
            ArrayList<PackageInfo> FilteredList = new ArrayList<PackageInfo>();

            if (prefix == null || prefix.length() == 0) {
                results.values = packageList_2;
                results.count = packageList_2.size();
                return results;
            }
            for (int i = 0; i < packageList_2.size(); i++) {
                String filterText = prefix.toString().toLowerCase();
                try {
                    PackageInfo data = packageList_2.get(i);
                    if (data.applicationInfo
                            .loadLabel(context.getApplicationContext().getPackageManager())
                            .toString().toLowerCase().contains(filterText)) {
                        FilteredList.add(data);
                    } else if (data.packageName.contains(filterText)) {
                        FilteredList.add(data);
                    }
                } catch (Exception e) {
                    Toast.makeText(context.getApplicationContext(),
                            "exception e" + e.toString(),
                            Toast.LENGTH_SHORT).show();
                }
            }
            results.values = FilteredList;
            results.count = FilteredList.size();
            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            Toast.makeText(context.getApplicationContext(),"result-0 "+results.count,
                    Toast.LENGTH_SHORT).show();
            packageList = (List<PackageInfo>) results.values;
            notifyDataSetChanged();

        }
    }
}

Here is my blockactivity fragment class pay attention to the oncreateoptionmenu method: 这是我的块活动片段类,请注意oncreateoptionmenu方法:

package com.spicycurryman.getdisciplined10.app;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.app.SearchManager;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SearchView;

import com.ibc.android.demo.appslist.app.ApkAdapter;

import info.androidhive.tabsswipe.adapter.TabsPagerAdapter;


// Find out about the default file template warning



/**
 * Created by Spicycurryman on 6/17/14.
 */
public  class BlockActivity extends ActionBarActivity implements
        ActionBar.TabListener, SearchView.OnQueryTextListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
     ApkAdapter mAppAdapter;
    SearchManager searchManager;
    SearchView searchView;
    // Tab titles
    private String[] tabs = {"Installed Apps"};

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        super.onCreate(savedInstanceState);
        setTheme(R.style.Theme_Light_appalled);


        SpannableString s = new SpannableString("GetDisciplined");
        s.setSpan(new TypefaceSpan(this, "roboto-lightitalic.ttf.ttf"), 0, s.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

// Update the action bar title with the TypefaceSpan instance
        ActionBar actionBar = getActionBar();
        actionBar.setTitle(s);
        setContentView(R.layout.block_apps);


        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        final ActionBar finalActionBar = actionBar;
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                finalActionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();

        inflater.inflate(R.menu.block, menu);
        searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                if (!isLoading()) {
                    mAppAdapter.getFilter().filter(query);
                }
                return true;
            }

            private boolean isLoading() {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (!isLoading()) {
                    if (newText.equals("")) {
                        mAppAdapter.getFilter().filter("");
                    }
                }
                return true;
            }
        });
        return false;
    }




    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case android.R.id.home:
                onBackPressed();
                break;

            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }





    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        viewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }


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

    @Override
    public boolean onQueryTextChange(String s) {
        return false;
    }


}

Here is my block.menu xml file 这是我的block.menu xml文件

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context=".BlockActivity" >

<item android:id="@+id/action_search"
      android:title="@string/search"
      android:orderInCategory="100"
      android:icon="@drawable/ic_action_search"
      app:showAsAction="always|collapseActionView"
      android:actionViewClass="android.widget.SearchView"
      android:actionLayout="@layout/layout_search"
    />
</menu>

And here is my layout search: 这是我的布局搜索:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:cursorVisible="true"
    android:hint="@string/hint"
    android:imeOptions="actionDone"
    android:inputType="text"
    android:textColor="@android:color/white"
    android:textCursorDrawable="@android:color/white" />

Please let me know if I can clarify anything. 请让我知道是否可以澄清任何事情。 Had to include all this code for proper context. 为了适当的上下文,必须包括所有这些代码。 I am just trying to get my listview search filter working properly for my baseadapter listview but the nullpointer exception gets in my way persistently. 我只是想让我的listview搜索过滤器对于我的baseadapter listview正常工作,但nullpointer异常会一直困扰着我。

Now I am getting a null pointer exception here: 现在我在这里得到一个空指针异常:

Process: com.spicycurryman.getdisciplined10.app.dev, PID: 1453
    java.lang.NullPointerException
            at com.spicycurryman.getdisciplined10.app.BlockActivity$2.onQueryTextChange(BlockActivity.java:128)



public boolean onQueryTextSubmit(String query) {
                if (!isLoading()) {
                    mAppAdapter.getFilter().filter(query);  //over here
                }
                return true;
            }

Does anything know how to properly define mAppAdapter? 有什么知道如何正确定义mAppAdapter的吗?

Your problem: 你的问题:

you are using ActionBarActivity which is from support library but you use searchview that dose not from that library. 您正在使用来自支持库的ActionBarActivity ,但使用的搜索searchview并非来自该库。

Solution: 解:

 <menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context=".BlockActivity" >

<item android:id="@+id/action_search"
      android:title="@string/search"
      android:orderInCategory="100"
      android:icon="@drawable/ic_action_search"
      app:showAsAction="always|collapseActionView"
      app:actionViewClass="android.support.v7.widget.SearchView"
      android:actionLayout="@layout/layout_search"
    />
</menu>

and also change import android.widget.SearchView; 并更改import android.widget.SearchView; to import android.support.v7.widget.SearchView; import android.support.v7.widget.SearchView; . fix all import. 修复所有导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM