【问题标题】:Cannot resolve methods requestLocationUpdates using a Pubnub tutorial无法使用 Pubnub 教程解析方法 requestLocationUpdates
【发布时间】:2016-07-01 07:21:22
【问题描述】:

好的,这个问题已经被问到了,我已经尝试了所有提供的解决方案,但似乎都不起作用,

我正在使用一个在 Android 应用上实时显示我的位置的 Pubnub 教程,我遇到了一些我无法解决的错误,主要是 “无法解析方法 requestLocationUpdates”

我在之前的帖子中看到要添加,import android.location.LocationListener;到导入,但它没有改变任何东西。

如果有人对此有任何解决方案,那就太好了,如果您知道的话,还有我的其他错误。

主要活动:

package nixerpubcom.nixerpub;

import android.location.Location;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.pubnub.api.PubnubError;

import android.location.LocationListener;

import org.json.JSONException;
import org.json.JSONObject;

import javax.security.auth.callback.Callback;

public class MainActivity extends AppCompatActivity  implements GoogleApiClient.ConnectionCallbacks {

//Create Google API Client
private GoogleApiClient mGoogleApiClient;
//Create pubnub variable
private Pubnub mPubnub;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //Start The Google Client
    this.buildGoogleApiClient();
    mGoogleApiClient.connect();

    //Retrieve pubnub keys
    mPubnub = new Pubnub ("pub-c-fe4ed754-2b9d-4563-abb6-63c3f048f9ad","sub-c-82de7130-eacb-11e5-8346-0619f8945a4f");

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

//Google Api build Method
private synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this).addApi(LocationServices.API)
            .build();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

// Implement the Connection Callback
@Override
public void onConnected(Bundle connectionHint) {
    LocationRequest mLocationRequest = createLocationRequest();
    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int cause) {

    Log.d(TAG, "Connection to Google API Suspended ");

}

private LocationRequest createLocationRequest() {
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    return mLocationRequest;
}

@Override
public void onLocationChanged(Location location) {
    broadcastLocation(location);
}


private void broadcastLocation(Location location) {
    JSONObject message = new JSONObject();
    try {
        message.put("lat", location.getLatitude());
        message.put("lng", location.getLongitude());
        message.put("alt", location.getAltitude());
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
    }
    mPubnub.publish("A Channel Name", message, publishCallback);
}

KeyEvent.Callback publishCallback = new Callback() {
    @Override
    public void successCallback(String channel, Object response) {
        Log.d("PUBNUB", response.toString());
    }

    @Override
    public void errorCallback(String channel, PubnubError error) {
        Log.e("PUBNUB", error.toString());
    }
};

}

This is the error I get for the Cannot resolve methods requestLocationUpdates, my other errors are also in there which is a TAG error and a pubnub error

我是新手,所以如果问题的格式不正确,我深表歉意。

【问题讨论】:

    标签: android google-maps android-studio google-api pubnub


    【解决方案1】:
    1. 在 requestLocationUpdates 方法中:

      LocationServices.FusedLocationApi.requestLocationUpdates( mGoogleApiClient, mLocationRequest, this);
      

      期望调用(MainActivity)的类实现com.google.android.gms.location.LocationListener,所以你需要实现它,或者用匿名类改变它:

      LocationServices.FusedLocationApi.requestLocationUpdates(
                  mGoogleApiClient, mLocationRequest, new LocationListener() {
                      @Override
                      public void onLocationChanged(Location location) {
                          // Here you have the updated location
                      }
                  });
      }
      
    2. 这个类中没有定义字符串 TAG,所以在你的类上创建一个这样的字段:

      private static final String TAG = "MainActivity";
      

      提示:在Android Studio中输入“logt”+回车即可生成这一行。

    3. 你想要的回调

      KeyEvent.Callback publishCallback = new Callback() {...}
      

      不是来自KeyEvent,而是来自com.pubnub.api.Callback

      com.pubnub.api.Callback publishCallback = new Callback() {...}
      

    请确保您的导入是正确的,并且您在清单中要求提供位置:

    android.permission.ACCESS_COARSE_LOCATION
    

    android.permission.ACCESS_COARSE_LOCATION
    

    【讨论】:

      猜你喜欢
      • 2015-05-11
      • 1970-01-01
      • 1970-01-01
      • 2016-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      相关资源
      最近更新 更多