我正在尝试进行“动态搜索”。
这个片段是我实现请求的地方...这里我得到一个错误(错误:(95,40)错误:不兼容的类型:无法转换为上下文)和MySingleton.getInstance(this).addToRequestQueue(jsObjRequest);这是我得到错误的那一行
分段:
public class CercaFragment extends Fragment {
TextView textView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.cerca, parent, false);
textView = (TextView) v.findViewById(R.id.textView);
setHasOptionsMenu(true);
EditText search = (EditText)v.findViewById(R.id.editText);
search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
textView.setText(s);
String url = "http://api.geonames.org/searchJSON?q=londoz&maxRows=10&fuzzy=0.8&username=demo";
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
GeonamesClass example = new Gson().fromJson(response.toString(),GeonamesClass.class);
textView.setText("Name"+example.getUsers().get(0).getName());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(jsObjRequest);
}
@Override
public void afterTextChanged(Editable s) {
}
});
return v;
}
}
MySingleton:
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private MySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized MySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new MySingleton(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
最佳答案
当您在事件中执行此操作时,您不是在引用应用程序的上下文,而是在生成事件的对象,要在您的单例中获取上下文,您必须在MySingleton.Getinstance(this)...之外事件,或者如果它在您要调用的地方,则执行MySingleton.Getinstance。(getActivity())之类的事情。
getActivity():是在片段内调用的方法,以将其调用到上下文引用
希望对您有所帮助
一声问候
关于java - 不兼容的类型:<anonymous TextWatcher>无法转换为Context,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43502794/