简体   繁体   中英

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo - not search activity

I have a problem with my code. can't add class to AndroidManifest.xml. How do I modify the code to make it work as a standalone class and work? I need to find a working solution for JSON scrape from URL.

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{cz.mikolashodan.proticovidu/cz.mikolashodan.proticovidu.Statistic}: java.lang.ClassCastException: cz.mikolashodan.proticovidu.Statistic cannot be cast to android.app.Activity

Statistic:

public class Statistic extends Fragment {

private TextView novych_pripadu_cislo, textView14s2;
private ProgressBar progressBar;

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {

    View root = inflater.inflate(R.layout.activity_statistic, container, false);

    // call view
    novych_pripadu_cislo = root.findViewById(R.id.novych_pripadu_cislo);
    textView14s2 = root.findViewById(R.id.textView14s2);
    progressBar = root.findViewById(R.id.progress_circular_home);

    // Action Bar title
    getActivity().setTitle("Overview");

    // call Volley
    getData();

    return root;
}

private String getDate(long milliSecond){
    // Mon, 23 Mar 2020 02:01:04 PM
    SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss aaa");

    Calendar calendar= Calendar.getInstance();
    calendar.setTimeInMillis(milliSecond);
    return formatter.format(calendar.getTime());
}

private void getData() {
    RequestQueue queue = Volley.newRequestQueue(getActivity());

    String url = "https://corona.lmao.ninja/all";

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            progressBar.setVisibility(View.GONE);

            try {
                JSONObject jsonObject = new JSONObject(response.toString());

                novych_pripadu_cislo.setText(jsonObject.getString("cases"));
                textView14s2.setText("Last Updated"+"\n"+getDate(jsonObject.getLong("updated")));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            progressBar.setVisibility(View.GONE);
            Log.d("Error Response", error.toString());
        }
    });

    queue.add(stringRequest);
}

I drew part of the code here . I'm not a programmer so it's hard for me to find a solution. Thank you so much for help me: :)

To AndroidManifest.xml are added only Activities, you got there a Fragment extended class.

So you should create activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM