我在这个函数中收到了这个错误,不知道如何解决。“潜在的空指针访问:变量流在此位置可能为空”下面是代码:
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 ();