【问题标题】:I'm getting error with CustomGridView in AlertDialog我在 AlertDialog 中遇到 CustomGridView 错误
【发布时间】:2015-01-05 02:35:26
【问题描述】:

我必须创建一个 AlertDialog 供用户在 gridview 中选择一个类别,但是我遇到了错误。这是我的代码。当我在 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>

BaseAdapter

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$6(NewEventActivity.java:199)
01-04 23:44:38.727: E/AndroidRuntime(10444):    at com.fb.newcomersapp.NewEventActivity$8.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)

【问题讨论】:

  • 哪里出错了?请添加错误堆栈跟踪。
  • 第 205 行是什么? GridView 可能为空。

标签: java android gridview layout android-alertdialog


【解决方案1】:

如果你发布你的 logcat,它会更容易帮助你。

但我猜你会得到一个 NullPointerException 就行了

grid.setAdapter(adapter);

当你打电话时

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

你会得到 null (除非它们是您当前布局中的 category_grid) 我认为您想要做的是扩大网格布局。

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

【讨论】:

  • 我更新了帖子。我进行了更改,应用程序没有崩溃,但网格视图没有出现。 private void showAlertDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); CustomGrid 适配器 = new CustomGrid(NewEventActivity.this, categoryName, categoryImage); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); GridView grid = (GridView) inflater.inflate(R.layout.pick_category, (ViewGroup)findViewById(R.id.category_grid)); grid.setAdapter(适配器); builder.setView(grid); builder.show(); }
  • 很高兴听到这个消息。通过发布另一个问题,您可能会获得更好的结果。但对于初学者来说,我可以看到适配器中的方法 public int getCount() 总是返回 0。我不知道 categoryName 是什么,但 getCount 可能应该返回 categoryName.length
  • 我从没想过这种方法会影响任何事情,只是按照你告诉我的去做,它奏效了。为什么?
  • 您可能会在这里找到最佳答案developer.android.com/reference/android/widget/… 如果视图知道它有项目,那么它将调用 getView 来加载项目,直到视图已满或直到您没有项目。总体 getCount 应该返回您想要在网格中显示的项目数量或适配器用于(列表/网格/等)的任何内容。
猜你喜欢
  • 2017-01-02
  • 2011-07-06
  • 2012-04-24
  • 2015-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-04
相关资源
最近更新 更多