我的程序有关上下文的错误,,, 我将结合导航绘制和列表视图,但我的代码,
的任何错误这是我的片段家庭活动
package com.marimuncak.mari_muncak;
import android.Manifest;
import android.app.Fragment;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatCallback;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.marimuncak.mari_muncak.adapter.CustomListAdapter;
import com.marimuncak.mari_muncak.app.AppController;
import com.marimuncak.mari_muncak.module.Jarak;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* Created by azoel on 6/8/2017.
*/
public class Home extends Fragment implements LocationListener,
SwipeRefreshLayout.OnRefreshListener{
private Context baseContext;
public Home(){}
RelativeLayout view;
SwipeRefreshLayout swipe;
ListView list;
CustomListAdapter adapter;
List<Jarak> itemList = new ArrayList<>();
Double latitude, longitude;
Criteria criteria;
Location location;
LocationManager locationManager;
String provider;
private static final String url = "https://marimuncak.000webhostapp.com/haversine.php?lat=";
private static final String TAG = Home.class.getSimpleName();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = (RelativeLayout) inflater.inflate(R.layout.home, container, false);
list = (ListView) view.findViewById(R.id.list);
swipe = (SwipeRefreshLayout) view.findViewById(R.id.swipe);
CustomListAdapter adapter = new CustomListAdapter (getActivity(), itemList);
list.setAdapter(adapter);
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
getActivity().setTitle("Home");
return view;
}
@Override
public void onRefresh(){
lokasi();
}
// fungsi ngecek lokasi GPS device pengguna
private void lokasi(){
location = locationManager.getLastKnownLocation(provider);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
// permintaan update lokasi device dalam waktu 10 detik
locationManager.requestLocationUpdates(provider, 10000, 1, this);
if(location!=null){
onLocationChanged(location);
callListVolley(latitude, longitude);
}else{
Toast.makeText(getBaseContext(), "Lokasi device pengguna tidak ditemukan.\nMohon hidupkan GPS.",
Toast.LENGTH_LONG).show();
/* latitude longitude Alun-alun Demak sebagai default jika tidak ditemukan lokasi dari device pengguna */
callListVolley(-6.894796, 110.638413);
}
}
// untuk menampilkan lokasi wisata terdekat dari device pengguna
private void callListVolley(double lat, double lng) {
itemList.clear();
adapter.notifyDataSetChanged();
swipe.setRefreshing(true);
JsonArrayRequest jArr = new JsonArrayRequest(url + lat +"&lng="+ lng,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Jarak j = new Jarak();
j.setNama(obj.getString("nama"));
j.setGambar(obj.getString("gambar"));
double jarak = Double.parseDouble(obj.getString("jarak"));
j.setJarak(""+round(jarak, 2));
itemList.add(j);
} catch (JSONException e) {
e.printStackTrace();
}
}
// memberitahu adapter jika ada perubahan data
adapter.notifyDataSetChanged();
swipe.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getBaseContext(), error.getMessage(), Toast.LENGTH_LONG).show();
swipe.setRefreshing(false);
}
});
// menambah permintaan ke queue
AppController.getInstance().addToRequestQueue(jArr);
}
// untuk menyederhanakan angka dibelakan koma jarak
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
long factor = (long) Math.pow(10, places);
value = value * factor;
long tmp = Math.round(value);
return (double) tmp / factor;
}
// untuk menentukan lokasi gps dari device pengguna
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
// untuk melihat latitude longitude posisi device pengguna pada logcat ditemukan atau tidak
Log.d(TAG, " "+ latitude +", "+longitude);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
public Context getBaseContext() {
return baseContext;
}
}
&#13;
这是一个logchat
:app:compileDebugJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).
D:\Users\azoel\AndroidStudioProjects\Mari_Muncak\app\src\main\java\com\marimuncak\mari_muncak\Home.java
Error:(94, 48) error: incompatible types: Home cannot be converted to Context
Error:(95, 89) error: incompatible types: Home cannot be converted to Context
D:\Users\azoel\AndroidStudioProjects\Mari_Muncak\app\src\main\java\com\marimuncak\mari_muncak\MainActivity.java
Error:(131, 5) error: method does not override or implement a method from a supertype
Note: D:\Users\azoel\AndroidStudioProjects\Mari_Muncak\app\src\main\java\com\marimuncak\mari_muncak\MainActivity.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
:app:compileDebugJavaWithJavac FAILED
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 1 mins 23.688 secs
Information:4 errors
Information:0 warnings
Information:See complete output in console
&#13;
我该怎么办? 非常感谢你帮助我,
答案 0 :(得分:0)
首先,请改用android.support.v4.app.Fragment
。你使用android.app.Fragment
第二行
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
更改为
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
第三,在第131行确保void onResponse(JSONArray response)
接口存在Response.Listener
。我从不使用凌空,所以我不知道正确的方法。