0

다음 코드를 사용하면 오류는 없지만 정지 시간이 있습니다.AsyncTask 내의 텍스트 뷰에서 java.lang.NullPointerException이 발생했습니다.

`@Override 보호 공극에서 onCreate (번들 savedInstanceState) {// TODO 방법 스텁 super.onCreate (savedInstanceState) 자동 생성; StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder(). permitAll(). build(); StrictMode.setThreadPolicy (policy); setContentView (R.layout.profilepic); 나는이 같은 AsyncTask를하고해서 ProgressDialog이 코드를 사용하는 경우

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

    } 
}` 

는하지만 :

`@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

텍스트 뷰에 대해 아무 것도 보이지 않지만 코드를 표시하고 logcat을 보겠습니다. – Graknol

답변

0

나는 tvProfilePic을 사용하는 것을 본다. 그러나 나는 당신이 그것을 정의하는 곳을 보지 못했다.

경우

당신이 그것을 정의하지 않습니다, 그것은

tvProfilePic = findViewById(R.id.profile_pic)

당신이이 일을하고 있습니까와 유사한 방식으로 수행해야 하는가? 'profile_pic'은 레이아웃 XML 문서의 이름입니다.

관련 문제