【问题标题】:android.app.ReceiverRestrictedContext cannot be cast to android.app.Activityandroid.app.ReceiverRestrictedContext 不能转换为 android.app.Activity
【发布时间】:2019-05-25 08:49:47
【问题描述】:

我已经阅读了android.app.Application cannot be cast to android.app.Activity,但我没有解决我的问题。我试着写代码

totalebevuto = (TextView) this.findViewById(R.id.totalebevuto);

按照帖子中的建议,但出现错误。

    public class MyReceiver2 extends BroadcastReceiver {

    WordListOpenHelper mDB;
    WordListAdapter mAdapter;

    TextView totalebevuto;

    @Override
    public void onReceive(Context context, Intent intent) {

        mDB = new WordListOpenHelper(context);
        // AGGIUNGERE LA DATA AL DATABASE
        totalebevuto = (TextView) ((Activity)context).findViewById(R.id.totalebevuto);
        mDB.insert_daily(Integer.parseInt(totalebevuto.getText().toString()));

        mDB.clear();

        mAdapter = new WordListAdapter(context, mDB);
        mAdapter.notifyDataSetChanged();

        Toast.makeText(context, "OnReceive2", Toast.LENGTH_LONG).show();

    }

}

【问题讨论】:

  • 显示你的完整代码
  • 你到底想做什么?
  • 我把所有的代码都贴出来了

标签: android


【解决方案1】:

我认为我从 MainActivity 获取价值的整个方法都是错误的。我在 SharedPreferences 中保存了我需要的值,然后在接收器中检索它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 2019-01-09
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多