从实现Filterable的java类通过intent传递数据时显示错误

时间:2017-07-26 11:12:56

标签: java android nullpointerexception

这是我的完整java类代码。 它显示空指针异常。我还添加了logcat屏幕截图。我试图解决这个问题,但没有办法。如果可能,请帮助我。

public class AppAdapter extends RecyclerView.Adapter<AppAdapter.AppViewHolder> implements Filterable {

// Load Settings
private AppPreferences appPreferences;


// AppAdapter variables
private List<AppInfo> appList;
private List<AppInfo> appListSearch;
private Context context;


public AppAdapter(List<AppInfo> appList, Context context) {
    this.appList = appList;
    this.context = context;
    this.appPreferences = SystemInfoManager.getAppPreferences();

}

@Override
public int getItemCount() {
    return appList.size();

}

public void clear() {
    appList.clear();
    notifyDataSetChanged();
}

@Override
public void onBindViewHolder(AppViewHolder appViewHolder, int i) {
    AppInfo appInfo = appList.get(i);
    appViewHolder.vName.setText(appInfo.getName());
    appViewHolder.vApk.setText(appInfo.getAPK());
    appViewHolder.vIcon.setImageDrawable(appInfo.getIcon());

    setButtonEvents(appViewHolder, appInfo);

}


private void setButtonEvents(AppViewHolder appViewHolder, final AppInfo appInfo) {
    ButtonFlat appAbout = appViewHolder.vAbout;
    ButtonFlat appShare = appViewHolder.vShare;
    final ImageView appIcon = appViewHolder.vIcon;
    final CardView cardView = appViewHolder.vCard;

    appAbout.setBackgroundColor(Color.BLUE);
    appShare.setBackgroundColor(Color.BLUE);

    appAbout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Activity activity = (Activity) context;

            Intent intent = new Intent(context, AppActivity.class);
            intent.putExtra("app_name", appInfo.getName());
            intent.putExtra("app_apk", appInfo.getAPK());
            intent.putExtra("app_version", appInfo.getVersion());
            intent.putExtra("app_source", appInfo.getSource());
            intent.putExtra("app_data", appInfo.getData());
            Bitmap bitmap = ((BitmapDrawable) appInfo.getIcon()).getBitmap();
            intent.putExtra("app_icon", bitmap);
            intent.putExtra("app_isSystem", appInfo.isSystem());

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                String transitionName = context.getResources().getString(R.string.transition_app_icon);

                ActivityOptions transitionActivityOptions = ActivityOptions.makeSceneTransitionAnimation(activity, appIcon, transitionName);
                context.startActivity(intent, transitionActivityOptions.toBundle());
            } else {
                context.startActivity(intent);
                activity.overridePendingTransition(R.anim.slide_in_right, R.anim.fade_back);
            }
        }
    });


    appShare.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            UtilsApp.copyFile(appInfo);
            Intent shareIntent = UtilsApp.getShareIntent(UtilsApp.getOutputFilename(appInfo));
            context.startActivity(Intent.createChooser(shareIntent,
                    String.format(context.getResources().getString(R.string.send_to), appInfo.getName())));
        }
    });


    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Activity activity = (Activity) context;


            Intent intent = new Intent(context, AppActivity.class);
            intent.putExtra("app_name", appInfo.getName());
            intent.putExtra("app_apk", appInfo.getAPK());
            intent.putExtra("app_version", appInfo.getVersion());
            intent.putExtra("app_source", appInfo.getSource());
            intent.putExtra("app_data", appInfo.getData());
            Bitmap bitmap = ((BitmapDrawable) appInfo.getIcon()).getBitmap();
            intent.putExtra("app_icon", bitmap);
            intent.putExtra("app_isSystem", appInfo.isSystem());

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                String transitionName = context.getResources().getString(R.string.transition_app_icon);

                ActivityOptions transitionActivityOptions = ActivityOptions.makeSceneTransitionAnimation(activity, appIcon, transitionName);
                context.startActivity(intent, transitionActivityOptions.toBundle());
            } else {
                context.startActivity(intent);
                activity.overridePendingTransition(R.anim.slide_in_right, R.anim.fade_back);
            }
        }
    });


}

public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            final FilterResults oReturn = new FilterResults();
            final List<AppInfo> results = new ArrayList<>();
            if (appListSearch == null) {
                appListSearch = appList;
            }
            if (charSequence != null) {
                if (appListSearch != null && appListSearch.size() > 0) {
                    for (final AppInfo appInfo : appListSearch) {
                        if (appInfo.getName().toLowerCase().contains(charSequence.toString())) {
                            results.add(appInfo);
                        }
                    }
                }
                oReturn.values = results;
                oReturn.count = results.size();
            }
            return oReturn;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            if (filterResults.count > 0) {
                InstalledAppsFragment.setResultsMessage(false);
            } else {
                InstalledAppsFragment.setResultsMessage(true);
            }
            appList = (ArrayList<AppInfo>) filterResults.values;
            notifyDataSetChanged();
        }
    };
}


@Override
public AppViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View appAdapterView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.app_layout, viewGroup, false);
    return new AppViewHolder(appAdapterView);
}

public static class AppViewHolder extends RecyclerView.ViewHolder {
    protected TextView vName;
    protected TextView vApk;
    protected ImageView vIcon;
    protected ButtonFlat vAbout;
    protected ButtonFlat vShare;
    protected CardView vCard;

    public AppViewHolder(View v) {
        super(v);
        vName = (TextView) v.findViewById(R.id.txtName);
        vApk = (TextView) v.findViewById(R.id.txtApk);
        vIcon = (ImageView) v.findViewById(R.id.imgIcon);
        vAbout = (ButtonFlat) v.findViewById(R.id.btnExtract);
        vShare = (ButtonFlat) v.findViewById(R.id.btnShare);
        vCard = (CardView) v.findViewById(R.id.app_card);

    }
}

}

这是logcat消息:

https://i.stack.imgur.com/vVOr7.jpg

如果可能,请给我一些解决方案。 我使用了context.getBaseContext()但它也给出了错误。

2 个答案:

答案 0 :(得分:0)

使用getActiviy()

而不是将上下文解析为活动
Intent intent = new Intent(getActivity(), AppActivity.class);

答案 1 :(得分:0)

尝试更改此行 代替

Intent intent = new Intent(context, AppActivity.class);

Intent intent = new Intent(YourActivityName.this, AppActivity.class);