简体   繁体   English

应用程序在 onPostExecute 方法中分配某些内容时崩溃

[英]App crashes up on assigning something in onPostExecute method

As a beginner, I am following a lecture on JSON Demo (with exactly the same code and same version) of andriod studio.作为初学者,我正在关注andriod studio的JSON Demo(完全相同的代码和相同的版本)的讲座。 For some reason the app is crashing in the emulator whenever I add something in onPostExecute method.出于某种原因,每当我在 onPostExecute 方法中添加某些内容时,应用程序就会在模拟器中崩溃。 For instance, it doesn't crash with following code:例如,它不会因以下代码而崩溃:

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

But it crashes when I add very basic to see the result in log like below:但是当我添加非常基本的以查看日志中的结果时,它会崩溃,如下所示:

@Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        Log.i("Website Content: ", result);

    }

I am not sure what's wrong here.我不确定这里有什么问题。 I tried restarting the Android Studio for the sake RAM memory allocation, my internet is on, it debugs nicely in gradle console but gives this in android monitor: I tried restarting the Android Studio for the sake RAM memory allocation, my internet is on, it debugs nicely in gradle console but gives this in android monitor:

 06-27 19:16:35.201 18116-18157/com.example.rushi.jsondemo V/RenderScript: 0xae53b000 Launching thread(s), CPUs 2 06-27 19:16:35.396 18116-19529/com.example.rushi.jsondemo W/System.err: java.io.FileNotFoundException: http://api.openweathermap.org/data/2.5/weather?q=London,uk 06-27 19:16:35.396 18116-19529/com.example.rushi.jsondemo W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238) 06-27 19:16:35.396 18116-19529/com.example.rushi.jsondemo W/System.err: at com.example.rushi.jsondemo.MainActivity$DownloadTask$override.doInBackground(MainActivity.java:44) 06-27 19:16:35.396 18116-19529/com.example.rushi.jsondemo W/System.err: at com.example.rushi.jsondemo.MainActivity$DownloadTask$override.access$dispatch(MainActivity.java) 06-27 19:16:35.396 18116-19529/com.example.rushi.jsondemo W/System.err: at com.example.rushi.jsondemo.MainActivity$DownloadTask.doInBackground(MainActivity.java:0) 06-27 19:16:35.396 18116-19529/com.example.rushi.jsondemo W/System.err: at com.example.rushi.jsondemo.MainActivity$DownloadTask.doInBackground(MainActivity.java:30) 06-27 19:16:35.396 18116-19529/com.example.rushi.jsondemo W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295) 06-27 19:16:35.396 18116-19529/com.example.rushi.jsondemo W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237) 06-27 19:16:35.396 18116-19529/com.example.rushi.jsondemo W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 06-27 19:16:35.396 18116-19529/com.example.rushi.jsondemo W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 06-27 19:16:35.411 18116-19529/com.example.rushi.jsondemo W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 06-27 19:16:35.411 18116-19529/com.example.rushi.jsondemo W/System.err: at java.lang.Thread.run(Thread.java:818) 06-27 19:16:35.451 18116-18116/com.example.rushi.jsondemo D/AndroidRuntime: Shutting down VM 06-27 19:16:35.529 18116-18116/com.example.rushi.jsondemo E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.rushi.jsondemo, PID: 18116 java.lang.NullPointerException: println needs a message at android.util.Log.println_native(Native Method) at android.util.Log.i(Log.java:160) at com.example.rushi.jsondemo.MainActivity$DownloadTask$override.onPostExecute(MainActivity.java:74) at com.example.rushi.jsondemo.MainActivity$DownloadTask$override.access$dispatch(MainActivity.java) at com.example.rushi.jsondemo.MainActivity$DownloadTask.onPostExecute(MainActivity.java:0) at com.example.rushi.jsondemo.MainActivity$DownloadTask.onPostExecute(MainActivity.java:30) at android.os.AsyncTask.finish(AsyncTask.java:651) at android.os.AsyncTask.-wrap1(AsyncTask.java) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 06-27 19:19:47.263 18116-18122/com.example.rushi.jsondemo W/art: Suspending all threads took: 7.513ms 06-27 19:21:35.896 18116-18116/com.example.rushi.jsondemo I/Process: Sending signal. PID: 18116 SIG: 9

Here is my complete code:这是我的完整代码:

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends Activity {

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

        DownloadTask task = new DownloadTask();
        task.execute("http://api.openweathermap.org/data/2.5/weather?q=London,uk");

    }

    public class DownloadTask extends AsyncTask<String, Void, String> {

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

            String result = "";
            URL url;
            HttpURLConnection urlConnection = null;

            try {
                url = new URL(urls[0]);

                urlConnection = (HttpURLConnection) url.openConnection();

                InputStream in = urlConnection.getInputStream();

                InputStreamReader reader = new InputStreamReader(in);

                int data = reader.read();

                while (data != -1) {

                    char current = (char) data;

                    result += current;

                    data = reader.read();

                }

                return result;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            }catch (IOException e){
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            Log.i("Website Content: ", result);

        }

    }

}

Apologies for long post.为长篇道歉。 Thanks.谢谢。

app crashes because the API returns nothing that because you haven't specified API KEY as document says https://openweathermap.org/faq#error401 , https://openweathermap.org/appid app crashes because the API returns nothing that because you haven't specified API KEY as document says https://openweathermap.org/faq#error401 , https://openweathermap.org/appid

Your error basically complains about a null or empty string.您的错误基本上抱怨 null 或空字符串。 This your URL returns with a 401 error, which is explaining why you don't get that your string populated as expected.您的 URL 返回 401 错误,这解释了为什么您没有按预期填充字符串。 So to avoid crashes or errors like this, before your log try checking the String result for containing something.因此,为避免此类崩溃或错误,在您的日志尝试检查字符串结果是否包含某些内容之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM