0

我正在实现在 Internet 离线时显示对话框,当我运行我的应用程序时,当我单击按钮并且应用程序崩溃时,我得到“FATAL Exception main”和 ClassCastException。有人可以告诉我我做错了什么吗?感谢您在高级。

这是我如何检查是否启用互联网的代码:

public class AndroidDetectInternetConnectionActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btnStatus = (Button) findViewById(R.id.btn_check);

        btnStatus.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                if (!isOnline())
                {
                    showNoConnectionDialog(this);

                }
            }

        });

    }

    public static void showNoConnectionDialog(OnClickListener onClickListener) 
    {
        final Context ctx = (Context) onClickListener;
        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setCancelable(true);
        builder.setMessage(R.string.no_connection);
        builder.setTitle(R.string.no_connection_title);
        builder.setPositiveButton(R.string.settings_button_text, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) 
            {

                ctx.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
            }
        });

        builder.setNegativeButton(R.string.cancel_button_text, new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int which) 
            {
                return;
            }
        });

        builder.setOnCancelListener(new DialogInterface.OnCancelListener() 
        {
            public void onCancel(DialogInterface dialog) {
                return;
            }
        });

        builder.show();
    }

    public boolean isOnline() 
    {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) 
        {
            return true;
        }
        return false;
    }

}

// 这是我的 Log Cat 堆栈跟踪

11-15 11:57:19.115: D/AndroidRuntime(453): Shutting down VM
11-15 11:57:19.115: W/dalvikvm(453): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-15 11:57:19.122: E/AndroidRuntime(453): FATAL EXCEPTION: main
11-15 11:57:19.122: E/AndroidRuntime(453): java.lang.ClassCastException: com.example.detectinternetconnection.AndroidDetectInternetConnectionActivity$1
11-15 11:57:19.122: E/AndroidRuntime(453):  at com.example.detectinternetconnection.AndroidDetectInternetConnectionActivity.showNoConnectionDialog(AndroidDetectInternetConnectionActivity.java:99)
11-15 11:57:19.122: E/AndroidRuntime(453):  at com.example.detectinternetconnection.AndroidDetectInternetConnectionActivity$1.onClick(AndroidDetectInternetConnectionActivity.java:64)
11-15 11:57:19.122: E/AndroidRuntime(453):  at android.view.View.performClick(View.java:2485)
11-15 11:57:19.122: E/AndroidRuntime(453):  at android.view.View$PerformClick.run(View.java:9080)
11-15 11:57:19.122: E/AndroidRuntime(453):  at android.os.Handler.handleCallback(Handler.java:587)
11-15 11:57:19.122: E/AndroidRuntime(453):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-15 11:57:19.122: E/AndroidRuntime(453):  at android.os.Looper.loop(Looper.java:123)
11-15 11:57:19.122: E/AndroidRuntime(453):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-15 11:57:19.122: E/AndroidRuntime(453):  at java.lang.reflect.Method.invokeNative(Native Method)
11-15 11:57:19.122: E/AndroidRuntime(453):  at java.lang.reflect.Method.invoke(Method.java:507)
11-15 11:57:19.122: E/AndroidRuntime(453):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-15 11:57:19.122: E/AndroidRuntime(453):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-15 11:57:19.122: E/AndroidRuntime(453):  at dalvik.system.NativeStart.main(Native Method)
11-15 11:57:24.892: I/Process(453): Sending signal. PID: 453 SIG: 9
4

3 回答 3

2

有两种方法可以解决这个问题。

1)showNoConnectionDialog(this);及以后: public static void showNoConnectionDialog(Context ctx) ...

2)showNoConnectionDialog();及以后:public void showNoConnectionDialog() { Context ctx = AndroidDetectInternetConnectionActivity.this

于 2013-11-15T07:34:46.427 回答
2

更改此行

 final Context ctx = (Context) onClickListener;

低于一

 final Context ctx = AndroidDetectInternetConnectionActivity.this;

基本上,您正在尝试将 onClickListener 转换为不正确且无法转换的 Contex。您可以直接ActivityName.this在需要上下文实例的任何地方使用,或者定义static Context ctx为类变量并onCreate()通过添加此行ctx =this来初始化它,还记得在使用它之前对其进行初始化。

享受

于 2013-11-15T06:43:39.103 回答
0

您的基本问题是由以下行产生的:

final Context ctx = (Context) onClickListener;

这根本不是一个上下文,所以试图强迫它成为一个是行不通的。

我相信您想要做的是将上下文(或活动)传递给此函数(与您现在传递的本地未命名 OnClickListener 类相反)

最简单的解决方案是简单地不将任何内容传递给构造函数并使用 AndroidDetectInternetConnectionActivity.this 来访问您的有效上下文。

于 2013-11-15T06:43:53.473 回答