我正在尝试要求用户启用GPS,我是应用程序开发的新手。我在互联网上找到了一些符合我的需求的资源。我从一个网站中获取了代码并实现到我的应用程序中,但是当我尝试构建时,我遇到了一个cannot find symbol
错误。
请查看下面的Java代码:
package gps.gpstest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
Context context;
private Context activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(!statusOfGPS)
{
buildAlertMessageNoGps();
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
builder.setMessage(R.string.gps_disabled)
.setCancelable(false)
.setTitle(R.string.gps_disabled_title)
.setPositiveButton(R.string.enable,
new DialogInterface.OnClickListener() {
public void onClick(
@SuppressWarnings("unused") final DialogInterface dialog,
@SuppressWarnings("unused") final int id) {
startActivity(new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog,
@SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
}
构建过程中出现错误
Error:(31, 73) error: 找不到getActivity()方法
Error:(44, 44) error: 找不到cancel变量
Error:(35, 44) error: 找不到enable变量
Error:(34, 35) error: 找不到gps_disabled_title变量
Error:(32, 36) error: 找不到gps_disabled变量
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
请帮我解决问题