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

查看:20
本文介绍了使用 BaseAdapter 为 ListView 执行搜索功能时出现 NullPointer 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚为我的 android 小部件添加了一个搜索功能,以搜索用户安装的应用程序列表.我的应用程序安装正常,一切正常,但是当我去搜索某些内容时,应用程序强制关闭并出现此错误:

I just finished adding a search feature to my android widget to search through a list of the users installed applications. My app installs fine and everything but when I go to search something the app force closes and I get this error:

10-18 11:10:49.393: E/AndroidRuntime(10901): FATAL EXCEPTION: main
10-18 11:10:49.393: E/AndroidRuntime(10901): java.lang.NullPointerException
10-18 11:10:49.393: E/AndroidRuntime(10901):    at com.example.awesomefilebuilderwidget.AppInfoAdapter.getCount(AppInfoAdapter.java:33)
10-18 11:10:49.393: E/AndroidRuntime(10901):    at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:778)
10-18 11:10:49.393: E/AndroidRuntime(10901):    at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:31)
10-18 11:10:49.393: E/AndroidRuntime(10901):    at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
10-18 11:10:49.393: E/AndroidRuntime(10901):    at com.example.awesomefilebuilderwidget.AppInfoAdapter$1.publishResults(AppInfoAdapter.java:98)
10-18 11:10:49.393: E/AndroidRuntime(10901):    at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
10-18 11:10:49.393: E/AndroidRuntime(10901):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 11:10:49.393: E/AndroidRuntime(10901):    at android.os.Looper.loop(Looper.java:150)
10-18 11:10:49.393: E/AndroidRuntime(10901):    at android.app.ActivityThread.main(ActivityThread.java:4333)
10-18 11:10:49.393: E/AndroidRuntime(10901):    at java.lang.reflect.Method.invokeNative(Native Method)
10-18 11:10:49.393: E/AndroidRuntime(10901):    at java.lang.reflect.Method.invoke(Method.java:507)
10-18 11:10:49.393: E/AndroidRuntime(10901):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-18 11:10:49.393: E/AndroidRuntime(10901):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-18 11:10:49.393: E/AndroidRuntime(10901):    at dalvik.system.NativeStart.main(Native Method)

这是我的 AppInfoAdapter,所以我可能在某处搞砸了:

Here is my AppInfoAdapter so I might have messed up somewhere along the line:

package com.example.awesomefilebuilderwidget;

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

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

public class AppInfoAdapter extends BaseAdapter implements Filterable {
private Context mContext;
private List<ApplicationInfo> mListAppInfo;
private PackageManager mPackManager;
private List<ApplicationInfo> originalListAppInfo;
private Filter filter;

public AppInfoAdapter(Context c, List<ApplicationInfo> listApp, PackageManager pm) {
    mContext = c;
    this.originalListAppInfo = this.mListAppInfo = listApp;
    mPackManager = pm;
    }

@Override
public int getCount() {
    return mListAppInfo.size();
}

@Override
public Object getItem(int position) {
    return mListAppInfo.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // get the selected entry
    ApplicationInfo entry = (ApplicationInfo) mListAppInfo.get(position);

    // reference to convertView
    View v = convertView;

    // inflate new layout if null
    if(v == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        v = inflater.inflate(R.layout.layout_appinfo, null);
    }

    // load controls from layout resources
    ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
    TextView tvAppName = (TextView)v.findViewById(R.id.tvName);
    TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);

    // set data to display
    ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
    tvAppName.setText(entry.loadLabel(mPackManager));
    tvPkgName.setText(entry.packageName);

    // return view
    return v;
}
@Override
public Filter getFilter() {
    if(filter == null) {
        filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                List<ApplicationInfo> myFilteredAppList = new ArrayList<ApplicationInfo>();
                constraint = constraint.toString().toLowerCase();

                for (ApplicationInfo appInfo : originalListAppInfo) {
                    String somefield = appInfo.name;
                    if (somefield.toLowerCase().contains(constraint.toString())) {
                        myFilteredAppList.add(appInfo);
                    }
                }
                results.count = myFilteredAppList.size();
                results.values = myFilteredAppList;
                return results;
            }


            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                mListAppInfo = (List<ApplicationInfo>)results.values;
                notifyDataSetChanged();
            }
        };
    }
    return filter;
}

}

