如果我使用以下代码,我将没有错误,但会有一个冻结时间 .
`@Override protected void onCreate(Bundle savedInstanceState){// TODO自动生成的方法stub 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自动生成的方法stub 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 .
什么问题可以任何人指导我?谢谢
1 回答
我看到你使用tvProfilePic,但我没看到你在哪里定义它 .
如果你没有定义它,它应该以类似的方式完成
tvProfilePic = findViewById(R.id.profile_pic)
你在做这个吗?请注意,'profile_pic'是布局XML文档中的名称 .