繁体   English   中英

在Android Studio中构建应用程序时发生Gradle错误

[英]Gradle error while building an application in Android Studio

我正在尝试创建一个给我当前位置的android应用程序。 由于在Button和Text View对象上创建,因此出现错误。

依赖项: classpath 'com.android.tools.build:gradle:3.1.0' : classpath 'com.android.tools.build:gradle:3.1.0'

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.example.suppalapat21.my_app.MainActivity">

<TextView
    android:id="@+id/textView"
    android:layout_width="238dp"
    android:layout_height="0dp"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="212dp"
    android:text="Hello World!"
    app:layout_constraintBottom_toTopOf="@+id/button"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.461" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="180dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

</android.support.constraint.ConstraintLayout>

MainActivity.java

package com.example.suppalapat21.my_app;

import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity implements LocationListener {

   Button getLocationBtn;
   TextView locationText;

   LocationManager locationManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_main);

    getLocationBtn = (Button)findViewById(R.id.getLocationBtn);
    locationText = (TextView)findViewById(R.id.locationText);


    if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, 101);

    }


    getLocationBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getLocation();
        }
    });
}

void getLocation() {
    try {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 5, this);
    }
    catch(SecurityException e) {
        e.printStackTrace();
    }
}

@Override
public void onLocationChanged(Location location) {
    locationText.setText("Latitude: " + location.getLatitude() + "\n Longitude: " + location.getLongitude());

    try {
        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        locationText.setText(locationText.getText() + "\n"+addresses.get(0).getAddressLine(0)+", "+
                addresses.get(0).getAddressLine(1)+", "+addresses.get(0).getAddressLine(2));
    }catch(Exception e)
    {

    }

}

@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(MainActivity.this, "Please Enable GPS and Internet", Toast.LENGTH_SHORT).show();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}
}

这是我在gradle构建之后收到的错误消息。 连接到设备并运行后,构建失败。

 Error:19:37:15.259 [ERROR] [system.err] 
 19:37:15.260 [ERROR] [system.err]         getLocationBtn = 
 (Button)findViewById(R.id.getLocationBtn);
 19:37:15.264 [ERROR] [system.err]                                                   
 19:37:15.264 [ERROR] [system.err]   symbol:   variable getLocationBtn
 19:37:15.264 [ERROR] [system.err]   location: class id
 19:37:15.301 [ERROR] [system.err] 
 19:37:15.302 [ERROR] [system.err]   locationText = 
 (TextView)findViewById(R.id.locationText);
 19:37:15.302 [ERROR] [system.err]                                                   
 19:37:15.302 [ERROR] [system.err]   symbol:   variable locationText
 19:37:15.302 [ERROR] [system.err]   location: class id
 19:37:15.421 [ERROR] [system.err] 2 errors

错误的原因是R.id.locationText和R.id.getLocationBtn

在下面的代码上将其更改为xml文件中的textview-ID设置为textView并将button-ID设置为button

getLocationBtn = (Button)findViewById(R.id.button);
locationText = (TextView)findViewById(R.id.textView);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM