Android Gridview无法正常工作

时间:2012-07-02 12:43:18

标签: android android-layout gridview

我为android网格视图编写了一个代码,但不幸的是,当我运行代码应用程序返回错误时,这样的“应用程序Nues Hound RSS(进程com.nues.rss)已经意外停止请稍后尝试agian”

如果有人知道如何解决这个问题,请帮助我。我找不到任何编码错误。所以欢迎专家给出适当的指示。

这是我的活动课

public void onCreate(Bundle savedInstanceState) {

    if (!isNetworkAvailbale()) {
        Toast.makeText(getApplicationContext(), "Internet Connection not available", Toast.LENGTH_SHORT).show();
        finish();
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.gridview);

    GridView gridView;

    ImageAdapter mAdapter;

    mAdapter = new ImageAdapter(this);

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

    gridView.setAdapter(mAdapter);

    gridView.setOnItemClickListener(new OnItemClickListener() {@
        Override
        public void onItemClick(AdapterView <? > arg0, View arg1, int position, long

            arg3) {
            Toast.makeText(NuesHoundRSSActivity.this, "Item co" + osition, Toast.LENGTH_SHORT).show();
        }
    });
}

这是适配器类

package com.nues.rss;

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

public class ImageAdapter extends BaseAdapter {
    private Activity activity;  

public ImageAdapter(Context 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 static class ViewHolder  
{  
    public ImageView imgView;    
}  

@Override  
public View getView(int position, View convertView, ViewGroup parent) {  
    // TODO Auto-generated method stub  
    ViewHolder view;  
    LayoutInflater inflator = activity.getLayoutInflater();  

    if(convertView==null)  
    {  
        view = new ViewHolder();  
        convertView = inflator.inflate(R.layout.gridview_row, null);  

        view.imgView = (ImageView) convertView.findViewById(R.id.imageView1);  

        convertView.setTag(view);  

    }  
    else  
    {  
        view = (ViewHolder) convertView.getTag();  
    }  

    view.imgView.setImageResource(mThumbIds[position]);  

    return convertView;  
}  

// references to our images
private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
};


}

这是我的Gridview XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dp">

<ImageView
    android:layout_height="64dp"
    android:id="@+id/imageView1"
    android:layout_width="64dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">
</ImageView>

<TextView
    android:text="TextView"
    android:layout_height="wrap_content"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_below="@+id/imageView1"
    android:layout_marginTop="2dp"
    android:layout_centerHorizontal="true"
    android:textSize="18sp"
    android:ellipsize="marquee"></TextView>
</RelativeLayout>

3 个答案:

答案 0 :(得分:2)

private Activity activity;  

public ImageAdapter(Context c) {

}

将此替换为以下

private Context c;

        public ImageAdapter(Context c) {
            this.c = c;

        }

并且

LayoutInflater inflator = activity.getLayoutInflater();  

LayoutInflater inflator = ((Activity) c).getLayoutInflater(); 

这样可以正常工作。

答案 1 :(得分:0)

见这里:

public class ImageAdapter extends BaseAdapter {
        private Activity activity;  

    public ImageAdapter(Context c) {

    }

您没有初始化您的活动对象。

试试这个:

 public ImageAdapter(Activity ac) {
    this.activity=ac;
    }

答案 2 :(得分:0)

在您的代码中private Activity activity;永远不会设置为任何内容并且为null,因此activity.getLayoutInflater();崩溃。

你想做这样的事情:

private Context context; 

public ImageAdapter(Context c) { 
    this.context = c;
} 

然后context.getLayoutInflater();