0

この関数でこのエラーが発生しましたが、正確に解決する方法がわかりません。「潜在的なnullポインタアクセス:この場所では変数ストリームがnullである可能性があります」以下のコードは次のとおりです。

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 ();
4

2 に答える 2

1
    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;
    }
于 2013-02-25T18:58:04.173 に答える
0

この行を次のように変更します

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

そうすれば、null値で誤った行に到達することはありません。

于 2013-02-25T18:57:52.780 に答える