我正在尝试将用户安装的Android应用程序的ListView显示在我的Android应用程序中的片段中。我使用自定义BaseAdapter填充其项目。
因为我正在使用片段,所以我遇到了问题。
我必须将我的Activity转换为Fragment。我成功地做到了,但之后我在Adapter类中遇到了运行时错误。
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;
}
}
这是我的Fragment类:
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是片段。
编辑:
以下是我更新的错误
答案 0 :(得分:4)
更改此
apkList.setAdapter(new ApkAdapter(this, packageList1, packageManager));
到
apkList.setAdapter(new ApkAdapter(getActivity(), packageList1, packageManager));
Fragment中的 this
没有引用有效的上下文。
另外
LayoutInflater inflater; // declare as instance variable
然后在构造函数
中public ApkAdapter(Context context, List<PackageInfo> packageList,
PackageManager packageManager) {
super();
inflater = LayoutInflater.from(context); // can initialize here
答案 1 :(得分:0)
您正试图从context
view
获取null
,这就是应用崩溃的原因。修改getView()
函数中的以下行将解决问题
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );