我在 AlertDialog 中使用 CustomGridView 时遇到错误

I'm getting error with CustomGridView in AlertDialog

我必须为用户在 gridview 中选择一个类别创建一个 AlertDialog,但是我遇到了错误。这是我的代码。当我在 TextView 中单击以调用在显示对话框中工作的方法时,应用程序崩溃。有人知道一些教程吗?

category_grid.xml

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

</GridView>

grid_single.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/vermelho_row" >

<ImageView 
    android:id="@+id/category_image"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/ic_action_image_photo_camera"
    android:layout_gravity="center"/>

<TextView 
    android:id="@+id/category_name"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:gravity="center"
    android:text="Experimentar"
    android:background="@color/laranja_row"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"/>


</LinearLayout>

基础适配器

package com.fb.newcomersapp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomGrid extends BaseAdapter{
private Context context;
private String [] categoryName;
private int [] categoryImage;
public CustomGrid(Context context, String [] categoryName, int [] categoryImage){
     this.context = context;
     this.categoryName = categoryName;
     this.categoryImage = categoryImage;
 }

 @Override
 public int getCount() {
     // TODO Auto-generated method stub
    return 0;
}

 @Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View grid;
    LayoutInflater inflater = (LayoutInflater)    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     if(convertView == null){
         grid = new View(context);
         grid = inflater.inflate(R.layout.grid_single, null);
        TextView textView = (TextView) grid.findViewById(R.id.category_name);
         ImageView imageView = (ImageView) grid.findViewById(R.id.category_image);
        textView.setText(categoryName[position]);
        imageView.setImageResource(categoryImage[position]);
    }
    else{
        grid = (View) convertView;
    }
    return grid;
    }

}

Activity 中显示对话框的方法

private void showAlertDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    CustomGrid adapter = new CustomGrid(NewEventActivity.this, categoryName, categoryImage);
    GridView grid = (GridView) findViewById(R.id.category_grid);


    grid.setAdapter(adapter);


    builder.setView(grid);
    builder.setTitle("Goto");
    builder.show();
}

** 日志 **

01-04 23:44:38.727: E/AndroidRuntime(10444): FATAL EXCEPTION: main
01-04 23:44:38.727: E/AndroidRuntime(10444): Process: com.fb.newcomersapp, PID: 10444
01-04 23:44:38.727: E/AndroidRuntime(10444): java.lang.NullPointerException
01-04 23:44:38.727: E/AndroidRuntime(10444):    at   com.fb.newcomersapp.NewEventActivity.showAlertDialog(NewEventActivity.java:205)
01-04 23:44:38.727: E/AndroidRuntime(10444):    at  com.fb.newcomersapp.NewEventActivity.access(NewEventActivity.java:199)
01-04 23:44:38.727: E/AndroidRuntime(10444):    at com.fb.newcomersapp.NewEventActivity.onClick(NewEventActivity.java:190)
01-04 23:44:38.727: E/AndroidRuntime(10444):    at android.view.View.performClick(View.java:4456)
01-04 23:44:38.727: E/AndroidRuntime(10444):    at android.view.View$PerformClick.run(View.java:18465)
01-04 23:44:38.727: E/AndroidRuntime(10444):    at android.os.Handler.handleCallback(Handler.java:733)
01-04 23:44:38.727: E/AndroidRuntime(10444):    at android.os.Handler.dispatchMessage(Handler.java:95)
01-04 23:44:38.727: E/AndroidRuntime(10444):    at android.os.Looper.loop(Looper.java:136)
01-04 23:44:38.727: E/AndroidRuntime(10444):    at android.app.ActivityThread.main(ActivityThread.java:5086)
01-04 23:44:38.727: E/AndroidRuntime(10444):    at java.lang.reflect.Method.invokeNative(Native Method)
01-04 23:44:38.727: E/AndroidRuntime(10444):    at java.lang.reflect.Method.invoke(Method.java:515)
01-04 23:44:38.727: E/AndroidRuntime(10444):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-04 23:44:38.727: E/AndroidRuntime(10444):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-04 23:44:38.727: E/AndroidRuntime(10444):    at dalvik.system.NativeStart.main(Native Method)

如果你 post 你的 logcat 会更容易帮助你。

但我猜你在线上遇到了 NullPointerException

grid.setAdapter(adapter);

当你打电话时

GridView grid = (GridView) findViewById(R.id.category_grid);

你将得到空值 (除非他们是您当前布局中的 category_grid) 我认为你想要做的是膨胀网格布局。

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
GridView grid = inflater.inflate(R.layout.category_grid, null);