如何解决Gradle错误找不到符号

4

我正在尝试要求用户启用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'.

请帮我解决问题


请使用 MainActivity.this 代替。 - Syed Danish Haider
3个回答

0
错误:(31,73)错误:找不到符号方法getActivity()
getActivity()”用于在片段中获取上下文,而不是在活动中。
要在活动中获取上下文,请使用
  • this
  • YourActvity.this
  • getApplicationContext()
请使用此方法。
final AlertDialog.Builder builder = new AlertDialog.Builder(context);

不要用这个

final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());

错误:找不到符号变量“enable” 错误:(34,35)错误:找不到符号变量“gps_disabled_title” 错误:(32,36)错误:找不到符号变量“gps_disabled”
您需要在 string.xml 中创建以下资源,请查看 String resources
  • gps_disabled
  • cancel
  • enable
  • gps_disabled_title
  • gps_disabled

你能帮我解决这些错误吗? 错误:(44,44)错误:找不到符号变量cancel 错误:(35,44)错误:找不到符号变量enable 错误:(34,35)错误:找不到符号变量gps_disabled_title 错误:(32,36)错误:找不到符号变量gps_disabled - Jason
@Jason 确定,我会尝试。 - AskNilesh
我刚才已经为你指出了错误。 - Jason
@Jason,请检查更新后的答案。 - AskNilesh

0
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

你能帮我解决这些错误吗?Error:(44, 44) error: cannot find symbol variable cancel Error:(35, 44) error: cannot find symbol variable enable Error:(34, 35) error: cannot find symbol variable gps_disabled_title Error:(32, 36) error: cannot find symbol variable gps_disabled - Jason
builder.setMessage("请启用") //此行 - Syed Danish Haider
.setTitle("GPS未启用")); - Syed Danish Haider

0

让我们来看看你的错误:

错误:(44,44)错误:找不到符号变量cancel

错误:(35,44)错误:找不到符号变量enable

错误:(34,35)错误:找不到符号变量gps_disabled_title

错误:(32,36)错误:找不到符号变量gps_disabled

你没有正确地访问字符串资源。 应该这样写:

.setTitle(R.string.gps_disabled_title);

你应该做:

.setTitle(getResources().getString(R.string.gps_disabled_title));

错误:(31,73)错误:找不到符号方法getActivity()

由于您已经在一个活动中,因此应按以下方式检索其上下文:

final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接