因此,我正在尝试实现一个AlertDialog,该对话框应在按下“激活”按钮时打开,但是在按下该按钮时,应用程序将崩溃。

这是我的代码。

主要活动-

public class MainActivity extends Activity {

Button activateButton;


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

    activateButton = (Button)findViewById(R.id.activate);
    activateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Speed s = new Speed();
        }
    });

}


这是另一个名为Speed的类,其中包含AlertDialog的代码

public class Speed extends Activity {
    LocationManager mManager;
    final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
    AlertDialog.Builder box = new AlertDialog.Builder(this);
    Speed(){
        Status();
    }

    private void Status(){

        if(!mManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            DialogBox();
        }
    }

    protected void DialogBox(){
        AlertDialog.Builder box = new AlertDialog.Builder(this);
        box.setTitle("The GPS needs to be enabled. Would you like to enable the GPS?").setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int id){
                        startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                    }
                }).setNegativeButton("No", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id){
                dialog.cancel();
            }
        });
        final AlertDialog alert = box.create();
        alert.show();

    }

}


另外,这是上下文中有错误的日志

10-01 21:27:00.439: E/AndroidRuntime(5503): FATAL EXCEPTION: main
10-01 21:27:00.439: E/AndroidRuntime(5503): java.lang.IllegalStateException: System services not available to Activities before onCreate()
10-01 21:27:00.439: E/AndroidRuntime(5503):     at android.app.Activity.getSystemService(Activity.java:3989)
10-01 21:27:00.439: E/AndroidRuntime(5503):     at com.scimet.admin.driveon.Speed.<init>(Speed.java:12)
10-01 21:27:00.439: E/AndroidRuntime(5503):     at com.scimet.admin.driveon.MainActivity$1.onClick(MainActivity.java:25)
10-01 21:27:00.439: E/AndroidRuntime(5503):     at android.view.View.performClick(View.java:3534)
10-01 21:27:00.439: E/AndroidRuntime(5503):     at android.view.View$PerformClick.run(View.java:14263)
10-01 21:27:00.439: E/AndroidRuntime(5503):     at android.os.Handler.handleCallback(Handler.java:605)
10-01 21:27:00.439: E/AndroidRuntime(5503):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-01 21:27:00.439: E/AndroidRuntime(5503):     at android.os.Looper.loop(Looper.java:137)
10-01 21:27:00.439: E/AndroidRuntime(5503):     at android.app.ActivityThread.main(ActivityThread.java:4441)
10-01 21:27:00.439: E/AndroidRuntime(5503):     at java.lang.reflect.Method.invokeNative(Native Method)
10-01 21:27:00.439: E/AndroidRuntime(5503):     at java.lang.reflect.Method.invoke(Method.java:511)
10-01 21:27:00.439: E/AndroidRuntime(5503):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-01 21:27:00.439: E/AndroidRuntime(5503):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-01 21:27:00.439: E/AndroidRuntime(5503):     at dalvik.system.NativeStart.main(Native Method)


这是我的第一个Android项目。在搜索多个小时以尝试解决此问题时,我最终没有任何解决方案。请指出错误。

谢谢!

最佳答案

您应将此代码放在onCreate方法中,因为它将在创建活动时运行。

例如

在您的课程中删除构造函数。

然后在活动的onCreate方法上执行以下操作:

public class MainActivity extends Activity {
  LocationManager manager;
  AlertDialog.Builder box;

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    box = new AlertDialog.Builder(this);
    manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
    Status();
  }
}

关于java - 尝试实现getSystemService()的onCreate()之前的 Activity 无法使用的系统服务,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26145664/

10-12 02:41