-1

我正在为我最近完成的一个先前发布的 iOS 项目移植到 Android。此应用程序从站点解析 XML 并将 XML 元素中的文本填充到 ListView 中。我的 XML 解析部分没有问题。我遇到的问题是将这些数据加载到 ListView 中。我有一个自定义适配器来处理将填充在每一行上的文本和按钮,但是应用程序在我设置适配器的行崩溃:lv.setAdapter(adap)。该错误是空指针异常。错误和代码如下。我相信我错过了 ListView 和适配器的一步。我还验证了我的 ArrayList menuItems 有数据并且不为空。我将不胜感激任何输入、指导或反馈。

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to startactivity 
ComponentInfo{tv.undignified.android/tv.undignified.android.Undignified}:     
java.lang.RuntimeException: Unable to start activity ComponentInfo{tv.undignified.android/ java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{tv.undignified.android/tv.undignified.android.Podcasts}: java.lang.NullPointerException

Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{tv.undignified.android/tv.undignified.android.Podcasts}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
at android.widget.TabHost.setCurrentTab(TabHost.java:323)
at android.widget.TabHost.addTab(TabHost.java:213)
at tv.undignified.android.Undignified.onCreate(Undignified.java:48)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

主要代码:

public class Podcasts extends ListActivity{

    private static final String HASHMAP_ID = "_id";
    private CustomAdapter adap;

    static final String URL = "http://www.undignified.podbean.com/feed";

    //XML Node Keys
    static final String KEY_ITEM  = "item";
    static final String KEY_TITLE = "title";
    static final String KEY_DESCRIPTION = "itunes:subtitle";
    static final String KEY_PODCASTURL = "enclosure";

    public static final  ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 

        setContentView(R.layout.podcasts); 



        UndigParser uparser = new UndigParser();

        String xml = uparser.getXmlFromURL(URL);        

        Document doc = uparser.getDomElement(xml);    

        NodeList nl = doc.getElementsByTagName(KEY_ITEM);

        for(int i = 0; i < nl.getLength(); i++){

            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);


            map.put(KEY_TITLE, uparser.getValue(e, KEY_TITLE));
            map.put(KEY_DESCRIPTION, uparser.getValue(e, KEY_DESCRIPTION));
            map.put(KEY_PODCASTURL, uparser.getValue(e, KEY_PODCASTURL));



            menuItems.add(map);
        }

        adap = new CustomAdapter(Podcasts.this, menuItems);           
       ListView lv = (ListView) findViewById(R.id.LinearLayout01);

       lv.setAdapter(adap);


    }

    public static class CustomAdapter extends BaseAdapter implements Filterable{

        private LayoutInflater mInflater;
        private Context context;


        public CustomAdapter(Context context, ArrayList<HashMap<String,String>>menuItems){

            mInflater = LayoutInflater.from(context);
            this.context = context;
        }

        public View getView(final int position, View convertView, ViewGroup parent){
            ViewHolder holder;

                convertView = mInflater.inflate(R.layout.list_item, null);

                holder = new ViewHolder();
                holder.description=(TextView)convertView.findViewById(R.id.description);
                holder.podcasturl=(TextView)convertView.findViewById(R.id.podcasturl);
                holder.title=(TextView)convertView.findViewById(R.id.title);
                holder.downloadBTN=(Button)convertView.findViewById(R.id.btnStartDownload);

                convertView.setOnClickListener(new OnClickListener(){
                    private int pos = position;

                    @Override
                    public void onClick(View v){
                        Toast.makeText(context, "Text-" + String.valueOf(pos), Toast.LENGTH_SHORT).show();
                    }
                });

                holder.downloadBTN.setOnClickListener(new OnClickListener(){
                    private int pos = position;

                    @Override
                    public void onClick(View v){
                        Toast.makeText(context, "Button-"+String.valueOf(pos), Toast.LENGTH_SHORT).show();
                    }
                });
                convertView.setTag(holder);
            return convertView;
        }

        static class ViewHolder{
            TextView description;
            TextView title;
            TextView podcasturl;
            Button downloadBTN;
        }

        @Override
        public int getCount() {

            Log.d("Sizetest2343",""+menuItems.size());
            return menuItems.size();
        }

        @Override
        public Object getItem(int position) {

            return null;
        }

        @Override
        public long getItemId(int position) {

            return position;
        }

        @Override
        public Filter getFilter() {
            return null;
        }
    }

 }

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dip">

<LinearLayout
    android:id="@+id/LinearLayout01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:id="@+id/startPodcastDownload"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:focusable="false"
        android:text="Download Podcast"> 
    </Button>        


</LinearLayout>    
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/LinearLayout01"
            android:textColor="#acacac"
            android:paddingBottom="2dip">
        </TextView>
        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/LinearLayout01"
            android:layout_below="@id/title"
            android:textColor="#acacac"
            android:paddingBottom="2dip">
        </TextView>
        <TextView
            android:id="@+id/podcasturl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/LinearLayout01"
            android:layout_below="@+id/description"
            android:textColor="#acacac"
            android:paddingBottom="2dip">
        </TextView>

</RelativeLayout>
4

1 回答 1

1

我认为问题的一部分可能是您的代码行:

ListView lv = (ListView) findViewById(R.id.LinearLayout01);

在这里,您尝试从 LinearLayout 制作 ListView。我的布局中通常会有这样的东西:

<ListView android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="0.7" />

然后你在你的活动中膨胀列表视图,如下所示:

ListView friendList = (ListView)findViewById(android.R.id.list);

使用 android.R.id.list 参数很重要。

于 2012-07-27T18:50:03.930 回答