我試圖執行一個AsyncTask時,無線網絡更改或開關開關,但是當我運行應用程序並更改WiFi網絡時,我得到了「InstantiationException」。在android中運行BroadcastReceiver時出現運行時異常?
06-12 10:31:59.881 24464-24464/com.poliveira.apps.materialtests E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate receiver com.poliveira.apps.test.MainActivity$WifiReceiver: java.lang.InstantiationException: can't instantiate class com.poliveira.apps.test.MainActivity$WifiReceiver; no empty constructor
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2357)
at android.app.ActivityThread.access$1500(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.InstantiationException: can't instantiate class com.poliveira.apps.test.MainActivity$WifiReceiver; no empty constructor
在我的Manifest.xml我有以下
<!-- Receive Wi-Fi connection state changes -->
<receiver android:name=".MainActivity$WifiReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
在MainActivity,我用這個類。
public class WifiReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMan.getActiveNetworkInfo();
if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI){
mToolbarSpinnerNavigationItems.clear();
mToolbarSpinnerNavigationItems.add(new SpinnerNavItem("Select your Device", R.drawable.deviceonline));
mArrayAdapter = new ArrayAdapter(MainActivity.this, R.layout.list_item_title_navigation,mToolbarSpinnerNavigationItems){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = View.inflate(MainActivity.this, R.layout.list_item_title_navigation, null);
// get view
TextView tvText1 = (TextView) convertView.findViewById(R.id.devicetitle);
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.deviceicon);
imgIcon.setVisibility(View.GONE);
//imgIcon.setImageResource(mToolbarSpinnerNavigationItems.get(position).getIcon());
// set content
tvText1.setText(mToolbarSpinnerNavigationItems.get(position).getTitle());
// return
return convertView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// implement the same way as getView() method
// inflate layout
convertView = View.inflate(MainActivity.this, R.layout.list_item_title_navigation, null);
// get view
TextView tvText1 = (TextView) convertView.findViewById(R.id.devicetitle);
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.deviceicon);
imgIcon.setVisibility(View.GONE);
//imgIcon.setImageResource(mToolbarSpinnerNavigationItems.get(position).getIcon());
// set content
tvText1.setText(mToolbarSpinnerNavigationItems.get(position).getTitle());
// return
return convertView;
}
};
//setting adapter to Spinner
mToolbarSpinner.setAdapter(mArrayAdapter);
new selectingDevices().execute();
}
else {
Log.d("WifiReceiver", "Don't have Wifi Connection");
AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
builder1.setTitle("Wifi not enabled");
builder1.setMessage("Please turn on your phone wifi");
builder1.setCancelable(true);
builder1.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alertDialog1;
alertDialog1 = builder1.create();
alertDialog1.show();
}
}
}
這是從AsyncTask繼承的類。這也在MainActivity類中。
private class selectingDevices extends AsyncTask
{
@Override
protected Object doInBackground(Object[] params) {
if (!isCancelled())
{
startBonjour();
}
return null;
}
@Override
protected void onCancelled(){
}
}
我認爲您必須瞭解如何爲信息擴展asynctask http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html –
thnx。這不是asynctask的問題。當WiFi改變應用程序將例外WifiReceiver類。那個班正在給予例外。 –