添加额外课程时未找到Android源代码

时间:2012-12-20 20:09:43

标签: java android eclipse

我查看了所有其他有此问题的帖子,但无济于事。

我创建了一个应用程序,当所有代码都放在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;
    }

}

的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>

谢谢!

2 个答案:

答案 0 :(得分:2)

这里

public class GpsCheck extends android.app.Activity{

    public GpsCheck(){

  }
      your code  .....
}

这不是创建非Activity类以获取Activity of Context的正确方法。您需要将Activity Context传递给非Activity类以访问系统服务或其他Activity或Application组件

通过将Context传递给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);

答案 1 :(得分:0)

活动:

final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

    if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
        buildAlertMessageNoGps();
    }

在片段(非活动类)中:

Boolean isGPSEnabled = false;
        final LocationManager manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE );

        isGPSEnabled = manager.isProviderEnabled( LocationManager.GPS_PROVIDER );