如果该站点不可用,我的应用程序将崩溃。如何解决?

Intent FirstIntent = new Intent().setClass(this, RssChannelActivity.class);
    FirstIntent.putExtra("rss-url", "http://rss-link");
    String FirstTabName = getResources().getString(R.string.tab_1);
    TabSpec FirstTabSpec = tabHost.newTabSpec(FirstTabName)
                .setIndicator(FirstTabName, getResources().getDrawable(R.drawable.rss_tab_1))
                .setContent(FirstIntent);
    tabHost.addTab(FirstTabSpec);

AsyncTask:
    @Override
    protected List<RssItem> doInBackground(String... urls) {
        try {
            // Create RSS reader
            RssReader rssReader = new RssReader(urls[0]);

            // Parse RSS, get items
            return rssReader.getItems();

        } catch (Exception e) {
            Log.e("RssChannelActivity", e.getMessage());
        }

        return null;
    }

    @Override
    protected void onPostExecute(List<RssItem> result) {

        // Get a ListView from the RSS Channel view
        ListView itcItems = (ListView) findViewById(R.id.rssChannelListView);

        // Create a list adapter
        ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(local,android.R.layout.simple_list_item_1, result);
        // Set list adapter for the ListView
        itcItems.setAdapter(adapter);

        // Set list view item click listener
        itcItems.setOnItemClickListener(new ListListener(result, local));
    }

LogCat:
04-12 21:17:49.496: D/dalvikvm(20954): GC_FOR_ALLOC freed 373K, 3% free 18044K/18448K, paused 11ms, total 11ms
04-12 21:18:02.926: E/RssChannelActivity(20954): At line 45, column 246: not well-formed (invalid token)
04-12 21:18:02.926: D/AndroidRuntime(20954): Shutting down VM
04-12 21:18:02.936: W/dalvikvm(20954): threadid=1: thread exiting with uncaught exception (group=0x41b62ba8)
04-12 21:18:02.956: D/dalvikvm(20954): GC_FOR_ALLOC freed 571K, 4% free 17986K/18588K, paused 22ms, total 23ms
04-12 21:18:02.956: E/AndroidRuntime(20954): FATAL EXCEPTION: main
04-12 21:18:02.956: E/AndroidRuntime(20954): Process: package.name, PID: 20954
04-12 21:18:02.956: E/AndroidRuntime(20954): java.lang.NullPointerException
04-12 21:18:02.956: E/AndroidRuntime(20954):    at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
04-12 21:18:02.956: E/AndroidRuntime(20954):    at android.widget.ListView.setAdapter(ListView.java:480)
04-12 21:18:02.956: E/AndroidRuntime(20954):    at package.name.RssChannelActivity$GetRSSDataTask.onPostExecute(RssChannelActivity.java:69)
04-12 21:18:02.956: E/AndroidRuntime(20954):    at package.name.RssChannelActivity$GetRSSDataTask.onPostExecute(RssChannelActivity.java:1)
04-12 21:18:02.956: E/AndroidRuntime(20954):    at android.os.AsyncTask.finish(AsyncTask.java:632)
04-12 21:18:02.956: E/AndroidRuntime(20954):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-12 21:18:02.956: E/AndroidRuntime(20954):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
04-12 21:18:02.956: E/AndroidRuntime(20954):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-12 21:18:02.956: E/AndroidRuntime(20954):    at android.os.Looper.loop(Looper.java:136)
04-12 21:18:02.956: E/AndroidRuntime(20954):    at
android.app.ActivityThread.main(ActivityThread.java:5017)
04-12 21:18:02.956: E/AndroidRuntime(20954):    at java.lang.reflect.Method.invokeNative(Native Method)
04-12 21:18:02.956: E/AndroidRuntime(20954):    at java.lang.reflect.Method.invoke(Method.java:515)
04-12 21:18:02.956: E/AndroidRuntime(20954):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-12 21:18:02.956: E/AndroidRuntime(20954):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-12 21:18:02.956: E/AndroidRuntime(20954):    at dalvik.system.NativeStart.main(Native Method)

最佳答案

您需要验证onPostExecute()中的结果是否不为null。在使用类似变量防止NullPointerExceptions之前,检查此类变量是否已初始化始终是一个好主意。

@Override
protected void onPostExecute(List<RssItem> result) {

    if (result == null) {
        // Do something when no result is found
        return;
    }

    // Get a ListView from the RSS Channel view
    ListView itcItems = (ListView) findViewById(R.id.rssChannelListView);

    // Create a list adapter
    ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(local,android.R.layout.simple_list_item_1, result);
    // Set list adapter for the ListView
    itcItems.setAdapter(adapter);

    // Set list view item click listener
    itcItems.setOnItemClickListener(new ListListener(result, local));
}

关于android - 如果网站不可用,Android应用程序崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23030840/

10-09 22:50