有什么问题吗?

添加:这是我的 Drag_and_Drop_App.java:

ADDED: Here is my Drag_and_Drop_App.java:

package com.example.awesomefilebuilderwidget;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class Drag_and_Drop_App extends Activity {
private ListView mListAppInfo;
// Search EditText
EditText inputSearch;
public static AppInfoAdapter adapter;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // set layout for the main screen
    setContentView(R.layout.drag_and_drop_app);
    // import buttons
    Button btnLinkToFeedback = (Button) findViewById(R.id.btnLinkToFeedback);

    // Link to Feedback Screen
    btnLinkToFeedback.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(),
                    Feedback.class);
            startActivity(i);
            finish();
        }
    });
    // create new adapter
    adapter = new AppInfoAdapter(this, (List<ApplicationInfo>) Utilities.getInstalledApplication(this), getPackageManager());
    // load list application
   mListAppInfo = (ListView)findViewById(R.id.lvApps);
    // set adapter to list view
    mListAppInfo.setAdapter(adapter);
    // search bar
    inputSearch = (EditText) findViewById(R.id.inputSearch);

    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            // Drag_and_Drop_App.this.adapter.getFilter().filter(cs);  
             if (Drag_and_Drop_App.this.adapter == null){
                 // some print statement saying it is null
                 Log.d ("msg_error", "adapter_is_null");
             }
                Drag_and_Drop_App.this.adapter.getFilter().filter(cs);

            }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub                          
        }
        });


    // implement event when an item on list view is selected
    mListAppInfo.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
            // get the list adapter
            AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
            // get selected item on the list
            ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
            // launch the selected application
            Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
        }

    });

    // implement event when an item on list view is selected via long-click for drag and drop
    mListAppInfo.setOnItemLongClickListener(new OnItemLongClickListener(){

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                int pos, long id) {
            // TODO Auto-generated method stub
            // get the list adapter
            AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
            // get selected item on the list
            ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
            // launch the selected application
            Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
            return true;
        }


    });
}
}

添加类:这是我的实用程序类:

ADDED CLASS: Here is my Utilities class:

package com.example.awesomefilebuilderwidget;

import java.util.List;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.widget.Toast;

public class Utilities {

/*
 * Get all installed application on mobile and return a list
 * @param   c   Context of application
 * @return  list of installed applications
 */
public static List<?> getInstalledApplication(Context c) {
    return c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
}

/*
 * Launch an application
 * @param   c   Context of application
 * @param   pm  the related package manager of the context
 * @param   pkgName Name of the package to run
 */
public static boolean launchApp(Context c, PackageManager pm, String pkgName) {
    // query the intent for lauching
    Intent intent = pm.getLaunchIntentForPackage(pkgName);
    // if intent is available
    if(intent != null) {
        try {
            // launch application
            c.startActivity(intent);
            // if succeed
            return true;

        // if fail
        } catch(ActivityNotFoundException ex) {
            // quick message notification
            Toast toast = Toast.makeText(c, "Application Not Found", Toast.LENGTH_LONG);
            // display message
            toast.show();
        }
    }
    // by default, fail to launch
    return false;
}
}

推荐答案

您的列表包含 appInfo.packageName 而不是 appInfo.name.

Your list contains appInfo.packageName not appInfo.name.

在 performFiltering 方法中,您将 appInfo.name 添加到过滤列表.相反,您应该添加 appInfo.packageName.

In performFiltering method you are adding appInfo.name to filtered list. Instead you should add appInfo.packageName.

你的 for 循环应该是这样的

Your for loop should be like this

 for (ApplicationInfo appInfo : originalListAppInfo) {
        String somefield = appInfo.packageName;
        if (somefield.toLowerCase().contains(constraint.toString())) {
              myFilteredAppList.add(appInfo);
        }
  }

这应该有效.永远不要忘记在 publishResults 方法

This should work. And never forget to check for null values in publishResults method

 @Override
protected void publishResults(CharSequence constraint, FilterResults results) {

  if(results.values != null)
  {
    mListAppInfo = (List<ApplicationInfo>)results.values;
    notifyDataSetChanged();
  }

}

这篇关于使用 BaseAdapter 为 ListView 执行搜索功能时出现 NullPointer 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