0

如果我使用下面的代码,我不会出错,但是会有一个冻结时间。

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy); 
    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 Auto-generated method stub
    super.onCreate(savedInstanceState);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy); 
    setContentView(R.layout.profilepic);
    initialize();
    getUsername();
}

private AsyncTask<String, Void, String> task;

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

我进入java.lang.NullPointerException文本视图。

有什么问题?谁能帮我解决这个问题NullPointerException

4

2 回答 2

0

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

如果你没有定义它,它应该以类似的方式完成

tvProfilePic = findViewById(R.id.profile_pic)

你在做这个吗?请注意,“profile_pic”是布局 XML 文档中的名称。

于 2013-01-15T21:21:21.887 回答
0

您正在使用 tvProfilePic 作为文本视图,但我看不到您在哪里定义它。如果你没有定义它,那么使用类似
tvProfilePic = findViewById(R.id.profile_pic) 的东西来定义它

您收到 nullpointerexception 因为您的 tvProfilePic 为空

于 2020-01-04T06:00:02.723 回答