如何使用地点选择器解决此问题?

时间:2020-06-11 00:13:06

标签: java android firebase gmsplacepicker

此错误显示在logcat中- java.lang.NoSuchMethodError:类Lcom / google / android / gms / common / GoogleApiAvailability中没有静态方法zzce(Landroid / content / Context;)V或其上级类(“ com.google.android.gms.common.GoogleApiAvailability”的声明出现在/data/app/com.example.gpsnotify-2/base.apk中)

NewtaskActivity.java

package com.example.gpsnotify;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlacePicker;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class NewtaskActivity extends AppCompatActivity {

    //For variables of adding task view
    private EditText edtitle,eddescription;
    Task task;
    DatabaseReference drf;
    FirebaseAuth fuserAuth = FirebaseAuth.getInstance();
    FirebaseUser fuser = fuserAuth.getCurrentUser();
    String uid= fuser.getUid();
    //For variables of adding task view

    //For placepicker
    ImageView map;
    int PLACE_PICKER_REQUEST = 1;
    String longi = null;
    String latt = null;
    //For placepicker

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

        //For toolbar
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close);
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#000000")));
        //For toolbar

        //For binding views of newtask layout
        edtitle = (EditText) findViewById(R.id.et_Title);
        eddescription = (EditText) findViewById(R.id.et_Description);
        //For binding views of newtask layout

        //For placepicker
        map = (ImageView) findViewById(R.id.map_pic);
        map.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
                try {
                    startActivityForResult(builder.build(NewtaskActivity.this),PLACE_PICKER_REQUEST);
                } catch (GooglePlayServicesRepairableException e) {
                    e.printStackTrace();
                } catch (GooglePlayServicesNotAvailableException e) {
                    e.printStackTrace();
                }
            }
        });

        //For placepicker

    }
    //For placepicker

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PLACE_PICKER_REQUEST) {
            if (resultCode == RESULT_OK) {
                Place place = PlacePicker.getPlace(data, this);
                longi = String.valueOf(place.getLatLng().longitude);
                latt = String.valueOf(place.getLatLng().latitude);
            }
        }
    }

    //For placepicker

    //For save icon
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater mif = getMenuInflater();
        mif.inflate(R.menu.new_task_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.save_task:
                savetask();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    private void savetask(){
        task = new Task();
        drf = FirebaseDatabase.getInstance().getReference("Users").child(uid).child("Tasks");
        String ttl = edtitle.getText().toString();
        String des = eddescription.getText().toString();
        if(ttl.trim().isEmpty())
        {
            edtitle.setError("Enter a title.");
            edtitle.requestFocus();
            return;
        }
        if(des.trim().isEmpty())
        {
            eddescription.setError("Enter a description.");
            eddescription.requestFocus();
            return;
        }
        task.setLatitude(latt);
        task.setLongitude(longi);
        task.setTitle(ttl);
        task.setDescription(des);
        drf.push().setValue(task);
        Toast.makeText(NewtaskActivity.this,"Saved",Toast.LENGTH_SHORT);
        finish();
    }
    //For save icon
}

AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gpsnotify">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="API key" />

        <activity android:name=".NewtaskActivity"
            android:parentActivityName=".MainActivity"
            android:label="Add new task"
            />
        <activity android:name=".LoginActivity" />
        <activity android:name=".SignupActivity" />
        <activity android:name=".SplashscreenActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity" />
    </application>

</manifest>
build.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.gpsnotify"
        minSdkVersion 18
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        multiDexEnabled true

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:22.0.0'
    //noinspection GradleCompatible
    implementation 'com.google.android.gms:play-services:12.0.0'
    //implementation 'com.google.android.gms:play-services-location:17.0.0'
    implementation 'com.google.firebase:firebase-core:12.0.0'
    implementation 'com.google.android.gms:play-services-gcm:12.0.0'


    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation "androidx.cardview:cardview:1.0.0"
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation "androidx.recyclerview:recyclerview-selection:1.1.0-rc01"
    implementation "androidx.recyclerview:recyclerview:1.1.0"

    implementation 'com.google.firebase:firebase-auth:19.3.1'
    implementation 'com.google.firebase:firebase-storage:12.0.0'
    implementation 'com.google.firebase:firebase-database:12.0.0'
    implementation 'com.firebaseui:firebase-ui-database:6.2.1'
    implementation 'com.firebaseui:firebase-ui-firestore:6.2.1'
    implementation 'android.arch.core:runtime:1.1.1'

    implementation 'com.android.support:multidex:1.0.3'

    //implementation 'com.google.firebase:firebase-firestore:21.4.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

1 个答案:

答案 0 :(得分:0)

map.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick(View v) {

                openPlacePicker ();

            }
        } );


        private void openPlacePicker () {
            PlacePicker.IntentBuilder builder = new 
    PlacePicker.IntentBuilder ();
            try {
                startActivityForResult ( builder.build ( this ), 
     PLACE_PICKER_REQUEST );

                //Enable Wifi
                wifiManager.setWifiEnabled ( true );


            } catch (GooglePlayServicesRepairableException e) {
                // Log.d("Exception",e.getMessage());

                e.printStackTrace ();
            } catch (GooglePlayServicesNotAvailableException e) {
                //Log.d("Exception",e.getMessage());

                e.printStackTrace ();
            }
        }

        @Override
        protected void onActivityResult ( int requestCode, int 
      resultCode, Intent data){
            super.onActivityResult ( requestCode, resultCode, data );

            if (resultCode == RESULT_OK) {
                switch (requestCode) {
                    case PLACE_PICKER_REQUEST:
                        Place place = PlacePicker.getPlace ( 
     MainActivity.this, data );

                        double latitude = place.getLatLng ().latitude;
                        double longitude = place.getLatLng ().longitude;
                        CharSequence address = place.getAddress ();
                        String PlaceLatLng = String.valueOf ( latitude ) 
     + " , " + String.valueOf ( longitude );

                        tv_MyLocation.setText ( PlaceLatLng );

                }
            }