我正在試圖讓一個基於網格的圖庫查看工作在我的片段之一,我得到這些錯誤安卓網圖庫查看錯誤
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;
}
}
}
}
它確實有差別,如果你只是與Android開始它會更容易爲你只是做這在一個活動,而不是在片段 – Sourabh
@Sourabh我知道,但它的工作和導航抽屜碎片似乎是最好的路線去 – DinoArmadillo
檢查我的答案 – Sourabh