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

时间:2018-09-05 12:11:30

标签: java android

我正在尝试要求用户启用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: cannot find symbol method getActivity() 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 Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

帮帮我

4 个答案:

答案 0 :(得分:0)

  

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

getActivity() 中使用了 fragments 来使 context 不在活动中

要获取活动中的上下文,请使用

  • this
  • YourActvity.this
  • getApplicationContext()

使用此

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

代替这个

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

错误:找不到符号变量启用错误:(34、35)错误:找不到符号变量gps_disabled_title错误:(32、36)错误:找不到符号变量gps_disabled

您需要在 string.xml 选中String resources

中创建以下资源
  • gps_disabled
  • cancel
  • enable
  • gps_disabled_title
  • gps_disabled

答案 1 :(得分:0)

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

答案 2 :(得分:0)

让我们解决您的错误:

  

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

     

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

     

错误:(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());

答案 3 :(得分:0)

在第一个错误中

  

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

为此,您需要使用此:

  

最后的AlertDialog.Builder builder =新的AlertDialog.Builder(MainActivity.this);

代替此:

  

最后的AlertDialog.Builder构建器=新的AlertDialog.Builder(this.getActivity());

然后是其他错误

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

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

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

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

这两个都表示取消和启用两个变量都无法从字符串中找到,因此转到字符串,然后如下定义

在字符串文件中:

<String name="enable">somthig</String>
<String name="cancel">somthig</String>
<String name="gps_disabled_title">somthig</String>
<String name="gps_disabled">somthig</String>
相关问题