[英]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.无法将 class 添加到 AndroidManifest.xml。 How do I modify the code to make it work as a standalone class and work?如何修改代码以使其作为独立的 class 工作并正常工作? I need to find a working solution for JSON scrape from URL.我需要从 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
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. AndroidManifest.xml
只添加了活动,你得到了一个扩展的片段 class。
So you should create activity.所以你应该创建活动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.