簡體   English   中英

java.lang.RuntimeException:無法實例化活動 ComponentInfo - 不是搜索活動

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

我的代碼有問題。 無法將 class 添加到 AndroidManifest.xml。 如何修改代碼以使其作為獨立的 class 工作並正常工作? 我需要從 URL 中找到 JSON 的有效解決方案。

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

統計:

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);
}

我在這里畫了部分代碼。 我不是程序員,所以我很難找到解決方案。 非常感謝你幫助我::)

AndroidManifest.xml只添加了活動,你得到了一個擴展的片段 class。

所以你應該創建活動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM