当我试图将gridview放入片段时,适配器中的getView()出错

时间:2014-03-30 02:42:37

标签: java android gridview android-fragments

我试图将已安装的应用列表显示为片段中的网格视图。

semms是代码没问题(也许)。但是当我试着测试它时。

在适配器类

中的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>

logcat的

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)

2 个答案:

答案 0 :(得分:3)

myPackageManager永远不会在您的适配器中初始化,因此当您将其传递给null

时,它是resolveInfo.loadIcon(myPackageManager)

答案 1 :(得分:2)

在Adapter类的构造函数中,执行以下操作:

PackageManager myPackageManager;

public AppDrawerAdapter(Context c, List<ResolveInfo> l){
    myContext = c;
    MyAppList = l;
    myPackageManager = c.getPackageManager();
}