Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从web获取RSS提要并在自定义列表视图中显示它们_Android_Eclipse_Android Emulator - Fatal编程技术网

Android 如何从web获取RSS提要并在自定义列表视图中显示它们

Android 如何从web获取RSS提要并在自定义列表视图中显示它们,android,eclipse,android-emulator,Android,Eclipse,Android Emulator,我编写这段代码是为了在自定义列表视图中解析后显示新闻提要(RSS提要)。。但我有一个例外,实际上我不知道问题在哪里,也不知道如何解决这个例外 编辑: 07-27 09:55:40.866: D/AndroidRuntime(6035): Shutting down VM 07-27 09:55:40.866: W/dalvikvm(6035): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 07-27

我编写这段代码是为了在自定义列表视图中解析后显示新闻提要(RSS提要)。。但我有一个例外,实际上我不知道问题在哪里,也不知道如何解决这个例外

编辑:

07-27 09:55:40.866: D/AndroidRuntime(6035): Shutting down VM
07-27 09:55:40.866: W/dalvikvm(6035): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-27 09:55:40.940: E/AndroidRuntime(6035): FATAL EXCEPTION: main
07-27 09:55:40.940: E/AndroidRuntime(6035): java.lang.NullPointerException
07-27 09:55:40.940: E/AndroidRuntime(6035):     at com.example.testfeeds.MainActivity.parseXML(MainActivity.java:286)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at com.example.testfeeds.MainActivity.access$0(MainActivity.java:227)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at com.example.testfeeds.MainActivity$GetFeeds.onPostExecute(MainActivity.java:169)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at com.example.testfeeds.MainActivity$GetFeeds.onPostExecute(MainActivity.java:1)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at android.os.AsyncTask.finish(AsyncTask.java:417)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at android.os.Looper.loop(Looper.java:123)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at java.lang.reflect.Method.invokeNative(Native Method)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at java.lang.reflect.Method.invoke(Method.java:521)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at dalvik.system.NativeStart.main(Native Method)
我通过改变以下位置解决了此问题:

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,feedsTitles);

             feedsList.setAdapter(new TempLyaout(MainActivity.this,feedsTitles));
所以我得到了RSS提要,并在自定义列表视图中显示它们^__^

main活动类:

package com.example.testfeeds;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

@SuppressWarnings("unused")
public class MainActivity extends Activity {

    String name;
    Button getfeeds;
    ArrayList<String> feedsTitles;
    ArrayList<String> feedsLinks;
    ListView feedsList;
    ArrayAdapter<String> adapter;
    ProgressDialog progress ;



    @SuppressWarnings("null")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getfeeds = (Button) findViewById(R.id.button1);
        feedsList=(ListView) findViewById(R.id.listView1);


        progress = new ProgressDialog(this); 
        progress.setMessage("Loading .... "); 
        progress.setCancelable(false); 

        feedsTitles = new ArrayList<String>();
        feedsLinks = new ArrayList<String>();

        @SuppressWarnings("unchecked")
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,feedsTitles);

         feedsList.setAdapter(new TempLyaout(MainActivity.this,feedsTitles));

        getfeeds.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            new GetFeeds().execute("http://yunn.yu.edu.jo/index.php?option=com_content&view=category&id=55&layout=blog&Itemid=104&format=feed&type=rss"); 

            }
        });

        feedsList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapter, View view, int position,
                    long id) {



                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(feedsLinks.get(position)));

                startActivity(browserIntent);

            }
        });



    }

