Android活动上下文为空 [英] Android Activity Context is Null

查看:100
本文介绍了Android活动上下文为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在这里有这些代码,它运行时没有崩溃。但是,当我将this传递给gridadapter时,mContext为null。我试图通过getApplicationContext()但仍然无法使getImage方法正常运行,因为getResources.getIdentifier行没有返回任何内容,因为Context为null。我很感激,如果有人可以教我如何发生这种情况,我该如何解决它。谢谢。

So I have these Codes here, It runs without crashing. However When I pass "this" into the gridadapter the mContext is null. I tried to pass getApplicationContext() through but still can't get the getImage method to run properly because the getResources.getIdentifier line does not return anything since Context is null. I appreciated it if someone can teach me how come this is happening and how can I fix it. Thanks.

public class ChampionInfo extends FragmentActivity {

GridView gridtable;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_champion_info);

    gridtable = (GridView) findViewById(R.id.gridtable);        
    gridtable.setAdapter(new GridAdapter(getApplicationContext()));

}

public class GridAdapter extends BaseAdapter {
    private Context mContext;

    String[] list = getResources().getStringArray(R.array.championlist);

    int[] champImage = getImage();

    public int[] getImage() {

        int[] tempImage = new int[list.length];

        for (int i = 0; i < list.length; i++) {
            tempImage[i] = getResources().getIdentifier(list[i],
                    "drawable", getPackageName());
        }

        return tempImage;

    }

    // Constructor
    public GridAdapter(Context c) {

        mContext = c;

    }

    public int getCount() {
        return champImage.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 View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            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(champImage[position]);
        return imageView;
    }

    // Keep all Images in array

}

}

推荐答案

您的问题的根源是您尝试初始化内联成员变量with

The root of your problem is that you are trying to initialize a member variable inline with

int[] champImage = getImage();

执行之前设置 mContext的构造函数到有效值。但是,在 getImage() mContext 的事实是 null c $ c>应该是无关紧要的,因为你从未在该方法中实际使用 mContext

This executes before the constructor which sets mContext to a valid value. However, the fact that mContext is null inside the call for getImage() should be irrelevant because you never actually use mContext in that method.

整体问题是你需要注意执行的顺序。

The overall issue is that you need to be careful about the order of execution.

这篇关于Android活动上下文为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