2016-03-24 57 views
0

我正在試圖讓一個基於網格的圖庫查看工作在我的片段之一,我得到這些錯誤安卓網圖庫查看錯誤

Error:(25, 20) error: onCreate(Bundle) in GalleryFragment cannot override onCreate(Bundle) in Fragment 
attempting to assign weaker access privileges; was public 
Error:(28, 9) error: cannot find symbol method setContentView(int) 
Error:(30, 39) error: cannot find symbol method findViewById(int) 
Error:(31, 43) error: incompatible types: GalleryFragment cannot be converted to Context 

任何想法如何解決?我所遵循的教程似乎旨在使這個活動不知道這是否有所作爲。由於

package com.test.test.app; 


import android.content.Context; 
import android.os.Bundle; 
import android.app.Activity; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.TextView; 

import java.util.ArrayList; 
import java.util.List; 


public class GalleryFragment extends Fragment 
{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     GridView gridView = (GridView)findViewById(R.id.gridview); 
     gridView.setAdapter(new MyAdapter(this)); 
    } 

    private class MyAdapter extends BaseAdapter 
    { 
     private List<Item> items = new ArrayList<Item>(); 
     private LayoutInflater inflater; 

     public MyAdapter(Context context) 
     { 
      inflater = LayoutInflater.from(context); 

      items.add(new Item("Image 1", R.drawable.image_1)); 
      items.add(new Item("Image 2", R.drawable.image_2)); 
      items.add(new Item("Image 3", R.drawable.image_3)); 
      items.add(new Item("Image 4", R.drawable.image_4)); 
      items.add(new Item("Image 5", R.drawable.image_5)); 
     } 

     @Override 
     public int getCount() { 
      return items.size(); 
     } 

     @Override 
     public Object getItem(int i) 
     { 
      return items.get(i); 
     } 

     @Override 
     public long getItemId(int i) 
     { 
      return items.get(i).drawableId; 
     } 

     @Override 
     public View getView(int i, View view, ViewGroup viewGroup) 
     { 
      View v = view; 
      ImageView picture; 
      TextView name; 

      if(v == null) 
      { 
       v = inflater.inflate(R.layout.gridview_item, viewGroup, false); 
       v.setTag(R.id.picture, v.findViewById(R.id.picture)); 
       v.setTag(R.id.text, v.findViewById(R.id.text)); 
      } 

      picture = (ImageView)v.getTag(R.id.picture); 
      name = (TextView)v.getTag(R.id.text); 

      Item item = (Item)getItem(i); 

      picture.setImageResource(item.drawableId); 
      name.setText(item.name); 

      return v; 
     } 

     private class Item 
     { 
      final String name; 
      final int drawableId; 

      Item(String name, int drawableId) 
      { 
       this.name = name; 
       this.drawableId = drawableId; 
      } 
     } 
    } 
} 
+0

它確實有差別,如果你只是與Android開始它會更容易爲你只是做這在一個活動,而不是在片段 – Sourabh

+0

@Sourabh我知道,但它的工作和導航抽屜碎片似乎是最好的路線去 – DinoArmadillo

+0

檢查我的答案 – Sourabh

回答

0

要麼你可以從片段移到你的代碼的活動或更換您的onCreate本:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.activity_main, container, false); 
    GridView gridView = (GridView) view.findViewById(R.id.gridview); 
    gridView.setAdapter(new MyAdapter(getActivity())); 
    return view; 
} 
+0

謝謝,這擺脫了我的錯誤。該應用程序現在崩潰了,但一次一步。知道任何好的資源,爲noob做一個簡單的網格庫視圖? – DinoArmadillo

+0

一個簡單的谷歌搜索給我[這](http://javatechig.com/android/android-gridview-example-building-image-gallery-in-android)看起來不錯,但不使用片段:P – Sourabh

0

Fragment不提供context。因此,你不能使用此:

gridView.setAdapter(new MyAdapter(this)); 

它應該是:

gridView.setAdapter(new MyAdapter(getActivity())); 

注:這是不是安全的getActivityonCreateFragment作爲activity可能無法onCreateView得到活動方面還創建。嘗試從onActivityCreated調用它。