新手 - tablayout中的GridView:无法从静态上下文引用此/ getApplicationContext

时间:2015-12-30 11:32:23

标签: java android android-gridview android-tablayout

我是Android新手,我遇到了问题。

按照android指南中的示例,我开始使用标签布局。所有工作正常,直到我尝试在#34;公共静态类PlaceholderFragment"内膨胀gridview。 下面是MainActivity.java中代码的部分副本

public class MainActivity extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    Toast.makeText(getBaseContext(), "MainActivity onCreate End", Toast.LENGTH_LONG).show();

}

适配器:

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "TAB 1";
            case 1:
                return "TAB 2";
            case 2:
                return "TAB 3";
        }
        return null;
    }

占位符,包含错误:

    public static class PlaceholderFragment extends Fragment {
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    public static PlaceholderFragment newInstance(int sectionNumber) {

        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        //Identificativo del TAB che si sta per visualizzare
        int tab_ID = getArguments().getInt(ARG_SECTION_NUMBER);

        View rootView;
        switch(tab_ID){
            case 1: rootView = inflater.inflate(R.layout.fragment_tab1, container, false);
                    break;
            case 2: rootView = inflater.inflate(R.layout.fragment_tab2, container, false);
                    break;
            case 3: rootView = inflater.inflate(R.layout.fragment_tab3, container, false);
                    GridView gridView = (GridView) rootView.findViewById(R.id.gridview3);
                    gridView.setAdapter(new MyGridAdapter(MainActivity.this.getApplicationContext()));
                    break;

            default:rootView = inflater.inflate(R.layout.fragment_tab2, container, false);
        }

        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }
}

gridView.setAdapter(new MyGridAdapter(MainActivity.this.getApplicationContext()));生成"这不能从静态上下文中引用"错误。

MyGridAdapter代码:

private class MyGridAdapter 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", Color.GREEN));
    items.add(new Item("Image 2", Color.RED));
    items.add(new Item("Image 3", Color.BLUE));
    items.add(new Item("Image 4", Color.GRAY));
    items.add(new Item("Image 5", Color.YELLOW));
}

@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).colorId;
}

@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.setBackgroundColor(item.colorId);
    name.setText(item.name);

    return v;
}

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

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

}

我试图在没有成功的情况下遵循其他一些例子。 如果我没有将PlaceholderFragment类声明为静态,则我在SectionPagerAdapter类中遇到PlaceholderFragment.newInstance(position + 1)的问题。

有人可以帮我解决并理解这个问题吗?

非常感谢!

2 个答案:

答案 0 :(得分:0)

在Fragments中,您可以通过调用getActivity()方法来获取活动。获得活动对象后,可以通过getAtivity()获取应用程序上下文.getApplicaionContext();

gridView.setAdapter(new MyGridAdapter(getActivity().getApplicationContext()));

答案 1 :(得分:0)

[解决] 错误的代码是:

            case 3: rootView = inflater.inflate(R.layout.fragment_tab3, container, false);
                GridView gridView = (GridView) rootView.findViewById(R.id.gridview3);
                gridView.setAdapter(new MyGridAdapter(MainActivity.this.getApplicationContext()));
                break;

确定代码是:

                    GridView gridView = (GridView) rootView.findViewById(R.id.gridview3);
                    gridView.setAdapter(new ImageAdapter(gridView.getContext()));

谢谢!