自定义对话框中的gridview不工作......?

时间:2010-09-06 03:00:30

标签: android

我尝试将网格视图添加到自定义对话框中。显示时 它崩溃的对话。
但是当尝试在正常活动中显示网格视图时 工作。(没有对话)
我从开发人员android网站上拿了例子。看看网格视图和 试图集成自定义对话框。
我使用了两个xml main.xml& category.xml。
这是我的代码:

package com.android.test;   
import android.app.Activity;   
import android.app.AlertDialog;   
import android.app.Dialog;   
import android.content.Context;   
import android.os.Bundle;   
import android.view.LayoutInflater;   
import android.view.View;   
import android.view.ViewGroup;   
import android.view.View.OnClickListener;  
import android.widget.AdapterView;    
import android.widget.BaseAdapter;   
import android.widget.Button;   
import android.widget.GridView;   
import android.widget.ImageView;   
import android.widget.TextView;   
import android.widget.Toast;   
import android.widget.AdapterView.OnItemClickListener;   
public class test extends Activity {   
    /** Called when the activity is first created. */   
    public final static int CATEGORY_ID=0;   
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.main);   
        Button button = (Button)findViewById(R.id.categories);   
        button.setOnClickListener(new Button.OnClickListener(){   
            public void onClick(View v) {   
                                showDialog(CATEGORY_ID);   
            }   
        });   
    }   
    protected Dialog onCreateDialog(int id) {   
        Dialog dialog;   
        switch(id) {   
        case CATEGORY_ID:   
                AlertDialog.Builder builder;   
                AlertDialog alertDialog;   
                Context mContext = this;   
                LayoutInflater inflater = (LayoutInflater)   
mContext.getSystemService(LAYOUT_INFLATER_SERVICE);   
                View layout = inflater.inflate(R.layout.categorydialog,   
                                               (ViewGroup)   
findViewById(R.id.layout_root));   
                TextView text = (TextView) layout.findViewById(R.id.text);   
                text.setText("Hello, this is a custom dialog!");   
                ImageView image = (ImageView)   
layout.findViewById(R.id.image);   
                image.setImageResource(R.drawable.find);   
                GridView gridview = (GridView) findViewById(R.id.gridview);   
           gridview.setAdapter(new ImageAdapter(this));   
            gridview.setOnItemClickListener(new OnItemClickListener()   
{   
                public void onItemClick(AdapterView<?> parent, View v,   
int position, long id) {   
                    Toast.makeText(test.this, "" + position,   
Toast.LENGTH_SHORT).show();   
                }   
            });   
                builder = new AlertDialog.Builder(mContext);   
                builder.setView(layout);   
                dialog = builder.create();   
            break;   
        default:   
            dialog = null;   
        }   
        return dialog;   
    }   
    public class ImageAdapter extends BaseAdapter {   
        private Context mContext;   
        public ImageAdapter(Context c) {   
            mContext = c;   
        }   
        public int getCount() {   
            return mThumbIds.length;   
        }   
        public Object getItem(int position) {   
            return null;   
        }   
        public long getItemId(int position) {   
            return 0;   
        }   
        // create a new ImageView for each item referenced by the   
Adapter   
        public View getView(int position, View convertView, ViewGroup   
parent) {   
            ImageView imageView;   
            if (convertView == null) {  // if it's not recycled,   
initialize some attributes   
                imageView = new ImageView(mContext);   
                imageView.setLayoutParams(new   
GridView.LayoutParams(85, 85));   
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);   
                imageView.setPadding(8, 8, 8, 8);   
            } else {   
                imageView = (ImageView) convertView;   
            }   
            imageView.setImageResource(mThumbIds[position]);   
            return imageView;   
        }   
        // references to our images   
        private Integer[] mThumbIds = {   
                R.drawable.info, R.drawable.find,   
                R.drawable.info, R.drawable.find,   
                R.drawable.info, R.drawable.find,   
                R.drawable.info, R.drawable.find,   
                R.drawable.info, R.drawable.find,   
                R.drawable.info, R.drawable.find,   
                R.drawable.info, R.drawable.find,   
                R.drawable.info, R.drawable.find,   
                R.drawable.info, R.drawable.find,   
                R.drawable.info, R.drawable.find,   
                R.drawable.info, R.drawable.find   
        };   
    }   
} 

main.xml

<?xml version="1.0" encoding="utf-8"?>   
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/   
android"   
              android:layout_width="fill_parent"   
              android:layout_height="fill_parent"   
              android:padding="3dip"   
              android:orientation="vertical">   
<Button android:id="@+id/categories"   
android:layout_width="150dip"   
android:layout_height="wrap_content"   
android:layout_gravity="right"   
android:text="Choose categories"   
/>   
</LinearLayout>   

categorydialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/   
android"   
              android:id="@+id/layout_root"   
              android:orientation="vertical"   
              android:layout_width="fill_parent"   
              android:layout_height="fill_parent"   
              android:padding="10dp"   
              >   
    <ImageView android:id="@+id/image"   
               android:layout_width="wrap_content"   
               android:layout_height="fill_parent"   
               android:layout_marginRight="10dp"   
               />   
    <TextView android:id="@+id/text"   
              android:layout_width="wrap_content"   
              android:layout_height="fill_parent"   
              android:textColor="#FFF"   
              />   
<GridView xmlns:android="http://schemas.android.com/apk/res/android"    
    android:id="@+id/gridview"   
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent"   
    android:columnWidth="90dp"   
    android:numColumns="auto_fit"   
    android:verticalSpacing="10dp"   
    android:horizontalSpacing="10dp"   
    android:stretchMode="columnWidth"   
    android:gravity="center"   
/>   
</LinearLayout>   

有人可以帮帮我吗。

1 个答案:

答案 0 :(得分:5)

更改此行:

GridView gridview = (GridView) findViewById(R.id.gridview);

这样的事情:

GridView gridview = (GridView) layout.findViewById(R.id.gridview);