1

편집 :이 pB = (ProgressBar) findViewById(R.id.progressBar);을 onPreExecute()에 넣습니다. 작동하고 모든 것이 작동합니다. 그러나 이것은 좋은 해결책입니까? 내 주 스레드에서 진행률 표시 줄이 있다는 것을 알고 있습니다! null. 그러나이 findViewById 전에, 내 asynctask 그냥 .execute() 그것을 전달하는 줄 알았는데, 원하는 진행률 표시 줄을 찾을 수 없습니다.onProgressUpdate()의 NullPointerException

이것은 asynctask에 대한 나의 첫 경험입니다. 셀 수있는 진행률 막대가 있지만 계속 NullPointerException 점점.

로그에 "진행 업데이트 : 1"이 표시되지만 VM이 종료됩니다. 그래서 정수가 전달되는 것을 알지만, 왜 그것이 진행 막대 (pB)를 찾을 수 없는지를 알 수 없습니다. onPreExecute()에서 주 스레드에서 setProgress (0) 시도했지만 컴퓨터가 그것을 싫어했다.

doInBackground()에서 for 루프의 나머지 부분을 실행하고 "진행률 :"및 "절전 모드"를 기록하지만 "진행률 업데이트 :"를 더 이상 기록하지 않습니다.

NullPointerException은 31 행에 있으며, pB.setProgress (progress [0]);

가 진행 표시 줄 초기화하려면 :

@Override 
protected void onProgressUpdate(Integer...progress){ 
    Log.d(LOG_TAG, "Progress Update: "+progress[0].toString()); 

    super.onProgressUpdate(progress); 
    if(progress[0]!=null){ 
    pB.setProgress(progress[0]); 
    } 
} 

@Override 
protected Boolean doInBackground(Integer...numSeconds){ 
    Log.d(LOG_TAG, "doInBackground: "+numSeconds[0]); 

    try { 
     int totalSecs = numSeconds[0].intValue(); 
     Log.d(LOG_TAG, "Total SECS: "+totalSecs); 

     for(int i = 1; i <= totalSecs; i++){ 
      Log.d(LOG_TAG, "Sleeping "+i); 
      Thread.sleep(1000); 

      float percentage = ((float)i/(float)totalSecs)*100; 
      Log.d(LOG_TAG, "Percentage Progress: "+ percentage); 

      Float progress = Float.valueOf(percentage); 
      publishProgress(new Float(progress).intValue()); 
     } 
    } catch (InterruptedException e){ 
     e.printStackTrace(); 
     return false; 
    } 
    return true; 
} 
@Override 
protected void onPostExecute(Boolean result){ 
    Log.d(LOG_TAG, "Post Execute "+ result); 

    super.onPostExecute(result); 

    pB.setProgress(0); 
} 

은 여기 내 주 스레드에서 일부의 타이머 = (ProgressBar의) findViewById를 (R.id.progressBar를); timer.setProgress (0);

은 AsyncTask를 실행하려면 :

OnClickListener startBubbles = new OnClickListener(){ 

    @Override 
    public void onClick(View arg0) { 
     new ProgBar(timer).execute(100); 
     setAll(); 
    }  
}; 

생성자 : 아직 initaialized되지되는 값을 액세스 할 때

public ProgBar(ProgressBar pB){ 
    Log.d(LOG_TAG, "Constructor"); 

} 
+7

야생입니다. pB가 null입니다! – Blackbelt

+0

나는 당신이 뭔가있는 것 같아. 하지만 null이 아니게하려면 어떻게해야합니까? 내 asyncTask 에서ViewById를 다시 찾아야합니까? 그것은 주 스레드에서 전달해야합니다, 그렇지? – user3067903

+0

@ user3067903 어디서나'pB'를 초기화했는지 확인하십시오. "주 스레드에서 전달되어야합니다." asynctask에 전달했는지 또는 asynctask에 activity 클래스의 내부 클래스가 있습니까? – Raghunandan

답변

0

널 포인터 예외가 발생합니다.

예. char a [10];

System.out.println (a [3]);

그래서 초기화하지 않고 코드 내에 변수가 있어야합니다. 나는 몇 초만 있지만 확실하지 않습니다.

죄송합니다. 내가 아는 전부입니다.