2011-07-06 1 views
0

m非常にアンドロイドでこれは私の最初のクエリStackoverflowです。 ここでは、AsyncTaskの使用中にnullポインタ例外が発生しています。 私はProgressDialogを使用しています。これは、AsynchTaskを拡張する(別の(新規))クラスを作成したためです。私はProgressDialogを表示したい場所から私のメインクラスからクラスに引数を渡すとき、それは私にnullポインタ例外を提供します。.. いずれかは何をすべきかを私に伝えることができます。.. ここにコードどのように複数のクラスでAsyncTaskを使用するには?それはヌルポインタ例外を与えています

class Loader extends AsyncTask<Object, Object, Object> { 
    ProgressDialog dialog; 

    public void setDialog(ProgressDialog dialog){ 
     this.dialog = dialog; 
    //here i get the reference of dialog from the caller class 

} 

おかげであります事前に..方法の代わりに、setDialog

+0

を – Rasel

答えて

0

使用することは、おそらくこれはあなたのエラーが発生します。 AsyncTask内に新しいProgressDialogを作成し、onPreExecuteメソッドで表示することができます。このメソッドはUIスレッドで実行されているためです。お役に立てれば。

P.S.もしあなたがそれを持っていれば、あなたのエラーのlogcatを常に投稿してください。これは、人々が手助けしやすくなります。あなたの最初の投稿をおめでとうございます!

1

あなたAsyncTaskにパラメータとしてダイアログを送信する必要はありませんonPreExecute上書き Mahaveer

0
private class LocationControl extends AsyncTask<Context, Void, Void> 
{ 
    private final ProgressDialog dialog = new ProgressDialog(MainActivity.this); 
    protected void onPreExecute() 
    { 
     this.dialog.setMessage("Tap outside dialog to close"); 
     this.dialog.setTitle("Searching your current location..."); 
     //this.dialog.setTitle(R.string.hello); 
     this.dialog.setCanceledOnTouchOutside(true); 
     this.dialog.show(); 
    } 

    protected Void doInBackground(Context... params) 
    { 
     //Wait 40 seconds to see if we can get a location from either network or GPS, otherwise stop 
     Long t = Calendar.getInstance().getTimeInMillis(); 
     while (!hasLocation && Calendar.getInstance().getTimeInMillis() - t < 40000) { 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     }; 
     return null; 
    } 

    protected void onPostExecute(final Void unused) 
    { 
     if(this.dialog.isShowing()) 
     { 
      this.dialog.dismiss(); 
     } 

     if (currentLocation != null) 
     { 
      GeoPoint startPoint = new GeoPoint((int)(currentLocation.getLatitude()*1E6),(int)(currentLocation.getLongitude()*1E6)); 
      final MapController mc = mapView.getController(); 
      mc.animateTo(startPoint); 
      mc.setZoom(16); 
     } 
     else 
     { 
      /* 
      Toast.makeText(MainActivity.this, "Location could not be found\nusing default location", Toast.LENGTH_SHORT).show(); 
      GeoPoint startPoint = new GeoPoint((int)(43.332628*1E6),(int)(5.353128*1E6));   
      final MapController mc = mapView.getController(); 
      mc.animateTo(startPoint); 
      mc.setZoom(16); 
      */ 
     } 
    } 
} 

そして、あなたのonCreate()から実行する呼び出し:あなたは内部クラスとしてローダを使用してい

locationControlTask = new LocationControl(); 
locationControlTask.execute(this); 
関連する問題