我试图在一个片段中显示安装的应用程序列表作为gridview。
似乎代码是可以的(也许)。但当我试着测试的时候。
它在适配器类中的getview()中出错
我该怎么做才能解决这个问题?
appdrawerfragment.java
public class AppDrawerFragment extends Fragment {
private GridView mGridAppInfo;
public AppDrawerFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Inflate Layout
View view = inflater.inflate(R.layout.fragment_app_drawer,container,false);
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> intentList = getActivity().getPackageManager().queryIntentActivities(intent, 0);
// load list application
mGridAppInfo = (GridView)view.findViewById(R.id.gridView);
// create new adapter
AppDrawerAdapter adapter = new AppDrawerAdapter(getActivity(), intentList); /*need fix & test*/
// set adapter to list view
mGridAppInfo.setAdapter(adapter);
// Inflate the layout for this fragment
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
}
appdraweradapter.java
public class AppDrawerAdapter extends BaseAdapter {
private Context myContext;
private List<ResolveInfo> MyAppList;
PackageManager myPackageManager;
public AppDrawerAdapter(Context c, List<ResolveInfo> l){
myContext = c;
MyAppList = l;
}
@Override
public int getCount() {
return MyAppList.size();
}
@Override
public Object getItem(int position) {
return MyAppList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// reference to convertView
View v = convertView;
// inflate new layout if null
if(v == null) {
LayoutInflater inflater = LayoutInflater.from(myContext);
v = inflater.inflate(R.layout.appdrawer, parent, false);
}
// load controls from layout resources
ImageView ivAppIcon = (ImageView)v.findViewById(R.id.icon);
TextView tvAppName = (TextView)v.findViewById(R.id.appname);
// TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);
// get the selected entry
ResolveInfo resolveInfo = MyAppList.get(position);
// set data to display
ivAppIcon.setImageDrawable(resolveInfo.loadIcon(myPackageManager));
tvAppName.setText(resolveInfo.loadLabel(myPackageManager));
// tvPkgName.setText(entry.packageName);
// return view
return v;
}
}
*fragment_app_drawer.xml文件*
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#30000000">
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gridView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
appdrawer.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/icon"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="App Name"
android:id="@+id/appname"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"/>
</LinearLayout>
洛卡特
03-30 10:28:26.322 13824-13824/com.hybdms.timeliness E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.hybdms.timeliness, PID: 13824
java.lang.NullPointerException
at android.content.pm.PackageItemInfo.loadIcon(PackageItemInfo.java:140)
at android.content.pm.ComponentInfo.loadDefaultIcon(ComponentInfo.java:172)
at android.content.pm.PackageItemInfo.loadIcon(PackageItemInfo.java:145)
at android.content.pm.ResolveInfo.loadIcon(ResolveInfo.java:205)
at com.hybdms.timeliness.workspace.AppDrawerAdapter.getView(AppDrawerAdapter.java:64)
at android.widget.AbsListView.obtainView(AbsListView.java:2263)
at android.widget.GridView.onMeasure(GridView.java:1044)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1451)
at android.view.View.measure(View.java:16497)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
at dalvik.system.NativeStart.main(Native Method)
最佳答案
在适配器类的构造函数中,执行以下操作:
PackageManager myPackageManager;
public AppDrawerAdapter(Context c, List<ResolveInfo> l){
myContext = c;
MyAppList = l;
myPackageManager = c.getPackageManager();
}