简体   繁体   中英

Getting Runtime Exception when run the BroadcastReceiver in android?

I'm trying to execute an AsyncTask when wifi network change or switch on/off,but when I run the app and change wifi network I got "InstantiationException".

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

In my Manifest.xml I'm having following

 <!-- Receive Wi-Fi connection state changes -->
    <receiver android:name=".MainActivity$WifiReceiver">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

In MainActivity, I used this class.

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();
        }

    }
}

this the class that extends from AsyncTask.This is also in the MainActivity class.

 private class selectingDevices extends AsyncTask
{

    @Override
    protected Object doInBackground(Object[] params) {
        if (!isCancelled())
        {
            startBonjour();
        }

        return null;
    }

    @Override
    protected void onCancelled(){

    }
}

Since you BroadcastReceiver is an inner class of MainActivity , so make WifiReceiver as static inner class.

public static class WifiReceiver extends BroadcastReceiver {
....... Your code.
}

However if you want to this with a non-static inner class, you can't do it via the AndroidManifest.xml. You can however dynamically register the BroadcastReceiver: Receiver as inner class in Android

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM