gpt4 book ai didi

java - 添加额外类时找不到 Android 源

转载 作者:行者123 更新时间:2023-12-02 07:22:51 25 4
gpt4 key购买 nike

我已经浏览了所有其他有此问题的帖子,但无济于事。

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

谢谢!

最佳答案

这里

public class GpsCheck extends android.app.Activity{

public GpsCheck(){

}
your code .....
}

这不是创建非 Activity 类来获取 Activity 上下文的正确方法。您需要将 Activity Context 传递给非 Activity 类以访问系统服务或其他 Activity 或应用程序组件

通过将 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 .....
}

并从您的 Activity 中发送当前 Activity 上下文:

GpsCheck gpscheckobj=new GpsCheck(MainActivity.this);

关于java - 添加额外类时找不到 Android 源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13979697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com