0

我刚刚在我的 android 小部件中添加了一个搜索功能,以搜索用户安装的应用程序列表。我的应用程序安装得很好,但是当我去搜索应用程序强制关闭的东西时,我收到了这个错误:

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,所以我可能在某个地方搞砸了:

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:

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;
        }


    });
}
}

附加课程:这是我的实用程序课程:

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;
}
}
4

3 回答 3

1

在 for 循环之前添加此代码:

if (constraint.length() == 0) {
myFilteredAppList.addAll(originalListAppInfo);
} 

IE

@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();

                     if (constraint.length() == 0) {
                         myFilteredAppList.addAll(originalListAppInfo);
                        } 

                    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;
    }
于 2013-10-18T17:41:14.403 回答
1

似乎mListAppInfo是空的

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

这让我假设由于某种原因您getInstalledApplications在实例化适配器时调用返回 null。

于 2013-10-18T20:31:44.303 回答
1

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

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

你的 for 循环应该是这样的

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

这应该有效。并且永远不要忘记检查publishResults方法中的空值

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

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

}
于 2013-10-19T03:57:19.870 回答