0

如果我使用下面的代碼,我將沒有錯誤,但會有凍結時間。在textview中的java.lang.NullPointerException asyncTask

`@Override 保護無效的onCreate(捆綁savedInstanceState){// TODO自動生成方法存根 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 保護無效的onCreate(捆綁savedInstanceState){// TODO自動生成方法存根 super.onCreate(savedInstanceState); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()。permitAll()。build(); StrictMode.setThreadPolicy(policy); 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

我沒有看到關於textview的任何內容,顯示代碼和logcat。 – Graknol

回答

0

我看到你使用tvProfilePic,但我沒有看到你在哪裏定義它。

如果你不定義它,它應該以類似的方式完成

tvProfilePic = findViewById(R.id.profile_pic)

要這樣? 請注意,'profile_pic'是佈局XML文檔中的名稱。

相關問題