执行 doInBackground() 时发生 java.lang.RuntimeException 错误?

编程入门 行业动态 更新时间:2024-10-21 11:34:17
本文介绍了执行 doInBackground() 时发生 java.lang.RuntimeException 错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我是 android 初学者,对于一个统一课程的项目,我们必须开发 5 个简单的应用程序,然后将它们组合"成一个.因此,您从单个 Activity/ListView 启动每个应用程序.所有应用程序都单独运行正常,但是从列表视图中运行一个应用程序时,我得到以下信息*异步任务 #1执行 doinBackground() 时发生错误;*

I am beginner in android, and for a project for a uny course we have to develop 5 simple apps and then "combine" them in one.So you start each app from a single Activity/ListView. All the apps are running ok individually but when running one app from the listview i get the following *AsyncTask #1 an error occured while executing doinBackground();*

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


    reportarray= new ArrayList<String>();


    try {
        URL url = new URL("http://www.yr.no/sted/Sverige/Kronoberg/V%E4xj%F6/forecast.xml");
        AsyncTask task = new WeatherRetriever().execute(url);
    } catch (IOException ioe ) {
        ioe.printStackTrace();
    }
    adapter= new Adapter(this);
    setListAdapter(adapter);



}

这似乎是一个线程问题,这是我使用的更多代码

It seems that is a thread issue , here is some more code i use

private class WeatherRetriever extends AsyncTask<URL, Void, WeatherReport> {
    protected WeatherReport doInBackground(URL... urls) {
        try {
            return WeatherHandler.getWeatherReport(urls[0]);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } 
    }

    protected void onProgressUpdate(Void... progress) {

    }

    protected void onPostExecute(WeatherReport result) {
        report = result;




        runOnUiThread(new Runnable() {
              public void run() {



                  reportarray.add(report.toString());;

                  for (WeatherForecast forecast : report) {
                        reportarray.add(forecast.toString());

                    }
                adapter.notifyDataSetChanged();
              }
            });
        PrintReportToConsole();


    }
}

}

LOGCAT 输出

09-10 14:51:36.347: E/AndroidRuntime(1411): FATAL EXCEPTION: AsyncTask #1
09-10 14:51:36.347: E/AndroidRuntime(1411): java.lang.RuntimeException: An error occured while executing doInBackground()
09-10 14:51:36.347: E/AndroidRuntime(1411):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
09-10 14:51:36.347: E/AndroidRuntime(1411):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
09-10 14:51:36.347: E/AndroidRuntime(1411):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
09-10 14:51:36.347: E/AndroidRuntime(1411):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
09-10 14:51:36.347: E/AndroidRuntime(1411):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-10 14:51:36.347: E/AndroidRuntime(1411):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-10 14:51:36.347: E/AndroidRuntime(1411):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-10 14:51:36.347: E/AndroidRuntime(1411):     at java.lang.Thread.run(Thread.java:856)
09-10 14:51:36.347: E/AndroidRuntime(1411): Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
09-10 14:51:36.347: E/AndroidRuntime(1411):     at com.example.ass1.VaxjoWeather$WeatherRetriever.doInBackground(VaxjoWeather.java:133)
09-10 14:51:36.347: E/AndroidRuntime(1411):     at com.example.ass1.VaxjoWeather$WeatherRetriever.doInBackground(VaxjoWeather.java:1)
09-10 14:51:36.347: E/AndroidRuntime(1411):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-10 14:51:36.347: E/AndroidRuntime(1411):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-10 14:51:36.347: E/AndroidRuntime(1411):     ... 4 more
09-10 14:51:36.347: E/AndroidRuntime(1411): Caused by: java.lang.RuntimeException: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
09-10 14:51:36.347: E/AndroidRuntime(1411):     at com.example.ass1.WeatherHandler.getWeatherReport(WeatherHandler.java:54)
09-10 14:51:36.347: E/AndroidRuntime(1411):     at com.example.ass1.VaxjoWeather$WeatherRetriever.doInBackground(VaxjoWeather.java:131)
09-10 14:51:36.347: E/AndroidRuntime(1411):     ... 7 more
09-10 14:51:36.347: E/AndroidRuntime(1411): Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
09-10 14:51:36.347: E/AndroidRuntime(1411):     at java.InetAddress.lookupHostByName(InetAddress.java:418)
09-10 14:51:36.347: E/AndroidRuntime(1411):     at java.InetAddress.getAllByNameImpl(InetAddress.java:236)

推荐答案

您似乎没有适当的权限.将此行添加到您的 manifest.xml

It appears you don't have the appropriate permissions. Add this line to your manifest.xml

 <uses-permission android:name="android.permission.INTERNET"></uses-permission>

这篇关于执行 doInBackground() 时发生 java.lang.RuntimeException 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 03:43:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034398.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   发生   doInBackground   java   RuntimeException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!