Android:进行全局功能时出错

时间:2013-12-11 05:38:01

标签: java android function global

我正在尝试创建一个全局函数类,但是当我使用它时似乎遇到了错误。 当我在课堂上一起使用它时,它工作正常。实际上我放在Helper.java上的每个代码总是会出错。任何人都知道为什么?

MainActivity.java:

package com.example.myfirstapp;

public class MainActivity extends Activity implements OnClickListener {

Helper application = (Helper) getApplication();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

if (isNetworkConnected(getApplicationContext())) {

        } else {
            application.AlertWifi("No Internet Connection\nWould you like to turn on the wifi?");
        }
}

Helper.java:

package com.example.myfirstapp;

import android.app.Application;

public class Helper extends Application {

public void AlertWifi(String msg) {
    AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
    dlgAlert.setMessage(msg);
    dlgAlert.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            dialog.dismiss();
        }
    });
    dlgAlert.setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    startActivity(new Intent(
                            android.provider.Settings.ACTION_WIFI_SETTINGS));

                }
            });
    dlgAlert.setCancelable(false);
    dlgAlert.create().show();
}

}

2 个答案:

答案 0 :(得分:0)

AlertDialog.Builder dlgAlert = new AlertDialog.Builder(Helper.this);

答案 1 :(得分:0)

//We will pass our Activity context
public void AlertWifi( Context ctx,String msg) {
    AlertDialog.Builder dlgAlert = new AlertDialog.Builder(ctx);
    dlgAlert.setMessage(msg);
    dlgAlert.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            dialog.dismiss();
        }
    });
    dlgAlert.setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    Intent i=new Intent(
                            android.provider.Settings.ACTION_WIFI_SETTINGS);
                            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(i);

                }
            });
    dlgAlert.setCancelable(false);
    dlgAlert.create().show();
}

}

在Activity OnCreate中

Helper application = (Helper) getApplication();
//this refer to our Activity
application.AlertWifi(this,"No Internet Connection\nWould you like to turn on the wifi?" );

加上AndroidManifest.xml

 <application 
        android:name="Helper"

这里没有旗帜。我将环境保存为全球弱势群体。所以在Onclick里面我可以把它拿回来。

WeakReference<Context> wm;
    /**
     * 
     * @param ctx Activity context
     * @param msg Message that will be displayed
     */
public void AlertWifi( Context ctx,String msg) {
    AlertDialog.Builder dlgAlert = new AlertDialog.Builder(ctx);
    wm=new WeakReference<Context>(ctx);
    dlgAlert.setMessage(msg);
    dlgAlert.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            dialog.dismiss();
        }
    });
    dlgAlert.setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    Context ctx=wm.get();
                    if(ctx!=null){
                    Intent i=new Intent(
                            android.provider.Settings.ACTION_WIFI_SETTINGS);
                            //i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                      ctx.startActivity(i);
                    }

                }
            });
    dlgAlert.setCancelable(false);
    dlgAlert.create().show();
}