不兼容的类型:<匿名TextWatcher>不能转换到上下文
问题描述:
我试图做一个“动态搜索” ..不兼容的类型:<匿名TextWatcher>不能转换到上下文
这个片段是我实现的请求......在这里,我得到一个错误(错误:(95,40)error:incompatible types:can not be converted to Context)and 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():是被称为片段中它的调用上下文引用
的方法我希望帮助 问候
啊奥科克的感谢! ! –