我在此函数中收到此错误,并且不知道确切的解决方法。
“潜在的空指针访问:变量流在此位置可能为空”
下面是代码:

public void downloadAudioIncrement(String mediaUrl) throws IOException {
        /*URLConnection cn = new URL(mediaUrl).openConnection(Proxy.NO_PROXY);

        cn.connect();
        InputStream stream = cn.getInputStream();*/
        URL url = new URL(mediaUrl);

        InputStream stream = url.openStream();
        //Toast.makeText(this.context, "here3", Toast.LENGTH_LONG);

        if (stream == null) {
            Log.e(getClass().getName(), "Não é possível criar InputStream para a url: " + mediaUrl);
        }

        //downloadingMediaFile = new File(context.getCacheDir(),"downloadingMedia_" + (counter++) + ".dat");
        downloadingMediaFile = new File(context.getCacheDir(),"downloadingMedia_.dat");
        FileOutputStream out = new FileOutputStream(downloadingMediaFile);
        byte buf[] = new byte[16384];

        int totalBytesRead = 0, incrementalBytesRead = 0;
        do {
            int numread = ***stream***.read(buf);
            if (numread <= 0)
                break;

            out.write(buf, 0, numread);
            totalBytesRead += numread;
            incrementalBytesRead += numread;
            totalKbRead = totalBytesRead/1000;

            testMediaBuffer();
            fireDataLoadUpdate();
        } while (validateNotInterrupted());

        if (validateNotInterrupted()) {
            fireDataFullyLoaded();
            //testMediaBuffer();
            //fireDataLoadUpdate();
        }
        ***stream***.close();
        out.close();
    }


如何解决此错误?错误发生在这里:

 numread ***stream***.Read = int (buf);


和这里:

 ***stream***.Close ();

最佳答案

    if (stream == null) {
        Log.e(getClass().getName(), "Não é possível criar InputStream para a url: " + mediaUrl);
    }


在这里,您检查stream是否为null并将其记录下来,但是您仍然继续使用该方法,并且从未创建新的stream或其他任何内容。我的建议:在您的广告块中添加return;

    if (stream == null) {
        Log.e(getClass().getName(), "Não é possível criar InputStream para a url: " + mediaUrl);
        return;
    }

07-28 13:44