Android 使用 GridView 和 BaseAdapter 的应用程序

Android application using GridView and BaseAdapter

我正在尝试 运行 我的 emulator/device 上的 Android 应用程序。

没有编译时错误,但是当 device/emulator 和

上的 运行 时应用程序崩溃

"Unfortunately, GridViewMapExample has stopped" 屏幕上显示的消息 ID。

MainActivity.java

package com.example.gridviewmapexample;

import java.util.ArrayList;

import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

    GridView grid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        grid = (GridView) findViewById(R.id.gridView);// step 3
        grid.setAdapter(new NikAdapter(this));

    }

}

class Country {
    String countryName;
    int imageId;

    Country(String countryName, int imageId) {
        this.countryName = countryName;
        this.imageId = imageId;

    }
}

class NikAdapter extends BaseAdapter {
    ArrayList<Country> list;
    Context c;

    NikAdapter(Context c) {
        this.c = c;
        list = new ArrayList<Country>();
        Resources res = c.getResources();
        String[] tempCountryNames = res.getStringArray(R.array.country_names);
        int[] countryImages = { R.drawable.germany_flag, R.drawable.india_flag,
                R.drawable.italy_flag, R.drawable.norway_flag,
                R.drawable.pakistan_flag, R.drawable.saudi_arabia_flag };
        for (int i = 0; i < 6; i++) {
            Country tempCountry = new Country(tempCountryNames[i],
                    countryImages[i]);
            list.add(tempCountry);
        }
    }

    @Override
    public int getCount() {

        return list.size();
    }

    @Override
    public Object getItem(int position) {

        return list.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    class ViewHolder {
        ImageView myImageView;

        ViewHolder(View v) {
            myImageView = (ImageView) v.findViewById(R.id.imageView);
        }
    }

    @Override
    public View getView(int i, View view, ViewGroup parent) {
        View row = view;
        ViewHolder holder = null;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) c
                    .getSystemService(Context.LAUNCHER_APPS_SERVICE);
            row = inflater.inflate(R.layout.single_item, parent, false);
            holder = new ViewHolder(row);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }
        Country temp = list.get(i);
        holder.myImageView.setImageResource(temp.imageId);
        holder.myImageView.setTag(temp);
        return row;
    }

}

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">GridViewMapExample</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>

     <string-array name="country_names">
        <item>Germany</item>
        <item>India</item>
        <item>Italy</item>
        <item>Norway</item>
        <item>Pakistan</item>
        <item>Saudi Arabia </item>
    </string-array>

</resources>        

single_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/india_flag" />

</RelativeLayout>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.gridviewmapexample.MainActivity" >

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:columnWidth="120dp"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="spacingWidthUniform"
        android:verticalSpacing="10dp" >
    </GridView>

</RelativeLayout>   

