gpt4 book ai didi

java - 不兼容的类型 : cannot be converted to Context

转载 作者:行者123 更新时间:2023-12-02 02:53:12 24 4
gpt4 key购买 nike

我正在尝试进行“动态搜索”..

这个 fragment 是我实现请求的地方...这里我收到错误(错误:(95, 40)错误:不兼容的类型:无法转换为上下文)和 MySingleton.getInstance(this).addToRequestQueue(jsObjRequest) ;这是我收到错误的行

fragment :

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():是在 fragment 中调用的方法,以调用其上下文引用

希望有帮助一句问候

关于java - 不兼容的类型 : <anonymous TextWatcher> cannot be converted to Context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43502794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com