Resolve Activity上下文需要FLAG_ACTIVITY_NEW_TASK标志。这真的是你想要的吗?

时间:2018-03-17 13:17:37

标签: java android android-layout listview android-studio

我正在关注本教程 https://www.simplifiedcoding.net/android-volley-tutorial-fetch-json/#comment-14239

从URL获取JSON数据,当我用文本字符串填充TextView但是当我用url填充ListView并且如果我点击链接然后应用程序崩溃时出现错误,< / p>

Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

注意:我在布局中使用自动链接

android:autoLink="web"

我在Stackover流程上遵循了几个相同的线程,但无法解决它......

这是我的班级:

package com.nepalpolice.cdp;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Sagar on 2017/09/23.
 */


public class notices extends Fragment {

    ProgressBar progressBar;

    //the URL having the json data
    private static final String JSON_URL = "https://simplifiedcoding.net/demos/view-flipper/heroes.php";

    //listview object
    ListView listView;

    //the hero list where we will store all the hero objects after parsing json
    List<Hero> heroList;


    public static notices newInstance() {
        notices fragment = new notices();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_notices, container, false);


        listView = (ListView) view.findViewById(R.id.listView);
        progressBar = (ProgressBar) view.findViewById(R.id.progressBar);

        heroList = new ArrayList<>();

        //this method will fetch and parse the data
        loadHeroList();

        Intent viewIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        viewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(viewIntent);

        return view;


    }

    private void loadHeroList() {


        //making the progressbar visible
        progressBar.setVisibility(View.VISIBLE);
        //creating a string request to send request to the url
        StringRequest stringRequest = new StringRequest(Request.Method.GET, JSON_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //hiding the progressbar after completion
                        progressBar.setVisibility(View.INVISIBLE);


                        try {
                            //getting the whole json object from the response
                            JSONObject obj = new JSONObject(response);

                            //we have the array named hero inside the object
                            //so here we are getting that json array
                            JSONArray heroArray = obj.getJSONArray("heroes");

                            //now looping through all the elements of the json array
                            for (int i = 0; i < heroArray.length(); i++) {
                                //getting the json object of the particular index inside the array
                                JSONObject heroObject = heroArray.getJSONObject(i);

                                //creating a hero object and giving them the values from json object
                                Hero hero = new Hero(heroObject.getString("name"), heroObject.getString("imageurl"));

                                //adding the hero to herolist
                                heroList.add(hero);
                            }

                            //creating custom adapter object
                            ListViewAdapter adapter = new ListViewAdapter(heroList, getActivity().getApplicationContext());

                            //adding the adapter to listview
                            listView.setAdapter(adapter);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //displaying the error in toast if occurrs
                        Toast.makeText(getActivity().getApplicationContext(), "Check Internet Connection", Toast.LENGTH_SHORT).show();

                    }
                });

        //creating a request queue
        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());

        //adding the string request to request queue
        requestQueue.add(stringRequest);
    }

}

我该如何解决?

谢谢。

1 个答案:

答案 0 :(得分:0)

以下错误消息:

Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

因为您想使用片段开始活动:

Intent viewIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(viewIntent);

您需要使用片段的主机Activity来启动另一个活动。所以,你需要getActivity()这样:

Intent viewIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
getActivity().startActivity(viewIntent);