Java 添加额外类时未找到Android源
我已经浏览了所有其他有这个问题的帖子,但没有结果 我已经创建了一个应用程序,当所有代码都放在MainActivity中时,它可以完美地工作,但是当我创建一个类来检查GPS,并清理MainActivity源文件时,我在调试时遇到一个错误,上面写着“找不到源” 我不熟悉android和java(我通常使用c&c++),所以我不知道这是否与manifest.xml之类的东西有关,但这让我发疯了 我已经在下面包含了我的代码以及manifest.xml文件 当逐行检查我的代码时,给出此错误的行是Java 添加额外类时未找到Android源,java,android,eclipse,Java,Android,Eclipse,我已经浏览了所有其他有这个问题的帖子,但没有结果 我已经创建了一个应用程序,当所有代码都放在MainActivity中时,它可以完美地工作,但是当我创建一个类来检查GPS,并清理MainActivity源文件时,我在调试时遇到一个错误,上面写着“找不到源” 我不熟悉android和java(我通常使用c&c++),所以我不知道这是否与manifest.xml之类的东西有关,但这让我发疯了 我已经在下面包含了我的代码以及manifest.xml文件 当逐行检查我的代码时,给出此错误的行是final
final LocationManager=(LocationManager)getSystemService(LOCATION\u 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;
}
}
Manifest.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>
谢谢大家! 这里
public class GpsCheck extends android.app.Activity{
public GpsCheck(){
}
your code .....
}
这不是创建非活动类以获取活动上下文的正确方法。您需要将活动上下文传递给非活动类,以便访问系统服务或其他活动或应用程序组件
通过将上下文传递给GpsCheck构造函数,将GpsCheck更改为,使其工作:
public class GpsCheck{
Context context;
LocationManager manager;
public GpsCheck(Context context){
this.context=context;
this.manager = (LocationManager)context.getSystemService(LOCATION_SERVICE);
//your code here
}
your code .....
}
并从活动中发送当前活动上下文,如下所示:
GpsCheck gpscheckobj=new GpsCheck(MainActivity.this);
在活动中:
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
}
在片段(非活动类)中:
“找不到源代码”通常在应用程序崩溃时打开,请在问题中发布您的LogCat错误,以便我们可以帮助您。LogCat除了应用程序已挂起外,什么也不显示。当我实现this.manager=(LocationManager)context.getSystemService(LOCATION\u服务)代码>我得到一个错误,位置\服务无法解析为变量。但是context.LOCATION_服务可以工作,但是稍后在if(!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)下运行
我遇到了和以前一样的问题。@Reid:我的答案只是参考如何解决您当前的问题,而不是有效的解决方案。因此,您需要在整个GpsCheck类中使用context
,而不是GpsCheck
来访问服务或UI元素
Boolean isGPSEnabled = false;
final LocationManager manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE );
isGPSEnabled = manager.isProviderEnabled( LocationManager.GPS_PROVIDER );