Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:在ListView中显示广播接收器_Android_Listview_Broadcastreceiver_Wifi - Fatal编程技术网

Android:在ListView中显示广播接收器

Android:在ListView中显示广播接收器,android,listview,broadcastreceiver,wifi,Android,Listview,Broadcastreceiver,Wifi,我创建了一个广播接收器来跟踪我的Wi-Fi,我在LogCat中获得了正常的信息,但我无法将数据放入列表视图,就像我不想那样。 我在onReceive方法中放置了一个ListView示例来测试它,但我没有工作,我遇到了以下错误: 构造器阵列适配器(Wifi.Receiver,int,int, 字符串[]未定义 这是我的代码: public class Wifi extends Activity implements OnClickListener{ /** Called when the acti

我创建了一个广播接收器来跟踪我的Wi-Fi,我在LogCat中获得了正常的信息,但我无法将数据放入列表视图,就像我不想那样。 我在
onReceive
方法中放置了一个ListView示例来测试它,但我没有工作,我遇到了以下错误:

构造器阵列适配器(Wifi.Receiver,int,int, 字符串[]未定义

这是我的代码:

public class Wifi extends Activity implements OnClickListener{
/** Called when the activity is first created. */
WifiManager wifi;
Button      enab;
String resultsString ; 
String[] myStringArray;

public class Receiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {

    }
ListView listView ;
            // Get ListView object from xml
            listView = (ListView) findViewById(R.id.listView1);

            // Defined Array values to show in ListView
        String[] values = new String[] { "Android List View", 
                                         "Adapter implementation",
                                         "Simple List View In Android",
                                         "Create List View Android", 
                                         "Android Example", 
                                         "List View Source Code", 
                                         "List View Array Adapter", 

                                        };
//getting this error : The constructor ArrayAdapter<String>(Wifi.Receiver, int, int, //String[]) is undefined           
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, values);


        listView.setAdapter(adapter);

}
}
公共类Wifi扩展活动{
/**在首次创建活动时调用*/
WifiManager-wifi;
按钮激活;
字符串结果字符串;
字符串[]myStringArray;
公共类接收器扩展了广播接收器{
@凌驾
公共void onReceive(上下文、意图){
}
列表视图列表视图;
//从xml获取ListView对象
listView=(listView)findViewById(R.id.listView1);
//要在ListView中显示的已定义数组值
字符串[]值=新字符串[]{“Android列表视图”,
“适配器实现”,
“Android中的简单列表视图”,
“创建列表视图”,
“Android示例”,
“列表查看源代码”,
“列表视图阵列适配器”,
};
//获取此错误:未定义构造函数ArrayAdapter(Wifi.Receiver,int,int,//String[])
ArrayAdapter=新的ArrayAdapter(这个,android.R.layout.simple_list_item_1,android.R.id.text1,值);
setAdapter(适配器);
}
}

PS:当我将ListView的代码放在
onCreate
方法中时,它工作正常,但我希望它放在
onReceive
方法中。

在您的情况下
不涉及活动上下文

使用

所以第一个参数是一个有效的上下文

java.lang.Object
   ↳    android.content.Context // see context
       ↳    android.content.ContextWrapper
           ↳    android.view.ContextThemeWrapper
               ↳    android.app.Activity // see activity
你的班级扩展了活动

public class Wifi extends Activity 

因此,请使用Wifi。此作为有效的上下文。

@Amina我知道您已经提出了7个问题,均未接受。点击答案旁边的勾号,接受答案。阅读感谢您的解释:)还有2分钟,我接受,您刚刚编辑(7分钟后我可以接受答案:)@Amina我理解。这是网站的限制,请检查编辑后的答案。你会知道我为什么使用Wifi。这个
java.lang.Object
   ↳    android.content.Context // see context
       ↳    android.content.ContextWrapper
           ↳    android.view.ContextThemeWrapper
               ↳    android.app.Activity // see activity
public class Wifi extends Activity