我读过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();
}
}
发布于 2019-05-25 17:53:26
我认为我从MainActivity获取价值的整个方法都是错误的。我将所需的值保存在SharedPreferences中,然后在接收器中检索它。
https://stackoverflow.com/questions/56303264
复制相似问题