好的,这个问题已经被问到,我已经尝试了所提供的所有解决方案,但它们似乎都没有用,
我正在使用Pubnub教程在Android应用上实时显示我的位置,我遇到了一些我无法解决的错误主要是 “无法解析方法requestLocationUpdates”
我在之前的帖子中看到要添加,导入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());
}
};
}
我是新手,所以如果问题没有正确格式化,我会道歉。
答案 0 :(得分: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
}
});
}
此类中未定义字符串TAG,因此在您的类上创建一个这样的字段:
private static final String TAG = "MainActivity";
提示:输入“logt”+在Android Studio中输入以生成此行。
您想要的回调
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