Java 如何使用独立类的getter获取wifiList?
我使用Wifi创建了一个项目。我实现了一个独立类(WifiScanReceiver)扩展了BroadcastReceiver。我尝试从MainActivity调用独立类。我想在触摸ImageView时触发wifiList alertdialog。现在我得到了空指针异常 在这里,它显示了另一个错误Java 如何使用独立类的getter获取wifiList?,java,android,android-wifi,Java,Android,Android Wifi,我使用Wifi创建了一个项目。我实现了一个独立类(WifiScanReceiver)扩展了BroadcastReceiver。我尝试从MainActivity调用独立类。我想在触摸ImageView时触发wifiList alertdialog。现在我得到了空指针异常 在这里,它显示了另一个错误,您需要将一个Theme.AppCompat主题(或子代)用于此活动 请帮我解决这些问题。任何帮助我都将不胜感激 main活动: public class MainActivity extends App
,您需要将一个Theme.AppCompat主题(或子代)用于此活动
请帮我解决这些问题。任何帮助我都将不胜感激
main活动
:
public class MainActivity extends AppCompatActivity {
private boolean isWifiStart = false;
WifiManager wifiManager;
String[] strWifiList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
ImageView imgView = (ImageView)findViewById(R.id.floorPlan);
imgView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (isWifiStart) {
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflateView = inflater.inflate(R.layout.dialog_wifi_list, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getBaseContext());
WifiScanReceiver receiver = new WifiScanReceiver(wifiManager);
registerReceiver(receiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifiManager.startScan();
try {
strWifiList = receiver.getWifiListString();
} catch (Exception e) {
shortMsg(getBaseContext(),"Exception : " + e.getMessage());
}
ListView wifiList = (ListView)inflateView.findViewById(R.id.list_Wifi);
builder.setView(inflateView);
wifiList.setAdapter(new ArrayAdapter<>(getBaseContext(), android.R.layout.simple_list_item_1,strWifiList));
builder.setView(inflateView);
builder.show();
}
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.wifiStart:
shortMsg(this,"Wifi Start");
isWifiStart = true;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void shortMsg(Context ctx,String msg) {
Toast.makeText(ctx,msg,Toast.LENGTH_SHORT).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.rewifiheatmap" >
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
请粘贴准确的错误代码。@HoseinIT这是准确的代码朋友。.我没有从任何项目中裁剪这些代码。.错误消息是不言自明的。但请同时发布您的清单和styles.xml。@NathanWalters当然可以兄弟..@NathanWalters现在您看到兄弟了。。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>