////////////////////////////////////////////////////////////////////////////

     class GetFeeds extends AsyncTask<String, Integer, String>{


         @Override
            protected void onPreExecute() {

                progress.show();
                super.onPreExecute();
            }

        @Override
        protected String doInBackground(String... arg0) {

            String xmlData = GetUrlBody(arg0[0]); 

            Log.d("msg","in doInBackground .. ");


            return xmlData ;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {

            super.onProgressUpdate(values);
        }


        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            Log.d("msg","the result not null");

            ////--------------------------------------////

            //Log.d("msg",result);

            FileOutputStream makeXml = null ; //declare a file to store the incoming xml data.
            InputStream xmlFile = null; // declare an input stream to get the xml file.
            try {


                makeXml= openFileOutput("temp.xml", Context.MODE_PRIVATE);
                makeXml.write(result.getBytes());
                makeXml.flush();
                makeXml.close(); 

                xmlFile= openFileInput("temp.xml"); 



            } catch (IOException e) {


                e.printStackTrace();
            }

          ////--------------------------------------////


            XmlPullParserFactory pullParserFactory;

            try {
                pullParserFactory = XmlPullParserFactory.newInstance();
                XmlPullParser parser = pullParserFactory.newPullParser();
                    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                    parser.setInput(xmlFile, null);
                    parseXML(parser);
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (IOException e) {

                e.printStackTrace();
            }


            progress.dismiss();

        }




     }


////////////////////////////////////////////////////////////////////////////

        String GetUrlBody (String Url ){

            Log.d("msg","in get url ..");

        HttpClient cli = new DefaultHttpClient(); 



        HttpGet g = new HttpGet(Url); 



        try{
        HttpResponse res = cli.execute(g);



        if(res.getStatusLine().getStatusCode() == 200){

            String s =
        EntityUtils.toString(res.getEntity(), HTTP.UTF_8); 

            return s; 

        }else {
            return "Not Found"; 
        }

        }catch(Exception exx){}


        return null; 
    }


////////////////////////////////////////////////////////////////////////////

        private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
        {

            int eventType = parser.getEventType();
            int getElement=0;
            String title,link,pubDate;


            while (eventType != XmlPullParser.END_DOCUMENT){

                String tagName = null;


                switch (eventType){

                //----------------------------------//
                case XmlPullParser.START_DOCUMENT: 
                    feedsTitles.clear();
                    feedsLinks.clear();
                    break;
               //----------------------------------//
                 case XmlPullParser.START_TAG:
                     tagName = parser.getName();
                     if ("item".equals(tagName)){
                          getElement=1;
                     } else if (getElement==1){
                         if ("title".equals(tagName)){
                             title = parser.nextText();
                             Log.d("value",title);
                             feedsTitles.add(title);
                         } else if ("link".equals(tagName)){
                            link = parser.nextText();
                            Log.d("value",link);
                            feedsLinks.add(link);
                         } else if ("pubDate".equals(tagName)){
                             pubDate= parser.nextText();
                             Log.d("value",pubDate);
                         }
                     }
                     break;                     
               //----------------------------------//
                 case XmlPullParser.END_TAG:
                     tagName = parser.getName();
                     if (tagName.equalsIgnoreCase("item") && getElement != 0){

                     }

                     break;

              //----------------------------------//


                }// end-switch.


                eventType= parser.next();

            }// end-while.


        } //end xmlParser method.


////////////////////////////////////////////////////////////////////////////


} // end- MainActivity class.
package com.example.testfeeds;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;


public class TempLyaout extends ArrayAdapter<String>  {

    Context context;
    ArrayList<String> sa;




    public TempLyaout(Context context , ArrayList<String> sa) {
        super(context,R.layout.list_view,sa);
        this.context=context;
        this.sa = sa;
    }





    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        LayoutInflater l = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        View rowView = l.inflate(R.layout.list_view, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.textView1);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);


        textView.setText(sa.get(position));


        return rowView;
    }




}

您确定服务器返回的响应有效吗?
java.lang.NullPointerException 07-27 09:55:40.940:E/AndroidRuntime(6035):在com.example.testfeeds.MainActivity.parseXML(MainActivity.java:286)
让我们从“哪一个是第286行”开始,您的parseXML函数似乎没有返回预期值,我们需要从第286行开始,您的适配器的值似乎没有改变,这就是为什么它不能返回布尔值。如果适配器在某些情况下没有更改,那么它不会更改,因此不会返回任何内容,这将返回一个空指针。如果您只需要在适配器更改时收到通知,则可以在语句的第286行使用try-catch块放置断点。检查变量。哪一个是空的?
07-27 09:55:40.866: D/AndroidRuntime(6035): Shutting down VM
07-27 09:55:40.866: W/dalvikvm(6035): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-27 09:55:40.940: E/AndroidRuntime(6035): FATAL EXCEPTION: main
07-27 09:55:40.940: E/AndroidRuntime(6035): java.lang.NullPointerException
07-27 09:55:40.940: E/AndroidRuntime(6035):     at com.example.testfeeds.MainActivity.parseXML(MainActivity.java:286)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at com.example.testfeeds.MainActivity.access$0(MainActivity.java:227)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at com.example.testfeeds.MainActivity$GetFeeds.onPostExecute(MainActivity.java:169)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at com.example.testfeeds.MainActivity$GetFeeds.onPostExecute(MainActivity.java:1)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at android.os.AsyncTask.finish(AsyncTask.java:417)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at android.os.Looper.loop(Looper.java:123)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at java.lang.reflect.Method.invokeNative(Native Method)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at java.lang.reflect.Method.invoke(Method.java:521)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-27 09:55:40.940: E/AndroidRuntime(6035):     at dalvik.system.NativeStart.main(Native Method)