在android中运行BroadcastReceiver时获取运行时异常?

时间:2015-06-12 05:14:24

标签: java android android-asynctask

我正在尝试在wifi网络更改或打开/关闭时执行AsyncTask,但是当我运行应用程序并更改wifi网络时,我得到了“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

在我的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(){

    }
}

1 个答案:

答案 0 :(得分:3)

由于1. SP_procedure1 2. SP_procedure2 3. SP_procedure3 BroadcastReceiver的内部类,因此将MainActivity设为静态内部类。

WifiReceiver

但是,如果您希望使用非静态内部类,则无法通过AndroidManifest.xml执行此操作。但是,您可以动态注册BroadcastReceiver:Receiver as inner class in Android