1

我在设置适配器的行(在 mGrid.setAdapter(new AppsAdapter()); 处收到空指针异常。据我所知,适配器中的所有内容都指向有效的东西。我正在尝试显示一个图标网格,它们是在应用程序抽屉(以及设置中的应用程序列表)中找到的应用程序图标。

这是片段的代码:

public class Tab3_fragment extends Fragment {

GridView mGrid;
private List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.tabfragment3, container, false);

}

@Override
public void onStart() {
    super.onStart();

    loadApps();
    mGrid = (GridView) getActivity().findViewById(R.id.myGrid);
    mGrid.setAdapter(new AppsAdapter());

}

@Override
public void onResume() {
    super.onResume();
    loadApps();
}

private void loadApps() {
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    mApps = getActivity().getPackageManager().queryIntentActivities(mainIntent, 0);
}

public class AppsAdapter extends BaseAdapter {
    public AppsAdapter() {
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i;

        if (convertView == null) {
            i = new ImageView(getActivity().getApplicationContext());
            i.setScaleType(ImageView.ScaleType.FIT_CENTER);
            i.setLayoutParams(new GridView.LayoutParams(50, 50));
        } else {
            i = (ImageView) convertView;
        }

        ResolveInfo info = mApps.get(position);
        i.setImageDrawable(info.activityInfo.loadIcon(getActivity().getPackageManager()));

        return i;
    }


    public final int getCount() {
        return mApps.size();
    }

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

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

这是布局的代码(包含在滚动视图中的“tab3fragment.xml”):

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/myGrid"
android:layout_width="match_parent" 
android:layout_height="match_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:columnWidth="60dp"
android:stretchMode="columnWidth"

android:gravity="center"
/>

当我滑动到第二个选项卡(3 个)时,它突然崩溃并给我这些错误:

04-27 21:31:51.337: D/libEGL(16874): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
04-27 21:31:51.369: D/libEGL(16874): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
04-27 21:31:51.369: D/libEGL(16874): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
04-27 21:31:51.627: D/OpenGLRenderer(16874): Enabling debug mode 0
04-27 21:31:51.634: I/Choreographer(16874): Skipped 110 frames!  The application may be doing too much work on its main thread.
04-27 21:32:22.994: D/AndroidRuntime(16874): Shutting down VM
04-27 21:32:22.994: W/dalvikvm(16874): threadid=1: thread exiting with uncaught exception (group=0x41c92930)
04-27 21:32:23.009: E/AndroidRuntime(16874): FATAL EXCEPTION: main
04-27 21:32:23.009: E/AndroidRuntime(16874): java.lang.NullPointerException
04-27 21:32:23.009: E/AndroidRuntime(16874):    at com.rdx.themetesterplus.Tab3_fragment.onStart(Tab3_fragment.java:36)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at android.support.v4.app.Fragment.performStart(Fragment.java:1484)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:807)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at android.support.v4.app.FragmentManagerImpl.startPendingDeferredFragments(FragmentManager.java:1112)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1461)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at android.support.v4.view.ViewPager$3.run(ViewPager.java:237)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at android.view.Choreographer.doFrame(Choreographer.java:531)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at android.os.Handler.handleCallback(Handler.java:725)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at android.os.Looper.loop(Looper.java:137)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at android.app.ActivityThread.main(ActivityThread.java:5226)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at java.lang.reflect.Method.invokeNative(Native Method)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at      java.lang.reflect.Method.invoke(Method.java:511)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
04-27 21:32:23.009: E/AndroidRuntime(16874):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

只是一个观察,不确定是否是问题,但根据我的理解 onStart() 和 onResume() 在 Fragment 1的生命周期开始时彼此紧随其后。我注意到 loadApps() 发生在每个。据我所知,loadApps() 函数在启动时被调用了两次。如果我错了,请纠正我,只是想我会提到它。

在首先设置视图之前,我不确定您是否可以引用 mGrid。通常,一旦设置了视图,就可以使用 findViewByID() 方法。

源码 片段生命周期源码

于 2013-04-28T06:42:41.393 回答
0

试着放

loadApps();
mGrid = (GridView) getActivity().findViewById(R.id.myGrid);
mGrid.setAdapter(new AppsAdapter());

您在 onActivityCreated() 方法而不是 onStart() 中的数据设置代码。这应该可以解决您的问题。让我知道您是否仍然遇到相同的问题。

于 2013-04-28T06:53:36.057 回答