当没有互联网连接时,我的RSS阅读器崩溃了

时间:2011-11-28 15:52:20

标签: android rss android-asynctask

我有一个RSS阅读器。我的问题是,每次禁用wi-fi或3g时,我的应用程序崩溃。这是我的代码:

    private void loadFeed(AsyncTask<Void, Void, Void> asyncTask){
        try{ 
            BaseFeedParser parser = new BaseFeedParser();
            messages = parser.parse();

            }
        catch (Exception t){
            Log.e("OSFP.News_notloading",t.getMessage(),t);

            Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();

            finish();
        }
    }


    private void displayRss(){

        ArrayList<HashMap<String, String>> List_nea = new ArrayList<HashMap<String, String>>(messages.size());


        for (Message msg : messages){

            des.add(msg.getDescription());// keimeno
            text.add(msg.getTitle());// titlos
            url.add(msg.getLink());// link
            imgl.add(msg.getImgLink());

        HashMap<String, String> map = new HashMap<String, String>();
        map.put("name", msg.getTitle());
        map.put("date", msg.getDate());     


        List_nea.add(map);
        ListAdapter mSchedule = new SimpleAdapter(this, List_nea, R.layout.row,
                new String[] {"name", "date"}, new int[] {R.id.TextView01, R.id.TextView02});
        this.setListAdapter(mSchedule);
        }}

public class BackgroundAsyncTask_nea extends
       AsyncTask<Void, Void, Void> {
          private ProgressDialog dialog;
        int myProgress;   

        @Override
        protected void onPostExecute(Void result) {
            displayRss();
             dialog.dismiss();


        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
              dialog = ProgressDialog.show(nea.this, "", "Loading. Please wait...", true);
            myProgress = 0;
        }

        protected void onProgressUpdate(Integer... progress) {
          // TODO Auto-generated method stub
          //super.onProgressUpdate(values);

            dialog.setProgress(progress[0]);
         }

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            loadFeed(this);
            return null;
        }



    }

我怎么能解决它?谢谢

logcat的:

11-28 18:03:17.640: E/OSFP.News_notloading(6960): java.lang.RuntimeException: java.net.UnknownHostException: feeds.feedburner.com
11-28 18:03:17.640: E/OSFP.News_notloading(6960): java.lang.RuntimeException: java.lang.RuntimeException: java.net.UnknownHostException: feeds.feedburner.com
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at kostas.menu.olympiakos.BaseFeedParser.parse(BaseFeedParser.java:94)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at kostas.menu.olympiakos.nea.loadFeed(nea.java:73)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at kostas.menu.olympiakos.nea.access$1(nea.java:70)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at kostas.menu.olympiakos.nea$BackgroundAsyncTask_nea.doInBackground(nea.java:235)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at kostas.menu.olympiakos.nea$BackgroundAsyncTask_nea.doInBackground(nea.java:1)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at android.os.AsyncTask$2.call(AsyncTask.java:185)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at java.util.concurrent.FutureTask.run(FutureTask.java:138)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at java.lang.Thread.run(Thread.java:1019)
11-28 18:03:17.640: E/OSFP.News_notloading(6960): Caused by: java.lang.RuntimeException: java.net.UnknownHostException: feeds.feedburner.com
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at kostas.menu.olympiakos.BaseFeedParser.getInputStream(BaseFeedParser.java:47)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at kostas.menu.olympiakos.BaseFeedParser.parse(BaseFeedParser.java:92)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   ... 10 more
11-28 18:03:17.640: E/OSFP.News_notloading(6960): Caused by: java.net.UnknownHostException: feeds.feedburner.com
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at java.net.InetAddress.getAllByName(InetAddress.java:256)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1018)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:512)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   at kostas.menu.olympiakos.BaseFeedParser.getInputStream(BaseFeedParser.java:45)
11-28 18:03:17.640: E/OSFP.News_notloading(6960):   ... 11 more
11-28 18:03:17.640: W/dalvikvm(6960): threadid=9: thread exiting with uncaught exception (group=0x40015578)
11-28 18:03:17.702: E/AndroidRuntime(6960): FATAL EXCEPTION: AsyncTask #1
11-28 18:03:17.702: E/AndroidRuntime(6960): java.lang.RuntimeException: An error occured while executing doInBackground()
11-28 18:03:17.702: E/AndroidRuntime(6960):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
11-28 18:03:17.702: E/AndroidRuntime(6960):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
11-28 18:03:17.702: E/AndroidRuntime(6960):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
11-28 18:03:17.702: E/AndroidRuntime(6960):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
11-28 18:03:17.702: E/AndroidRuntime(6960):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
11-28 18:03:17.702: E/AndroidRuntime(6960):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
11-28 18:03:17.702: E/AndroidRuntime(6960):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
11-28 18:03:17.702: E/AndroidRuntime(6960):     at java.lang.Thread.run(Thread.java:1019)
11-28 18:03:17.702: E/AndroidRuntime(6960): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11-28 18:03:17.702: E/AndroidRuntime(6960):     at android.os.Handler.<init>(Handler.java:121)
11-28 18:03:17.702: E/AndroidRuntime(6960):     at android.widget.Toast.<init>(Toast.java:68)
11-28 18:03:17.702: E/AndroidRuntime(6960):     at android.widget.Toast.makeText(Toast.java:231)
11-28 18:03:17.702: E/AndroidRuntime(6960):     at kostas.menu.olympiakos.nea.loadFeed(nea.java:79)
11-28 18:03:17.702: E/AndroidRuntime(6960):     at kostas.menu.olympiakos.nea.access$1(nea.java:70)
11-28 18:03:17.702: E/AndroidRuntime(6960):     at kostas.menu.olympiakos.nea$BackgroundAsyncTask_nea.doInBackground(nea.java:235)
11-28 18:03:17.702: E/AndroidRuntime(6960):     at kostas.menu.olympiakos.nea$BackgroundAsyncTask_nea.doInBackground(nea.java:1)
11-28 18:03:17.702: E/AndroidRuntime(6960):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
11-28 18:03:17.702: E/AndroidRuntime(6960):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
11-28 18:03:17.702: E/AndroidRuntime(6960):     ... 4 more
11-28 18:03:24.710: E/WindowManager(6960): Activity kostas.menu.olympiakos.nea has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40528658 that was originally added here
11-28 18:03:24.710: E/WindowManager(6960): android.view.WindowLeaked: Activity kostas.menu.olympiakos.nea has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40528658 that was originally added here
11-28 18:03:24.710: E/WindowManager(6960):  at android.view.ViewRoot.<init>(ViewRoot.java:263)
11-28 18:03:24.710: E/WindowManager(6960):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:171)
11-28 18:03:24.710: E/WindowManager(6960):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:114)
11-28 18:03:24.710: E/WindowManager(6960):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
11-28 18:03:24.710: E/WindowManager(6960):  at android.app.Dialog.show(Dialog.java:241)
11-28 18:03:24.710: E/WindowManager(6960):  at android.app.ProgressDialog.show(ProgressDialog.java:107)
11-28 18:03:24.710: E/WindowManager(6960):  at android.app.ProgressDialog.show(ProgressDialog.java:90)
11-28 18:03:24.710: E/WindowManager(6960):  at kostas.menu.olympiakos.nea$BackgroundAsyncTask_nea.onPreExecute(nea.java:221)
11-28 18:03:24.710: E/WindowManager(6960):  at android.os.AsyncTask.execute(AsyncTask.java:391)
11-28 18:03:24.710: E/WindowManager(6960):  at kostas.menu.olympiakos.nea.onCreate(nea.java:61)
11-28 18:03:24.710: E/WindowManager(6960):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-28 18:03:24.710: E/WindowManager(6960):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
11-28 18:03:24.710: E/WindowManager(6960):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-28 18:03:24.710: E/WindowManager(6960):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-28 18:03:24.710: E/WindowManager(6960):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-28 18:03:24.710: E/WindowManager(6960):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 18:03:24.710: E/WindowManager(6960):  at android.os.Looper.loop(Looper.java:130)
11-28 18:03:24.710: E/WindowManager(6960):  at android.app.ActivityThread.main(ActivityThread.java:3687)
11-28 18:03:24.710: E/WindowManager(6960):  at java.lang.reflect.Method.invokeNative(Native Method)
11-28 18:03:24.710: E/WindowManager(6960):  at java.lang.reflect.Method.invoke(Method.java:507)
11-28 18:03:24.710: E/WindowManager(6960):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
11-28 18:03:24.710: E/WindowManager(6960):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-28 18:03:24.710: E/WindowManager(6960):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

有一些问题: 您的onPostExecute方法会调用displayRss,并且在那里您正在迭代messages,而不会检查它是否为空。因此,当您运行时无法访问Internet时,您可能会收到NullPointerException(因为您的parse方法可能会抛出异常messages永远不会被初始化)。

你的吐司没有显示,因为你没有打电话给show。你需要做这样的事情:

Toast myToast = Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT);
myToast.show();