Java listView=(listView)findViewById(R.id.listView1)的问题;进入onReceive()

Java listView=(listView)findViewById(R.id.listView1)的问题;进入onReceive(),java,android,broadcastreceiver,Java,Android,Broadcastreceiver,我需要在广播接收器类中获取对象列表视图。。。 但是如果我写listView=(listView)findviewbyd(R.id.listView1)我得到一个错误 有什么想法吗 提前谢谢大家 我希望你能帮助我 public class AlarmReceiver extends BroadcastReceiver { private Intent intent; String value1 = null; String value2 = null; List

我需要在
广播接收器
类中获取对象
列表视图
。。。 但是如果我写
listView=(listView)findviewbyd(R.id.listView1)我得到一个错误

有什么想法吗

提前谢谢大家

我希望你能帮助我

public class AlarmReceiver extends BroadcastReceiver {


    private Intent intent;
    String value1 = null;
    String value2 = null;
    ListView lv;
    ArrayList<String> al;
    ArrayAdapter<String> aa;
    ArrayList<Sms> dataSms;
    public static CustomAdapter adapter;
    ListView listView;

 /*   @Override
    protected void onCreate(final Bundle savedInstanceState) {

        lv = (ListView) findViewById(R.id.listView1);

    }
*/
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

      //  TextView textView = (TextView) ((Activity) context).findViewById(R.id.textView1);
         listView = (ListView)findViewById(R.id.listView1);
}
公共类AlarmReceiver扩展了BroadcastReceiver{
私人意图;
字符串值1=null;
字符串值2=null;
ListView lv;
ArrayList al;
ArrayAdapter aa;
阵列列表数据;
公共静态自定义适配器;
列表视图列表视图;
/*@覆盖
创建时受保护的void(最终捆绑包savedInstanceState){
lv=(ListView)findViewById(R.id.listView1);
}
*/
@凌驾
公共void onReceive(上下文、意图){
//TODO自动生成的方法存根
//TextView TextView=(TextView)((活动)上下文).findViewById(R.id.textView1);
listView=(listView)findViewById(R.id.listView1);
}

创建一个类变量,并在活动的onCreate方法中对其进行初始化。初始化后,可以从类内的任何位置使用它

因此:

希望这有助于

干杯

试试:

LayoutInflater mInf = LayoutInflater.from(context);
View view = mInf.inflate(R.layout.activity_favorite_list, null);
ListView listView = view.findViewById(R.id.listView1);

发布代码和错误日志“错误:(39,31)错误:找不到符号方法findViewById(int)”请发布导致问题的代码。请确保包含类和方法声明以及所有相关变量声明。不要更新广播接收器类中的listView数据。在MainActivity类中创建一个公共静态方法,并在广播接收器类中调用该方法。在该方法中,在UI上执行您的代码阅读。参考此链接它取决于BroadcastReceiver…您需要创建活动/片段本身的内部非静态类,并且不要忘记在正确的生命周期事件中注销它。我无法将列表视图实例化到broadcast receiver类中的onReceive()中…不在Activity中您不在onReceive()中实例化listView。如上所述初始化listview,然后在onReceive()onReceive(..){lv.xxxxx;}中使用它。您的代码仍然会忽略以下内容:
super.onCreate(savedInstanceState);setContentView(R.layout.activityName);
紧跟在onCreate之后,其他任何内容之前
LayoutInflater mInf = LayoutInflater.from(context);
View view = mInf.inflate(R.layout.activity_favorite_list, null);
ListView listView = view.findViewById(R.id.listView1);