Android 虚拟方法&x27;getApplicationContext()和#x27;无法在应用程序中显示上下文

Android 虚拟方法&x27;getApplicationContext()和#x27;无法在应用程序中显示上下文,android,Android,显示上下文时,会发生故障。有什么问题吗? 找不到您的请求的结果 如果找到结果,则在没有上下文菜单时显示,未找到任何结果,但给出错误和应用程序失败 public void run(final View view) throws IOException{ Log.d(TAG, "run: up and running..."); if (frag_id==0 || frag_id==1) { //if home fragment & search fragment

显示上下文时,会发生故障。有什么问题吗? 找不到您的请求的结果

如果找到结果,则在没有上下文菜单时显示,未找到任何结果,但给出错误和应用程序失败

 public void run(final View view) throws IOException{
    Log.d(TAG, "run: up and running...");

    if (frag_id==0 || frag_id==1) {  //if home fragment & search fragment
        Log.d(TAG, "getIntentData: frag id not 2");
        if (search_term == null) {
            urlImage = "https://api.pexels.com/v1/curated";
        } else {
            int randPage;
            do {
                randPage = new Random().nextInt(15);
            }while(randPage==0);
            urlImage = "https://api.pexels.com/v1/search?query=" + search_term.toLowerCase() + "&per_page=15&page=" + randPage;
        }
    }

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .header("Authorization", StaticUtils.API_KEY)
            .url(urlImage)
            .build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(@NonNull Call call, @NonNull IOException e) {
            call.cancel();
        }

        @Override
        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {

            final String myResponse = response.body() != null ? response.body().string() : null;

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.d(TAG, "run: integrated successfully: ");
                    if (!StaticUtils.imagesListTemp.isEmpty()){
                        StaticUtils.imagesListTemp.clear();
                    }

                    try {
                        JSONObject json = new JSONObject(myResponse);

                        JSONArray jsonArray = json.getJSONArray("photos");
                        if (jsonArray.length()>0){
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);

                                int imageId = jsonObject.getInt("id");
                                String srcName = jsonObject.getString("photographer");
                                String srcUrl = jsonObject.getString("photographer_url");
                                String dimen = jsonObject.getString("width")+"x"+jsonObject.getString("height");

                                JSONObject srcObject = jsonObject.getJSONObject("src");  //getting the image sources of different dimens
                                String imgSrcOrg = srcObject.getString("original");
                                String imgSrcLarg = srcObject.getString("large");
                                String imgSrcMid = srcObject.getString("medium");
                                String imgSrcSml = srcObject.getString("small");
                                String imgSrcPort = srcObject.getString("portrait");
                                String imgSrcLand = srcObject.getString("landscape");
                                String imgSrcTiny = srcObject.getString("tiny");
                                ImageDifferentSize imageArray = new ImageDifferentSize(imgSrcOrg,imgSrcLarg,imgSrcMid,imgSrcSml,imgSrcPort,imgSrcLand,imgSrcTiny);

                                StaticUtils.imagesListTemp.add(new ImagesItem(imageId, srcName, dimen, srcUrl,imageArray));
                                ImagesFragment.adapter.notifyDataSetChanged();
                            }
                        }else{
                            //replace with snackBar
                            Toast.makeText(getApplicationContext(), "This is my Toast message!",
                                    Toast.LENGTH_SHORT).show();
                        }
                        Log.d(TAG, "run: List size: "+StaticUtils.imagesListTemp.size());
                        boolean isAddSucceed = StaticUtils.imagesList.addAll(StaticUtils.imagesListTemp);
                        Log.d(TAG, "onCreate: Copy operation succeed:"+isAddSucceed+", Final arrayList size: "+StaticUtils.imagesList.size());
                    } catch (JSONException e) {
                        Log.d(TAG, "onResponse: Json Parsing data error.");
                        e.printStackTrace();
                    }
                }
