0

我有问题ListFragment。当我使用片段开始我的活动时,我的应用程序崩溃了。我使用选项卡主机,我想显示 XML 文件中的元素。

 import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.poki.apeiron.XMLParser;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;



public class Novosti extends ListFragment
{
     static final String URL = "http://apeiron.comyr.com/fit.xml";
        // XML node keys
        static final String KEY_NOVOST = "novost"; // parent node
        static final String KEY_NASLOV = "naslov";
        static final String KEY_DATUM = "datum";
        static final String KEY_TEXT = "text";
        static final String KEY_LINK = "link";
        static final String KEY_LINK1 = "link1";

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


    //String faks= (String) getActivity().getIntent().getSerializableExtra("Faks");

    //Toast.makeText(fa, faks, Toast.LENGTH_SHORT).show();
    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_NOVOST);
    // looping through all item nodes <item>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value

        map.put(KEY_NASLOV, parser.getValue(e, KEY_NASLOV));
        map.put(KEY_DATUM, "Datum: " + parser.getValue(e, KEY_DATUM));
        map.put(KEY_TEXT, "Novost: "+parser.getValue(e, KEY_TEXT));

        // adding HashList to ArrayList
        menuItems.add(map);
    }

    // Adding menuItems to ListView

    ListAdapter adapter = new SimpleAdapter(this.getActivity(), menuItems,R.layout.list_row,new String[] { KEY_NASLOV, KEY_DATUM, KEY_DATUM }, new int[] {
                    R.id.naslov, R.id.datum, R.id.novost });

    setListAdapter(adapter);
    //ListView listView = (ListView)view.findViewById(R.id.ListViewId);
    //listView.setAdapter((ListAdapter) new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));

    // selecting single ListView item
    ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String naslov = ((TextView) view.findViewById(R.id.naslov)).getText().toString();
            String datum = ((TextView) view.findViewById(R.id.datum)).getText().toString();
            String text = ((TextView) view.findViewById(R.id.novost)).getText().toString();
             //Context anAct = getActivity().getApplicationContext();
            //   Toast.makeText(anAct, "About!", Toast.LENGTH_SHORT).show(); 
            // Starting new intent
            Intent in = new Intent(getActivity(),Novost.class);
            in.putExtra(KEY_NASLOV, naslov);
            in.putExtra(KEY_DATUM, datum);
            in.putExtra(KEY_TEXT, text);
            in.putExtra("link", KEY_LINK);
            in.putExtra("link1", KEY_LINK1);
            startActivity(in);


        }
    });

        return view;
    }

}

如何解决?

这是来自 logcat 的日志:

03-12 00:34:40.579: E/AndroidRuntime(1984): FATAL EXCEPTION: main
03-12 00:34:40.579: E/AndroidRuntime(1984): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.poki.apeiron/com.poki.apeiron.Prikaz}: java.lang.ClassCastException: com.poki.apeiron.Novosti cannot be cast to android.app.Fragment
03-12 00:34:40.579: E/AndroidRuntime(1984):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-12 00:34:40.579: E/AndroidRuntime(1984):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-12 00:34:40.579: E/AndroidRuntime(1984):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-12 00:34:40.579: E/AndroidRuntime(1984):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-12 00:34:40.579: E/AndroidRuntime(1984):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 00:34:40.579: E/AndroidRuntime(1984):     at android.os.Looper.loop(Looper.java:137)
03-12 00:34:40.579: E/AndroidRuntime(1984):     at android.app.ActivityThread.main(ActivityThread.java:5039)
03-12 00:34:40.579: E/AndroidRuntime(1984):     at java.lang.reflect.Method.invokeNative(Native Method)
03-12 00:34:40.579: E/AndroidRuntime(1984):     at java.lang.reflect.Method.invoke(Method.java:511)
03-12 00:34:40.579: E/AndroidRuntime(1984):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-12 00:34:40.579: E/AndroidRuntime(1984):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-12 00:34:40.579: E/AndroidRuntime(1984):     at dalvik.system.NativeStart.main(Native Method)
03-12 00:34:40.579: E/AndroidRuntime(1984): Caused by: java.lang.ClassCastException: com.poki.apeiron.Novosti cannot be cast to android.app.Fragment
03-12 00:34:40.579: E/AndroidRuntime(1984):     at android.app.Fragment.instantiate(Fragment.java:585)
03-12 00:34:40.579: E/AndroidRuntime(1984):     at android.app.Fragment.instantiate(Fragment.java:560)
4

1 回答 1

0

您的 ListFragment 来自支持库

import android.support.v4.app.ListFragment; 

但是您使用的是 SDK 本身的 FragmentManager。

所以你可以按照 system32 的建议做,但你不会向后兼容。

或者,如果与 Android 版本 < 3.0 兼容对您很重要,您可以打电话getSupportFragmentManager()而不是打电话getFragmentManager()Activity

于 2013-03-12T01:19:15.147 回答