如果我使用以下代码,我将没有错误,但会有冻结时间。
`@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。
问题可以由任何人指导我吗?谢谢
答案 0 :(得分:0)
我看到你使用tvProfilePic,但我看不到你在哪里定义它。
如果您没有定义它,它应该以与
类似的方式完成 tvProfilePic = findViewById(R.id.profile_pic)
答案 1 :(得分:0)
您正在使用tvProfilePic作为textview,但是我看不到您在哪里定义它。
如果您没有定义它,则使用
之类的东西来定义它
tvProfilePic = findViewById(R.id.profile_pic)
由于您的tvProfilePic为空,因此您将获得nullpointerexception