我使用Wifi创建一个项目。我实现了一个独立的类(WifiScanReceiver)扩展BroadcastReceiver。我尝试从MainActivity调用独立类。我想在触摸ImageView时触发wifiList alertdialog。现在我得到了空指针异常。
在此处显示另一个错误You need to use a Theme.AppCompat theme (or descendant) with this activity
。
请帮我解决这些问题。任何帮助我都会非常感激。
MainActivity
:
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();
}
}
WifiScanReceiver
:(独立课程)
public class WifiScanReceiver extends BroadcastReceiver {
private String[] wifiListString;
private WifiManager wifiMgr;
public WifiScanReceiver(WifiManager manager) {
this.wifiMgr = manager;
}
@Override
public void onReceive(Context context, Intent intent) {
List<ScanResult> wifiScanList = wifiMgr.getScanResults();
Collections.sort(wifiScanList, new Comparator<ScanResult>() {
@Override
public int compare(ScanResult lhs, ScanResult rhs) {
return (lhs.level > rhs.level ? -1 : (lhs.level == rhs.level ? 0 : 1));
}
});
wifiListString = new String[wifiScanList.size()];
for (int i = 0; i < wifiScanList.size(); i++) {
wifiListString[i] = (wifiScanList.get(i).SSID);
}
}
public String[] getWifiListString() {
return wifiListString;
}
}
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>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>