【问题标题】:NullPointerException in getView method for ApkAdapter class which extends BaseAdapter扩展 BaseAdapter 的 ApkAdapter 类的 getView 方法中的 NullPointerException
【发布时间】:2014-06-23 08:26:33
【问题描述】:

我正在尝试让用户安装的 android 应用程序的 ListView 显示在我的 android 应用程序的片段中。我正在使用自定义 BaseAdapter 填充其项目。

由于我使用的是片段,所以我遇到了问题。

我必须将我的 Activity 转换为 Fragment。我成功地做到了,但在那之后我的适配器类中出现了运行时错误。

06-23 00:40:53.824  23082-23082/com.spicycurryman.getdisciplined10.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.ibc.android.demo.appslist.app.ApkAdapter.getView(ApkAdapter.java:50)

LayoutInflator 是错误所在的第 50 行

LayoutInflater inflater = (LayoutInflater) convertView.getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );

我做了一些研究

NullPointerException in getView Of Adapter extends BaseAdapter

extends BaseAdapter appear NullPointerException

并试图遵循这些;但是,该解决方案仍然产生了错误。

我不确定为什么convertView == null 总是错误的。

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

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.spicycurryman.getdisciplined10.app.InstalledAppActivity;
import com.spicycurryman.getdisciplined10.app.R;

import java.util.List;

public class ApkAdapter extends BaseAdapter {

    List<PackageInfo> packageList;
    InstalledAppActivity context;
    PackageManager packageManager;

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

    private class ViewHolder {
        TextView apkName;
    }

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        // LayoutInflator is line 50 where is error is
        LayoutInflater inflater = (LayoutInflater) convertView.getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );

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

            holder.apkName = (TextView) convertView.findViewById(R.id.appname);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        PackageInfo packageInfo = (PackageInfo) getItem(position);
        Drawable appIcon = packageManager
                .getApplicationIcon(packageInfo.applicationInfo);
        String appName = packageManager.getApplicationLabel(
                packageInfo.applicationInfo).toString();
        appIcon.setBounds(0, 0, 65, 65);
        holder.apkName.setCompoundDrawables(appIcon, null, null, null);
        holder.apkName.setCompoundDrawablePadding(15);
        holder.apkName.setText(appName);

        return convertView;
    }
}

这是我的片段类:

package com.spicycurryman.getdisciplined10.app;

import android.support.v4.app.Fragment;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

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

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



public class InstalledAppActivity extends Fragment
        implements OnItemClickListener {

    PackageManager packageManager;
    ListView apkList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        View rootView = inflater.inflate(R.layout.installed_apps, container, false);
        packageManager = getActivity().getPackageManager();
        List<PackageInfo> packageList = packageManager
                .getInstalledPackages(PackageManager.GET_PERMISSIONS);

        List<PackageInfo> packageList1 = new ArrayList<PackageInfo>();

        /*To filter out System apps*/
        for(PackageInfo pi : packageList) {
            boolean b = isSystemPackage(pi);
            if(!b) {
                packageList1.add(pi);
            }
        }
        apkList = (ListView) rootView.findViewById(R.id.applist);
        apkList.setAdapter(new ApkAdapter(this, packageList1, packageManager));

        apkList.setOnItemClickListener(this);

        return rootView;

    }

    /**
     * Return whether the given PackgeInfo represents a system package or not.
     * User-installed packages (Market or otherwise) should not be denoted as
     * system packages.
     *
     * @param pkgInfo
     * @return boolean
     */
    private boolean isSystemPackage(PackageInfo pkgInfo) {
        return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true
                : false;
    }


// Don't need in Fragment
 /*   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.block, menu);
        return true;
    }*/

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }
}

请记住 InstalledAppActivity 是一个片段

编辑:

这是我更新的错误

【问题讨论】:

  • ApkAdapter.java:50 是什么??
  • 让我对此发表评论并明确说明我的错误
  • @XiJiaopin 你能指出第50行吗??

标签: java android listview android-fragments nullpointerexception


【解决方案1】:

改变这个

  apkList.setAdapter(new ApkAdapter(this, packageList1, packageManager));

 apkList.setAdapter(new ApkAdapter(getActivity(), packageList1, packageManager));

片段中的this 没有引用有效的上下文。

还有

LayoutInflater inflater; // declare as instance variable

然后在构造函数中

public ApkAdapter(Context context, List<PackageInfo> packageList,
                  PackageManager packageManager) {
    super();
    inflater = LayoutInflater.from(context); // can initialize here 

【讨论】:

  • apkList.setAdapter(new ApkAdapter(getActivity(), packageList1, packageManager));无法应用
  • @XiJiaopin 什么不能申请??
  • 无法应用context layoutinflater
  • 我仍然在 apkList.setAdapter(new ApkAdapter(getActivity(), packageList1, packageManager)); 中得到一个错误;
  • @XiJiaopin 这是一个错字它的LayoutInflater
【解决方案2】:

您试图从viewnull 获取context,这就是应用程序崩溃的原因。修改getView()函数中的以下行将解决问题

LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多