Java Android中广播接收器的奇怪问题

Java Android中广播接收器的奇怪问题,java,android,listview,broadcastreceiver,Java,Android,Listview,Broadcastreceiver,我有一段非常简单的代码。我有一个列表,在onCreate方法中,我将一些对象添加到此列表中,以便在屏幕上显示它们。我有一个广播接收器,当没有互联网连接时,它必须启用/禁用列表中的某些元素 当应用程序已经在该活动的屏幕中时,如果连接丢失,则广播接收器工作正常。问题是在进入此活动之前没有连接。在这种情况下,在onresume()中调用oncreate()方法之后,接收器被注册,但是当我在接收器中调用getListView()时,它没有任何子级(尽管我已经在oncreate方法中添加到适配器,并且我没

我有一段非常简单的代码。我有一个列表,在onCreate方法中,我将一些对象添加到此列表中,以便在屏幕上显示它们。我有一个广播接收器,当没有互联网连接时,它必须启用/禁用列表中的某些元素

当应用程序已经在该活动的屏幕中时,如果连接丢失,则广播接收器工作正常。问题是在进入此活动之前没有连接。在这种情况下,在onresume()中调用oncreate()方法之后,接收器被注册,但是当我在接收器中调用getListView()时,它没有任何子级(尽管我已经在oncreate方法中添加到适配器,并且我没有使用任何线程加载)

谁能告诉我为什么会这样

public class MyActivity extends ListActivity {
    private List<MyClass> myObjects;

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            //check if internet connection is available
            boolean networkAvailable = ... ;
            if (!networkAvailable) {
                //No Internet connection: disabled non-cached objects
                List<MyClass> cachedObjects = getCachedObjects();
                for(int i = 0; i<myObjects.size(); i++){
                    MyClass myObject = myObjects.get(i);
                    if (!cachedSurveys.contains(myObject)) {
                        ListView listView = getListView();
                        //The problem is here: listView is empty when there was no connection
                        //before creating the activity so the broadcast receiver was called in a sticky way                     
                        View child = listView.getChildAt(i);
                        child.setEnabled(false);

                    }
                }
            } else {
                // Internet connection: enable all myObjects
                 int size = getListView().getChildCount();
                 for (int i = 0; i < size; i++) {
                    View child = getListView().getChildAt(i);
                    child.setEnabled(true);
                }
            }
        }
     }; 

     @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        myObjects = getMyObjects();

        setListAdapter(new ArrayAdapter<MyClass>(this, android.R.layout.simple_list_item_1, myObjects));
        getListView().setTextFilterEnabled(true);   
}

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
        registerReceiver(receiver, intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }
}
公共类MyActivity扩展了ListActivity{
私有列表对象;
专用最终BroadcastReceiver=新的BroadcastReceiver(){
@凌驾
公共void onReceive(上下文、意图){
//检查互联网连接是否可用
布尔网络可用=;
如果(!网络可用){
//没有Internet连接:已禁用非缓存对象
List cachedObjects=getCachedObjects();

对于(int i=0;i首先,我认为我们应该了解一些事实:

  • 调用onResume()方法后,活动将开始绘制其内容,但不是“立即”之后
  • 调用ListView.setListAdapter(…)后,ListView仅存储适配器对象,此时没有子视图
  • 在ListView在屏幕上绘制该子视图之后(在调用Adapter.getView()之后),子视图将可用,并且每次ListView添加一个子视图时,子视图列表将逐个增加,直到ListView在屏幕上绘制所有子视图为止
对于您的代码,您无法在活动第一次启动时获取子视图列表的原因是“ListView没有在屏幕上绘制任何内容”。因为接收器的onReceive()方法是在ListView绘制之前触发的

您可以通过重写适配器和show log来检查这些方法的调用顺序。 以下是我的结果:

07-30 23:06:03.231: DEBUG/MyActivity(386): onResume 1280505963237   
07-30 23:06:03.281: DEBUG/MyActivity(386): onReceive 1280505963281
07-30 23:06:03.361: DEBUG/MyActivity(386): getView 0 1280505963371
07-30 23:06:03.381: DEBUG/MyActivity(386): getView 1 1280505963386

希望我的回答能对你有所帮助!

首先,我认为我们应该了解一些事实:

  • 调用onResume()方法后,活动将开始绘制其内容,但不是“立即”之后
  • 调用ListView.setListAdapter(…)后,ListView仅存储适配器对象,此时没有子视图
  • 在ListView在屏幕上绘制该子视图之后(在调用Adapter.getView()之后),子视图将可用,并且每次ListView添加一个子视图时,子视图列表将逐个增加,直到ListView在屏幕上绘制所有子视图为止
对于您的代码,您无法在活动第一次启动时获取子视图列表的原因是“ListView没有在屏幕上绘制任何内容”。因为接收器的onReceive()方法是在ListView绘制之前触发的

您可以通过重写适配器和show log来检查这些方法的调用顺序。 以下是我的结果:

07-30 23:06:03.231: DEBUG/MyActivity(386): onResume 1280505963237   
07-30 23:06:03.281: DEBUG/MyActivity(386): onReceive 1280505963281
07-30 23:06:03.361: DEBUG/MyActivity(386): getView 0 1280505963371
07-30 23:06:03.381: DEBUG/MyActivity(386): getView 1 1280505963386
希望我的回答能帮助你