2016-01-15 63 views
1

我是新來的Android,目前想創建一個片段顯示的GridView layout.However我無法顯示圖像,並返回下面的錯誤在GridView控件顯示圖像:無法使用片段

 java.lang.NullPointerException 
     at miui.util.AttributeResolver.i(SourceFile:35) 
     at miui.util.AttributeResolver.resolveInt(SourceFile:145) 
     at miui.util.AttributeResolver.isUsingMiuiTheme(SourceFile:179) 
     at miui.os.Environment.isUsingMiui(SourceFile:99) 
     at android.view.ViewConfigurationInjector.get(ViewConfigurationInjector.java:18) 
     at android.view.ViewConfigurationHelper.get(ViewConfigurationHelper.java:10) 
     at android.view.ViewConfiguration.get(ViewConfiguration.java:357) 
     at android.view.View.<init>(View.java:3452) 
     at android.widget.ImageView.<init>(ImageView.java:114) 
     at info.androidhive.slidingmenu.GridViewAdapter.getView(GridViewAdapter.java:52) 
     at android.widget.AbsListView.obtainView(AbsListView.java:2257) 

下面是我的片段代碼

公共類HomeFragment延伸片段{

}

下面是我的gridview的適配器代碼

public class GridViewAdapter extends BaseAdapter { 

    private Activity _activity; 
    private LayoutInflater inflater; 
    private int imageWidth; 
    private Context context; 

    public GridViewAdapter(Activity activity) { 
     this._activity=activity; 

    } 

    @Override 
    public int getCount() { 
     return this.photoList.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView; 
     if (inflater == null) 
      inflater = (LayoutInflater) _activity 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.grid_item_photo, null); 
      imageView = new ImageView(context); 
      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(photoList[position]); 
     return imageView; 
    } 

    public Integer[] photoList = { 

      R.drawable.ic_whats_hot, R.drawable.ic_home, 
      R.drawable.ic_launcher, R.drawable.ic_communities, 
      R.drawable.ic_people, R.drawable.ic_whats_hot 
    }; 
} 

錯誤行表明我行從上下文獲取圖像時有問題,可能會建議?

imageView = new ImageView(context); 
+0

什麼是第52行? –

+0

你是否動態創建imageview?或者你想在你的佈局中使用imageview – playmaker420

+0

line 52 -imageView = new ImageView(context); – michelletbs

回答

1

您的上下文對象爲空。因爲你有你的活動的上下文。所以使用這個

imageView = new ImageView(_activity); 
0

由於它是你的上下文返回爲null,我們需要提供一個非空的參數。提供上下文的一些常見類是活動,應用程序,服務和視圖。在第9行中,您初始化一個Activity對象_activity。

由於此對象具有上下文,因此可以使用它來代替「上下文」。

imageView = new ImageView(_activity); 
+0

謝謝@John Riggs對上下文的解釋和知識共享。現在我對背景有了更好的理解。 – michelletbs

0

您正在使用未初始化的上下文。

請使用從HomeFragment通過GridViewAdapter解析的_activity。

錯誤是您的「上下文」從未在您的GridViewAdapter中初始化。

替換爲imageView = new ImageView(_activity);