简体   繁体   English

如何在 Android 上的 Bufferedreader 中修复 NullPointerException?

[英]How to fix NullPointerException in Bufferedreader on Android?

Goal: Reading a HTML-file using Buffered reader in an AsyncTask on Android.目标:在 Android 上的AsyncTask使用缓冲阅读器读取 HTML 文件。

Problem: The debug log gives the following error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference .问题:调试日志给出以下错误: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference It gives the error at the onPostExecute void.它在onPostExecute void 处给出错误。 On further investigation I found out the following code: TVCode.setText(result);在进一步调查中,我发现了以下代码: TVCode.setText(result); is throwing the NullPointerException .正在抛出NullPointerException This means result = null .这意味着result = null I can't find out why result = null since similar code on the normal Java, using Eclipse , is working fine.我不知道为什么result = null因为在普通 Java 上使用 Eclipse 的类似代码工作正常。

Further info:更多信息:

Android Studio: 1.3.2安卓工作室:1.3.2

JDK: 1.7.0_79-b15 amd64 JDK:1.7.0_79-b15 amd64

Permissions:权限:

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Gradle: minSdkVersion 15, targetSdkVersion 23, compileSdkVersion 23 Gradle: minSdkVersion 15, targetSdkVersion 23, compileSdkVersion 23

Here is the code of MainActivity.java :这是MainActivity.java的代码:

public class MainActivity extends AppCompatActivity {


public static String ETURL = null;
public static final String TAG = MainActivity.class.getSimpleName();



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

        TextView TVCode = (TextView) findViewById(R.id.TVCode);
        ImageButton BTSend = (ImageButton) findViewById(R.id.BTSend);
        final EditText ETURLInput = (EditText) findViewById(R.id.ETUrlInput);

        BTSend.setOnClickListener(new View.OnClickListener() {

                                      public void onClick(View v) {
                                          ETURL = ETURLInput.getText().toString();

                                          new aSync().execute(ETURLInput.getText().toString());


                                      }


                                  }
        );

}

private class aSync extends AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {

    }

    protected String doInBackground(String... ETURL)   {
        String line=null;
        String result="";


            try {
                URL url;

                url = new URL("" + ETURL);
                URLConnection conn = url.openConnection();


                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                line=br.readLine();
                while (line!= null) {

                    result=result+line;
                    line=br.readLine();
                }

                br.close();

                return result;


            } catch (Exception e) {
               Log.e(TAG, "Error:", e);
            }


        return result;
    }

    protected void onPostExecute(String result)  {
        try {
            TextView TVCode = (TextView) findViewById(R.id.TVCode);
            TVCode.setText(result);
        }
        catch (Exception e){
            Log.e(TAG, "ERROR: ", e);
        }
    }
}

Fix:使固定:

The TextView must be declared in the onPreExecute method instead of the on onPostExecute method. TextView必须在onPreExecute方法而不是onPostExecute方法中声明。 Thank you all for your help and time.感谢大家的帮助和时间。

The problem lies in the onPostExecute() .问题在于onPostExecute() here you are trying to use findViewById() .在这里,您尝试使用findViewById() But there is no view to be found.但是没有找到视图。 Hence the issue.因此问题。 You need to call the findViewById() on the activity or the inflated layout.您需要在活动或膨胀的布局上调用findViewById() If it's an activity, you need to pass the activity context to the async task and in case of fragment, you could pass in the inflated view.如果是活动,则需要将活动上下文传递给异步任务,如果是片段,则可以传入膨胀视图。

More info here : Can't access "findViewById" in AsyncTask更多信息: 无法在 AsyncTask 中访问“findViewById”

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

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