Android:尝试调用虚方法' void android.widget.GridView.setAdapter(android.widget.ListAdapter)'在null对象引用上

时间:2017-01-07 15:43:27

标签: android android-layout android-studio gridview

我按照教程列出了所有已安装的软件包"应用程序"在GridView布局中。到目前为止,这导致了致命的错误。我已经尝试了许多其他的代码变体,但也收到了相同的错误消息。我很抱歉,如果问题看起来太明显,或者我犯了一个非常愚蠢的错误,因为我刚刚在1周前启动Android并且无法找出问题的根源。任何帮助都非常感谢。

Launcher.java

public class Launcher extends FragmentActivity {

    DrawerAdapter drawerAdapterObject;
    GridView drawerGrid;

    class Package {
        Drawable icon;
        String name;
        String label;
    }

    Package[] packs;
    PackageManager pm;
    ViewPager viewpager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
        drawerGrid = (GridView) findViewById(R.id.content);
        pm=getPackageManager();
        set_packs();
        drawerAdapterObject = new DrawerAdapter(this, packs);
        drawerGrid.setAdapter(drawerAdapterObject);

        viewpager = (ViewPager) findViewById(R.id.pager);
        PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(adapter);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window w = getWindow();
            w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
    }

    public void set_packs() {
        final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> packsList = pm.queryIntentActivities(mainIntent, 0);
        packs = new Package[packsList.size()];
        for(int I=0;I<packsList.size();I++){
            packs[I]=  new Package();
            packs[I].icon=packsList.get(I).loadIcon(pm);
            packs[I].name=packsList.get(I).activityInfo.packageName;
            packs[I].label=packsList.get(I).loadLabel(pm).toString();
        }
    }
}

DrawerAdapter.java

public class DrawerAdapter extends BaseAdapter {
    Context mContext;
    Launcher.Package[] packsForAdapter;

    public DrawerAdapter (Context c, Launcher.Package packs[]) {
        mContext =c;
        packsForAdapter = packs;
    }

    @Override
    public int getCount() {
        return packsForAdapter.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageDrawable(packsForAdapter[pos].icon);
        imageView.setLayoutParams(new GridView.LayoutParams(65, 65));
        imageView.setPadding(3,3,3,3);
        return imageView;
    }
}

activity_apps.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_apps"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    tools:context="com.visualartsinternational.www.artui.Launcher">

<GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/content"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="50dp"
        android:horizontalSpacing="50dp"
        android:stretchMode="columnWidth"
        android:gravity="center"/>

</RelativeLayout>

activity_launcher.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_launcher"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.visualartsinternational.www.artui.Launcher">

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>

</RelativeLayout>

logcat的

01-07 15:10:28.023 14259-14259/? I/art: Not late-enabling -Xcheck:jni (already on)
01-07 15:10:28.023 14259-14259/? W/art: Unexpected CPU variant for X86 using defaults: x86
01-07 15:10:28.039 14259-14265/? I/art: Debugger is no longer active
01-07 15:10:28.040 14259-14265/? I/art: Starting a blocking GC Instrumentation
01-07 15:10:28.266 14259-14259/? W/ActivityThread: Application com.visualartsinternational.www.artui is waiting for the debugger on port 8100...
01-07 15:10:28.267 14259-14259/? I/System.out: Sending WAIT chunk
01-07 15:10:29.249 14259-14265/com.visualartsinternational.www.artui I/art: Debugger is active
01-07 15:10:29.272 14259-14259/com.visualartsinternational.www.artui I/System.out: Debugger has connected
01-07 15:10:29.272 14259-14259/com.visualartsinternational.www.artui I/System.out: waiting for debugger to settle...
01-07 15:10:29.474 14259-14259/com.visualartsinternational.www.artui I/System.out: waiting for debugger to settle...
01-07 15:10:29.674 14259-14259/com.visualartsinternational.www.artui I/System.out: waiting for debugger to settle...
01-07 15:10:29.876 14259-14259/com.visualartsinternational.www.artui I/System.out: waiting for debugger to settle...
01-07 15:10:30.078 14259-14259/com.visualartsinternational.www.artui I/System.out: waiting for debugger to settle...
01-07 15:10:30.279 14259-14259/com.visualartsinternational.www.artui I/System.out: waiting for debugger to settle...
01-07 15:10:30.480 14259-14259/com.visualartsinternational.www.artui I/System.out: waiting for debugger to settle...
01-07 15:10:30.681 14259-14259/com.visualartsinternational.www.artui I/System.out: waiting for debugger to settle...
01-07 15:10:30.882 14259-14259/com.visualartsinternational.www.artui I/System.out: debugger has settled (1463)
01-07 15:10:30.886 14259-14259/com.visualartsinternational.www.artui W/System: ClassLoader referenced unknown path: /data/app/com.visualartsinternational.www.artui-1/lib/x86
01-07 15:10:30.894 14259-14259/com.visualartsinternational.www.artui I/InstantRun: Instant Run Runtime started. Android package is com.visualartsinternational.www.artui, real application class is null.
01-07 15:10:31.061 14259-14259/com.visualartsinternational.www.artui W/System: ClassLoader referenced unknown path: /data/app/com.visualartsinternational.www.artui-1/lib/x86
01-07 15:10:31.245 14259-14259/com.visualartsinternational.www.artui W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-07 15:10:31.467 14259-14263/com.visualartsinternational.www.artui I/art: Do partial code cache collection, code=30KB, data=26KB
01-07 15:10:31.470 14259-14263/com.visualartsinternational.www.artui I/art: After code cache collection, code=28KB, data=27KB
01-07 15:10:31.470 14259-14263/com.visualartsinternational.www.artui I/art: Increasing code cache capacity to 128KB
01-07 15:10:31.509 14259-14259/com.visualartsinternational.www.artui D/AndroidRuntime: Shutting down VM
01-07 15:10:31.510 14259-14259/com.visualartsinternational.www.artui E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                       Process: com.visualartsinternational.www.artui, PID: 14259
                                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.visualartsinternational.www.artui/com.visualartsinternational.www.artui.Launcher}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference
                                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                                        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference
                                                                                           at com.visualartsinternational.www.artui.Launcher.onCreate(Launcher.java:36)
                                                                                           at android.app.Activity.performCreate(Activity.java:6679)
                                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                           at android.os.Looper.loop(Looper.java:154) 
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

1 个答案:

答案 0 :(得分:1)

活动使用的布局与activity_apps.xml不同 你正在做setContentView(R.layout.activity_launcher)

将其替换为setContentView(R.layout.activity_apps)或将GridView添加到activity_launcher.xml