Android 活动上下文为空

2024-04-11

所以我这里有这些代码,它运行时不会崩溃。但是,当我将“this”传递到网格适配器时,mContext 为空。我尝试传递 getApplicationContext() 但仍然无法使 getImage 方法正常运行,因为 getResources.getIdentifier 行不会返回任何内容,因为 Context 为空。如果有人能教我为什么会发生这种情况以及如何解决它,我将不胜感激。谢谢。

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

}

}


尝试下面的代码或使用getFragmentManager() or getParent();

public class ChampionInfo extends FragmentActivity {

GridView gridtable;

Context ctx;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_champion_info);
    ctx=ChampionInfo.this;//getFragmentManager() or getParent()
    gridtable = (GridView) findViewById(R.id.gridtable);        
    gridtable.setAdapter(new GridAdapter(ctx));

}

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

}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 活动上下文为空 的相关文章

随机推荐

  • bash while 循环没有按预期工作

    我知道从技术上讲 它可能会按原样工作 并且这是人们所期望的bash语言 但这不是我所期望和写的 我试图让一切尽可能简单 This is fileA Name Status Networks Image Plans HostName A PA
  • Google App Engine 数据存储区索引上限

    有人可以用简单的英语解释一下数据存储中 5000 个索引的上限吗 这是否意味着存储对象的索引列表属性不能包含超过 5000 个元素 数据存储区限制单个实体可以拥有的索引条目数量 此限制设置为每个实体 5000 个元素 您可以使用以下命令轻松
  • Julia - 迭代字典中的键组合

    有没有一种巧妙的方法来迭代字典中的键组合 我的字典有这样的值 1 gt 1 2 2 3 gt 15 3 gt 6 7 8 4 9 11 gt 3 我需要做的是获取所有长度的键组合1 n where n可能是FX 3 就像上面的例子一样 我想
  • java.sql.SQLFeatureNotSupportedException:方法 org.postgresql.jdbc4.Jdbc4Connection.isValid(int) 尚未实现。在Spring引导中

    我正在开发Spring Boot Microservices例子 在这个例子中 我使用了config server and licensing service并成功部署于docker 当我调用 REST 端点时 http localhost
  • java.lang.RuntimeException:在 android.media.MediaRecorder.start(本机方法)处启动失败

    我正在处理电话录音 当我开始录制电话时 不幸的是它停止了 它给出错误 MediaRecorder 启动失败 2147483648 我请告诉我我的代码有什么问题 这是我的代码 public class incomingcall extends
  • 将 PDF 文档打印到 esc/pos 热敏打印机

    我们正在使用 xamarin forms 开发 POS APP 因为我们需要将收据打印到通过 LAN 连接的 esc pos 热敏打印机 我们的应用程序支持多语言 通过更改代码页使用 esc pos 命令打印多种语言效果非常好 但它仅适用于
  • 使用 JAX-RS 获取服务器的基本 url

    如何使用 JAX RS 获取服务器的基本 url 基本上 当程序位于本地主机上时 我想要 http localhost 8080 当程序位于实时服务器上时 我想要 http www theSite com 我正在使用泽西岛框架 是的 您可以
  • Firestore 每日配额

    我使用开发了一个示例应用程序Angular and 火库 我大约两个月前部署了我的应用程序 从那时起一直运行良好 然而 在上周 我注意到该应用程序在使用几个小时后因 客户端离线 错误而被阻止 我发现使用2个小时后我超出了每日的阅读操作限额
  • 在负载平衡的 Tornado 站点上使用 apache 基准测试获取失败的请求,但没有记录

    也许这是一个关于 ab 的问题 而不是关于 Tornado 的问题 但有些事情没有意义 我这样运行基准测试 ab n 100 http localdomainname 2 tornados being 1 nginx Concurrency
  • python 2 doctest 为何会失败,但失败消息中的值却没有差异?

    我在 Windows 中使用 Python 2 7 9 我有一个 UTF 8 编码的 python 脚本文件 其中包含以下内容 coding utf 8 def test func u gt gt gt test func u return
  • 以 RTL 格式格式化时 TextView 内的数字会反转

    里面的数字TextView以 RTL 格式格式化时会发生反转 当数字位于文本内的末尾时TextView他们逆转了 我怎样才能以编程方式解决这个问题 例如 下面的数字是相反的 它们应该显示为 误解 像 ARABIC 这样的 RTL 语言中的数
  • 如何解决对等依赖安装问题

    我正在尝试使用 npm 安装react contenteditable 但是 eslint 的对等依赖项不允许我安装以下错误的 risal 我正在使用 Ubuntu 18 04 LTS 操作系统 这是我用来安装react contented
  • 根据控制器指定不同的_Layout.cshtml

    我创建了一个 asp mvc3 项目 我想要根据选择的控制器有不同的 Layout cshtml 这是因为控制器 1 有 2 个按钮 控制器 2 有 3 个按钮 控制器 3 有 4 个按钮 每个控制器适用于特定类型的用户 因此取决于登录 我
  • Laravel 中的 Socket.io 轮询 404

    我正在尝试使用 Socket io 实现一个聊天应用程序 进入我的 Laravel 应用程序 聊天应用程序本身运行良好 但我在 Laravel 中遇到问题 我尝试在端口 8000 上提供 Laravel 服务 并在 8000 上提供聊天服务
  • 访问回调 user_is_anonymous 的反义词是什么?

    我知道在 drupal 模块中使用它来指定只有匿名用户才能看到该模块 仅指定登录用户的回调是什么 我有一个页面 我只想让登录用户访问 谢谢 它是用户 is logged in http api drupal org api function
  • 在 AVD 上运行自定义 ROM

    有谁知道是否可以在 AVD 上运行自定义 ROM 我该怎么做 谢谢 如果您自己构建自定义 rom 则在构建自定义 rom 后 您可以使用以下命令启动它emulator 但要做到这一点 你首先需要为模拟器构建 ROM 通常 full gene
  • 数据库结果的数组结构

    这可能是一个非常微不足道的问题 但是以下哪种方法是构造返回数据库结果的数组的最佳实践 比如说博客文章列表 对文章进行排序和分组 或者对元素进行排序和分组 Array title gt Array 0 gt Untitled 1 gt Unt
  • current_prolog_flag double_quotes DCG(代码或字符)?

    在使用 SWI Prolog DCG 时 我注意到有些人注意到 set prolog flag double quotes codes Jan http www swi prolog org pldoc man section string
  • 为什么正则表达式 ((x,y)|(x,z)) 是不确定的?

    为什么正则表达式 x y x z 像 Core Java 一书中所说的那样是不确定的 作者给出了他的观点 当解析器看到 x 时 它不知道采取两个替代方案中的哪一个 这个表达式可以以确定性形式重写为 x y z 谁能给我一个解释吗 为了具有确
  • Android 活动上下文为空

    所以我这里有这些代码 它运行时不会崩溃 但是 当我将 this 传递到网格适配器时 mContext 为空 我尝试传递 getApplicationContext 但仍然无法使 getImage 方法正常运行 因为 getResources