在asyncTask中的textview中的java.lang.NullPointerException

时间:2013-01-10 17:48:07

标签: android-asynctask nullpointerexception progressdialog textview android

如果我使用以下代码,我将没有错误,但会有冻结时间。

  

`@Override       protected void onCreate(Bundle savedInstanceState){           // TODO自动生成的方法存根           super.onCreate(savedInstanceState);           StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()。permitAll()。build();           StrictMode.setThreadPolicy(政策);           的setContentView(R.layout.profilepic);

    initialize();

    Bundle bundle = getIntent().getExtras();
    email = bundle.getString("Email");

    ArrayList<NameValuePair> postParameters;
    String response = null;

    postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("emaillog", email));              

    try {
        response = CustomHttpClient.executeHttpPost("http://whatstherex.info/checkU.php", postParameters);

        res = response.toString();

        res = res.replaceAll("null", "");                   

        username = res.toString();

        tvProfilePic.setText("Hi " + username + ", you are encourage to add a profile picture.");

    }catch(Exception e){
        res = e.toString();
        tvProfilePic.setText(res);

    }
}`

但是,如果我使用asynctask和progressDialog这样的代码:

  

`@Override       protected void onCreate(Bundle savedInstanceState){           // TODO自动生成的方法存根           super.onCreate(savedInstanceState);           StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()。permitAll()。build();           StrictMode.setThreadPolicy(政策);           的setContentView(R.layout.profilepic);

    initialize();

    getUsername();

}

私有AsyncTask任务;

public void getUsername(){      
    Bundle bundle = getIntent().getExtras();
    email = bundle.getString("Email");

    task = new AsyncTask<String, Void, String>() {

        ProgressDialog dialog;
        ArrayList<NameValuePair> postParameters;
        String response = null;

        @Override
        protected void onPreExecute() {
            //
            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("emaillog", email));

            dialog = new ProgressDialog(Profilepic.this, ProgressDialog.STYLE_SPINNER);
            dialog.setMessage("Loading Data...");       

            dialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            //
            try {
                response = CustomHttpClient.executeHttpPost("http://whatstherex.info/checkU.php", postParameters);

                res = response.toString();

                res = res.replaceAll("null", "");   

                username = res.toString();

                return username;
            }catch(Exception e){
                res = e.toString();
                return res;
            }
        }

        @Override
        protected void onPostExecute(String result) {               
            if(result.length()< 25){
                username = result;
                tvProfilePic.setText("Hi " + result + ", you are encourage to add a profile picture.");
                dialog.dismiss();
            }else{
                tvProfilePic.setText(result);
                dialog.dismiss();
            }
        }
    };
    task.execute(); 
}`

然后我会在textview中获得java.lang.NullPointerException。

问题可以由任何人指导我吗?谢谢

2 个答案:

答案 0 :(得分:0)

我看到你使用tvProfilePic,但我看不到你在哪里定义它。

如果您没有定义它,它应该以与

类似的方式完成

tvProfilePic = findViewById(R.id.profile_pic)

你正在这样做吗? 请注意,'profile_pic'是布局XML文档中的名称。

答案 1 :(得分:0)

您正在使用tvProfilePic作为textview,但是我看不到您在哪里定义它。 如果您没有定义它,则使用
之类的东西来定义它 tvProfilePic = findViewById(R.id.profile_pic)

由于您的tvProfilePic为空,因此您将获得nullpointerexception