我浏览了所有其他有此问题的帖子,但无济于事。
我创建了一个应用程序,当所有代码都包含在 MainActivity 中时,它可以完美运行,但是当我创建一个类来检查 GPS 并清理我的 MainActivity 源文件时,我在调试时收到一个错误,提示“源不是成立”。
就此而言,我是 android 和 java 的新手(我通常使用 c 和 c++),所以我不知道这是否与我的 manifest.xml 等有关,但这让我发疯了!
我在下面包含了我的代码以及我的 manifest.xml 文件。
当逐行浏览我的代码时,给我这个错误的行是final LocationManager manager = (LocationManager) getSystemService(LOCATION_SERVICE);
GpsCheck.java
package com.omegaapp.openomega;
import android.os.*;
import android.location.*;
import android.location.GpsStatus.Listener;
import android.app.*;
import android.content.*;
import com.omegaapp.*;
public class GpsCheck extends android.app.Activity{
public GpsCheck(){
final LocationManager manager = (LocationManager) getSystemService(LOCATION_SERVICE);
Listener gpsStatusListener = new Listener(){
@Override
public void onGpsStatusChanged(int arg0) {
gpsEnable();
}
};
manager.addGpsStatusListener(gpsStatusListener);
}
public boolean gpsEnable(){
//checks weather the GPS is enabled. If not, prompts the user to enable it then checks again. Returns false if the user chooses not to enable the application.
if(!checkGpsEnabled()){
promptEnableGps();
}
return true;
}
private boolean checkGpsEnabled(){
final LocationManager manager = (LocationManager) getSystemService(LOCATION_SERVICE);
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
return false;
}
return true;
}
private boolean promptNoGpsQuit(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Without enabling GPS this app can not function, Would you like to quit?");
alertDialogBuilder.setCancelable(false);
DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
}
};
alertDialogBuilder.setPositiveButton("Goto Settings Page To Enable GPS", onClick);
DialogInterface.OnClickListener onClickCancel = new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
finish();
}
};
alertDialogBuilder.setNegativeButton("Quit", onClickCancel);
AlertDialog alert = alertDialogBuilder.create();
alert.show();
return true;
}
private boolean promptEnableGps(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?");
alertDialogBuilder.setCancelable(false);
DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
}
};
alertDialogBuilder.setPositiveButton("Goto Settings Page To Enable GPS", onClick);
DialogInterface.OnClickListener onClickCancel = new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
promptNoGpsQuit();
}
};
alertDialogBuilder.setNegativeButton("Cancel", onClickCancel);
AlertDialog alert = alertDialogBuilder.create();
alert.show();
return true;
}
}
清单.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.omegaapp.openomega"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.omegaapp.openomega.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
谢谢!