自定义gridview中的NullPointerException

时间:2014-02-25 16:52:09

标签: android gridview nullpointerexception

我有以下GridView代码,它适用于大多数Android手机但只在三星Note 8.0上与NPE崩溃:

public class MainActivity extends Activity  {


    GridView gridView;
    ArrayList<Item> gridArray = new ArrayList<Item>();
     CustomGridViewAdapter customGridAdapter;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

      //set grid view item
        Bitmap homeIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.index_1home);
        Bitmap portalIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.index_2portal);
        Bitmap messageIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.index_3message);
        Bitmap calendarIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.index_4calendar);
        Bitmap eventIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.index_5events);
        Bitmap galleryIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.index_6gallery);
        Bitmap contactIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.index_7contact);
        Bitmap timetableIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.index_8timetable);
        Bitmap classlistIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.index_9classlist);
        Bitmap attendanceIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.index_10attendance);
        Bitmap accountIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.index_11profile);


        gridArray.add(new Item(homeIcon,"Home"));
        gridArray.add(new Item(portalIcon,"Portal"));
        gridArray.add(new Item(messageIcon,"Message"));
        gridArray.add(new Item(calendarIcon,"Calendar"));
        gridArray.add(new Item(eventIcon,"Event"));
        gridArray.add(new Item(galleryIcon,"Gallery"));
        gridArray.add(new Item(contactIcon,"Contact"));
        gridArray.add(new Item(timetableIcon,"Timetable"));
        gridArray.add(new Item(classlistIcon,"Classlist"));
        gridArray.add(new Item(attendanceIcon,"Attendance"));
        gridArray.add(new Item(accountIcon,"Account"));


        gridView = (GridView) findViewById(R.id.gridView1);           
        customGridAdapter = new CustomGridViewAdapter(MainActivity.this, R.layout.row_grid, gridArray);
        gridView.setAdapter(customGridAdapter);


    }

在最后一行使用NullPointerException:

gridView.setAdapter(customGridAdapter);

由于此问题仅发生在Samsung Note 8.0中,以下是我的清单的一部分:

<activity
            android:name="com.company.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Main.xml存在如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/home_bg"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="55dp"
        android:padding="10dp"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:numColumns="3"
        android:columnWidth="140dp"
        android:stretchMode="columnWidth"
        android:gravity="center_horizontal" />

</LinearLayout>

这是CustomAdapter代码:

{    
public class CustomGridViewAdapter extends ArrayAdapter<Item> {
    Context context;
    int layoutResourceId;
    ArrayList<Item> data = new ArrayList<Item>();

    public CustomGridViewAdapter(Context context, int layoutResourceId,
            ArrayList<Item> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        RecordHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new RecordHolder();
            holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
            holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
            row.setTag(holder);
        } else {
            holder = (RecordHolder) row.getTag();
        }

        Item item = data.get(position);
        holder.txtTitle.setText(item.getTitle());
        holder.imageItem.setImageBitmap(item.getImage());
        return row;

    }

    static class RecordHolder {
        TextView txtTitle;
        ImageView imageItem;

    }
}

这是日志:

E/AndroidRuntime(8341): FATAL EXCEPTION: main
E/AndroidRuntime(8341): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company/com.company.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime(8341):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
E/AndroidRuntime(8341):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
E/AndroidRuntime(8341):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
E/AndroidRuntime(8341):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
E/AndroidRuntime(8341):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(8341):     at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(8341):     at android.app.ActivityThread.main(ActivityThread.java:4947)
E/AndroidRuntime(8341):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(8341):     at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(8341):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
E/AndroidRuntime(8341):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
E/AndroidRuntime(8341):     at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(8341): Caused by: java.lang.NullPointerException
E/AndroidRuntime(8341):     at com.company.MainActivity.onCreate(MainActivity.java:72)
E/AndroidRuntime(8341):     at android.app.Activity.performCreate(Activity.java:5207)
E/AndroidRuntime(8341):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
E/AndroidRuntime(8341):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2085)
E/AndroidRuntime(8341):     ... 11 more

请帮我弄明白我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

最后经过几个小时的整理代码并抓取网页后,我发现了问题。

此问题是由此行返回null值引起的: gridView = (GridView) findViewById(R.id.gridView1);

我已经在我的/ layout /文件夹中正确设置了main.xml,但后来我意识到我的/ layout-large /中还有一个main.xml,我忘记更新了。这就是NPE仅在平板电脑上安装时才会发生的原因。

当我更新/ layout-large /文件夹中的main.xml时,我已经解决了我的问题。谢谢大家。