我得到以下堆栈跟踪,现在搜索了一段时间后,我仍然不知道是什么原因造成的。

        04-26 18:09:01.944: D/ServiceManager(22760): The name of the service is display
        04-26 18:09:01.946: D/ServiceManager(22760): The name of the service is dropbox
        04-26 18:09:01.949: D/jdwp(22760): sendBufferedRequest : len=0x57
        04-26 18:09:01.952: W/asset(22760): AssetManager-->addDefaultAssets CIP path not exsit!
        04-26 18:09:01.954: D/ServiceManager(22760): The name of the service is package
        04-26 18:09:01.962: D/ServiceManager(22760): The name of the service is connectivity
        04-26 18:09:02.031: D/ServiceManager(22760): The name of the service is window
        04-26 18:09:02.033: D/ServiceManager(22760): The name of the service is accessibility
        04-26 18:09:02.063: D/ServiceManager(22760): The name of the service is theme
        04-26 18:09:02.067: W/asset(22760): AssetManager-->addDefaultAssets CIP path not exsit!
        04-26 18:09:02.068: D/ThemeManager(22760): contextandroid.app.ContextImpl@41296280
        04-26 18:09:02.069: D/ThemeManager(22760): create ThemeManager object
        04-26 18:09:02.070: D/ThemeManager(22760): Res_clear()
        04-26 18:09:02.107: W/asset(22760): AssetManager-->addDefaultAssets CIP path not exsit!
        04-26 18:09:02.138: I/dalvikvm(22760): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
        04-26 18:09:02.138: W/dalvikvm(22760): VFY: unable to resolve virtual method 11343: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
        04-26 18:09:02.138: D/dalvikvm(22760): VFY: replacing opcode 0x6f at 0x0000
        04-26 18:09:02.139: I/dalvikvm(22760): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
        04-26 18:09:02.139: W/dalvikvm(22760): VFY: unable to resolve virtual method 11349: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
        04-26 18:09:02.140: D/dalvikvm(22760): VFY: replacing opcode 0x6f at 0x0000
        04-26 18:09:02.142: I/dalvikvm(22760): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
        04-26 18:09:02.143: W/dalvikvm(22760): VFY: unable to resolve virtual method 9037: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
        04-26 18:09:02.143: D/dalvikvm(22760): VFY: replacing opcode 0x6e at 0x000e
        04-26 18:09:02.154: I/dalvikvm(22760): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
        04-26 18:09:02.154: W/dalvikvm(22760): VFY: unable to resolve virtual method 365: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
        04-26 18:09:02.155: D/dalvikvm(22760): VFY: replacing opcode 0x6e at 0x0002
        04-26 18:09:02.157: I/dalvikvm(22760): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
        04-26 18:09:02.157: W/dalvikvm(22760): VFY: unable to resolve virtual method 387: Landroid/content/res/TypedArray;.getType (I)I
        04-26 18:09:02.157: D/dalvikvm(22760): VFY: replacing opcode 0x6e at 0x0002
        04-26 18:09:02.194: D/ThemeManager(22760): Res_clear()
        04-26 18:09:02.206: W/asset(22760): AssetManager-->addDefaultAssets CIP path not exsit!
        04-26 18:09:02.231: D/AbsListView(22760): checkAbsListViewlLogProperty get invalid command
        04-26 18:09:02.245: V/PhoneWindow(22760): DecorView setVisiblity: visibility = 4
        04-26 18:09:02.245: D/ServiceManager(22760): The name of the service is input_method
        04-26 18:09:02.251: D/ServiceManager(22760): The name of the service is power
        04-26 18:09:02.257: V/PhoneWindow(22760): DecorView setVisiblity: visibility = 0
        04-26 18:09:02.276: D/GN_FW_TextView(22760): onMeasure,mLayout=android.text.BoringLayout@41312d60
        04-26 18:09:02.278: D/AndroidRuntime(22760): Shutting down VM
        04-26 18:09:02.279: W/dalvikvm(22760): threadid=1: thread exiting with uncaught exception (group=0x40d249a8)
        04-26 18:09:02.295: E/AndroidRuntime(22760): FATAL EXCEPTION: main
        04-26 18:09:02.295: E/AndroidRuntime(22760): java.lang.NullPointerException
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at com.example.gridviewmapexample.NikAdapter.getView(MainActivity.java:98)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.widget.AbsListView.obtainView(AbsListView.java:2207)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.widget.GridView.onMeasure(GridView.java:1040)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.View.measure(View.java:15635)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.View.measure(View.java:15635)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.View.measure(View.java:15635)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.View.measure(View.java:15635)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.View.measure(View.java:15635)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.View.measure(View.java:15635)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2200)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.View.measure(View.java:15635)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2165)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1443)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4872)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.Choreographer.doCallbacks(Choreographer.java:579)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.Choreographer.doFrame(Choreographer.java:548)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.os.Handler.handleCallback(Handler.java:800)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.os.Handler.dispatchMessage(Handler.java:100)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.os.Looper.loop(Looper.java:194)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at android.app.ActivityThread.main(ActivityThread.java:5410)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at java.lang.reflect.Method.invokeNative(Native Method)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at java.lang.reflect.Method.invoke(Method.java:525)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        04-26 18:09:02.295: E/AndroidRuntime(22760):    at dalvik.system.NativeStart.main(Native Method)
        04-26 18:09:02.332: I/Process(22760): Sending signal. PID: 22760 SIG: 9

既然您已经发布了堆栈跟踪(顺便说一句,不需要日志的其余部分),您应该能够自己找到问题发生的地方。

堆栈跟踪告诉您 NPE (NullPointerException) 出现在 getView() 的第 98 行 MainActivity.java

我不知道第 98 行是哪一行,但我在 getView() 中看到一个潜在的问题:

  • 你想要 LayoutInflater,但你调用 getSystemService(Context.LAUNCHER_APPS_SERVICE)。您可能想改为调用 getSystemService(Context.LAYOUT_INFLATER_SERVICE)