public void run(最终视图)引发IOException{
Log.d(标记“运行:启动并运行…”);
if(frag_id==0 | | frag_id==1){//if主片段和搜索片段
Log.d(标签“getIntentData:frag id非2”);
如果(搜索项==null){
urlImage=”https://api.pexels.com/v1/curated";
}否则{
int randPage;
做{
randPage=new Random().nextInt(15);
}而(randPage==0);
urlImage=”https://api.pexels.com/v1/search?query=“+search_term.toLowerCase()+”&per_page=15&page=“+randPage;
}
}
OkHttpClient=新的OkHttpClient();
Request Request=newrequest.Builder()
.header(“授权”,StaticUtils.API_键)
.url(urlImage)
.build();
client.newCall(request).enqueue(new Callback()){
@凌驾
public void onFailure(@NonNull Call Call,@NonNull ioe异常){
call.cancel();
}
@凌驾
public void onResponse(@NonNull Call Call,@NonNull Response Response)引发IOException{
最后一个字符串myResponse=response.body()!=null?response.body().String():null;
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Log.d(标记“run:integratedsuccessfully:”);
如果(!StaticUtils.imagesListTemp.isEmpty()){
StaticUtils.imagesListTemp.clear();
}
试一试{
JSONObject json=新的JSONObject(myResponse);
JSONArray JSONArray=json.getJSONArray(“照片”);
if(jsonArray.length()>0){
for(int i=0;i
显示上下文时发生故障。可能是什么问题? 找不到您的请求的结果


如果找到结果,则在没有上下文菜单、未找到结果时显示,但它会给出错误和应用程序失败

java.lang.NullPointerException:尝试调用虚拟方法“android.content.context android.content.context.getApplicationContext()'在nulljava.lang.NullPointerException上:尝试在nulljava.lang.NullPointerException上调用虚拟方法'android.content.Context android.content.Context.getApplicationContext()
 public void run(final View view) throws IOException{
    Log.d(TAG, "run: up and running...");

    if (frag_id==0 || frag_id==1) {  //if home fragment & search fragment
        Log.d(TAG, "getIntentData: frag id not 2");
        if (search_term == null) {
            urlImage = "https://api.pexels.com/v1/curated";
        } else {
            int randPage;
            do {
                randPage = new Random().nextInt(15);
            }while(randPage==0);
            urlImage = "https://api.pexels.com/v1/search?query=" + search_term.toLowerCase() + "&per_page=15&page=" + randPage;
        }
    }

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .header("Authorization", StaticUtils.API_KEY)
            .url(urlImage)
            .build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(@NonNull Call call, @NonNull IOException e) {
            call.cancel();
        }

        @Override
        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {

            final String myResponse = response.body() != null ? response.body().string() : null;

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.d(TAG, "run: integrated successfully: ");
                    if (!StaticUtils.imagesListTemp.isEmpty()){
                        StaticUtils.imagesListTemp.clear();
                    }

                    try {
                        JSONObject json = new JSONObject(myResponse);

                        JSONArray jsonArray = json.getJSONArray("photos");
                        if (jsonArray.length()>0){
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);

                                int imageId = jsonObject.getInt("id");
                                String srcName = jsonObject.getString("photographer");
                                String srcUrl = jsonObject.getString("photographer_url");
                                String dimen = jsonObject.getString("width")+"x"+jsonObject.getString("height");

                                JSONObject srcObject = jsonObject.getJSONObject("src");  //getting the image sources of different dimens
                                String imgSrcOrg = srcObject.getString("original");
                                String imgSrcLarg = srcObject.getString("large");
                                String imgSrcMid = srcObject.getString("medium");
                                String imgSrcSml = srcObject.getString("small");
                                String imgSrcPort = srcObject.getString("portrait");
                                String imgSrcLand = srcObject.getString("landscape");
                                String imgSrcTiny = srcObject.getString("tiny");
                                ImageDifferentSize imageArray = new ImageDifferentSize(imgSrcOrg,imgSrcLarg,imgSrcMid,imgSrcSml,imgSrcPort,imgSrcLand,imgSrcTiny);

                                StaticUtils.imagesListTemp.add(new ImagesItem(imageId, srcName, dimen, srcUrl,imageArray));
                                ImagesFragment.adapter.notifyDataSetChanged();
                            }
                        }else{
                            //replace with snackBar
                            Toast.makeText(getApplicationContext(), "This is my Toast message!",
                                    Toast.LENGTH_SHORT).show();
                        }
                        Log.d(TAG, "run: List size: "+StaticUtils.imagesListTemp.size());
                        boolean isAddSucceed = StaticUtils.imagesList.addAll(StaticUtils.imagesListTemp);
                        Log.d(TAG, "onCreate: Copy operation succeed:"+isAddSucceed+", Final arrayList size: "+StaticUtils.imagesList.size());
                    } catch (JSONException e) {
                        Log.d(TAG, "onResponse: Json Parsing data error.");
                        e.printStackTrace();
                    }
                }