listView =(ListView)findViewById(R.id.listView1);进入onReceive()

时间:2017-01-20 13:39:54

标签: java android broadcastreceiver

我需要在list view班级中获取对象broadcast receiver ... 但如果我写listView = (ListView) findViewById(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);
}

2 个答案:

答案 0 :(得分:0)

创建一个类变量并在您的活动的onCreate方法中初始化它。一旦初始化,它可以在班级内的任何地方使用......

这样:

private ListView lv;
@Override
protected void onCreate(final Bundle savedInstanceState) {
       ...
       lv = (ListView) findViewById(R.id.listView1);
       ...
}

希望有帮助...

干杯

答案 1 :(得分:0)

尝试:

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