如何修复java.lang.ClassCastException Android

时间:2017-06-12 08:12:27

标签: android json listview android-listfragment

我一直在寻找这个案例,我发现它只是在C#中。哦,我尝试使用ListFragment android,json和mysql将我的数据显示到listview中,但是当我运行它时我得到了一个错误Class Cast,我希望任何人都可以帮助我。 在这里我的错误:

06-12 17:04:04.685 20975-20975/com.spp.udi.sppmap E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.spp.udi.sppmap, PID: 20975
                                                                    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.spp.udi.sppmap/com.spp.udi.sppmap.Potensi}: java.lang.ClassCastException: com.spp.udi.sppmap.Potensi cannot be cast to android.app.Activity
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2345)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
                                                                        at android.app.ActivityThread.access$900(ActivityThread.java:153)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5451)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                     Caused by: java.lang.ClassCastException: com.spp.udi.sppmap.Potensi cannot be cast to android.app.Activity
                                                                        at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2335)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
                                                                        at android.app.ActivityThread.access$900(ActivityThread.java:153) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:148) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5451) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我的Potensi.java:

package com.spp.udi.sppmap;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.app.ListFragment;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class Potensi extends ListFragment {
    private static final String ID_KECAMATAN = "id_kecamatan";
    private static final String NAMA_KECAMATAN = "nama_kecamatan";
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String,String>>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        JSONObject json = JSONFunction.getJSONFromUrl("http://192.168.43.203/project/s9/udi/android/potensi.php");
        try {
            JSONArray laporan = json.getJSONArray("result");
            for(int i=0; i<laporan.length(); i++){
                JSONObject jsonobj = laporan.getJSONObject(i);
                String id_kecamatan = jsonobj.getString(ID_KECAMATAN);
                String nama_kecamatan = jsonobj.getString(NAMA_KECAMATAN);
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("id_kecamatan", id_kecamatan);
                map.put("nama_kecamatan", nama_kecamatan);
                mylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("log_tag","error parsing data "+e.toString());
        }
        this.adapter_listview();
    }

    public void adapter_listview() {
        ListAdapter adapter = new SimpleAdapter(getActivity(), mylist, R.layout.potensi,
                new String[] {ID_KECAMATAN, NAMA_KECAMATAN}, new int[] {R.id.id_kecamatan, R.id.nama_kecamatan});

        setListAdapter(adapter);
        ListView lv = getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                String id_kecamatan = ((TextView) view.findViewById(R.id.id_kecamatan)).getText().toString();
                Intent in = new Intent(getActivity(), DetailPotensi.class);
                in.putExtra(ID_KECAMATAN, id_kecamatan);
                startActivity(in);
            }
        });
    }
}

我的输出json:

{"result":[{"id_kecamatan":"8","nama_kecamatan":"Batang Dua"},{"id_kecamatan":"7","nama_kecamatan":"Pulau Moti"},{"id_kecamatan":"6","nama_kecamatan":"Pulau Hiri"},{"id_kecamatan":"5","nama_kecamatan":"Pulau Ternate"},{"id_kecamatan":"4","nama_kecamatan":"Ternate Utara"},{"id_kecamatan":"3","nama_kecamatan":"Ternate Tengah"},{"id_kecamatan":"2","nama_kecamatan":"Ternate Selatan"}]}

1 个答案:

答案 0 :(得分:2)

<强> ClassCastException

  

抛出以指示代码已尝试将对象强制转换为   它不是实例的子类。

Caused by: java.lang.ClassCastException: com.spp.udi.sppmap.Potensi cannot be cast to android.app.Activity

您正在使用 android inbuild Listview @ + id / android:list

所以,请扩展ListActivity ,而不是 ListFragment

public class Potensi extends ListActivity{