2012-07-02 78 views
0

我寫了一個代碼爲android網格視圖,但不幸的是,當我運行代碼應用程序返回像這樣的錯誤「Appication Nues Hound RSS(進程com.nues.rss)意外停止了請稍後嘗試」Android Gridview不工作

如果有人知道如何解決這個問題,請幫助我。我無法找到任何編碼錯誤。因此歡迎專家給予適當的指導。

這是我的活動類

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> 
+0

當您的應用崩潰時,LogCat中的日誌是什麼?什麼例外?什麼線? –

+0

ImageAdapter class line 40的java.lang.NullPointerException,即「LayoutInflater inflator = activity.getLayoutInflater();」 , 你有什麼主意嗎 ?我正在使用Android平臺2.1。 –

回答

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(); 

這將罰款代替這一點。

0

在這裏看到:

public class ImageAdapter extends BaseAdapter { 
     private Activity activity; 

    public ImageAdapter(Context c) { 

    } 

您並未初始化您的活動對象。

試試這個:

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

謝謝隊友您的解決方案也正確,但我不能接受多個答案... :( –

0

在你的代碼private Activity activity;從未設置到任何東西,是空,所以activity.getLayoutInflater();崩潰。

你想要做這樣的事情:

private Context context; 

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

然後context.getLayoutInflater();

+0

感謝隊友您的解決方案也正確,但我不能接受多個答案...... :( –