2016-04-22 16 views
0

私のプロジェクトには、データの取得やデータの入力などのサーバー操作のみを行うクラスがあります。私は、リストを作成し、このリストを取得するメソッドを設定するクラスを持っています。問題は私が "getList"メソッドを呼び出して、バックグラウンド演算がhasentを終了した後に "getList"メソッドからヌルを取得したことです。doInBackgroundメソッドの終了後にAsyncTaskクラスオブジェクトからarraylistを取得します。

これは "getList"を見ることができるので、私のAsyncTaskクラスです私のリストは

public class GetRoomatesListActivity extends AsyncTask<String, String, String> { 
    private ArrayList<RoomateModel> tmpList; 
    private ArrayList<RoomateModel> completeList; 
    DBHelper db; 
    Context context; 

    public GetRoomatesListActivity(Context context){ 
     this.context = context; 
    } 

    @Override 
    protected String doInBackground(String... params) { 

     db = DBHelper.getInstance(context); 
     HttpURLConnection connection = null; 
     BufferedReader reader = null; 
     tmpList = new ArrayList<RoomateModel>(); 
     try { 
      URL url = new URL(params[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.connect(); 

      InputStream stream = connection.getInputStream(); 

      reader = new BufferedReader(new InputStreamReader(stream)); 
      StringBuffer buffer = new StringBuffer(); 

      String line = ""; 

      while ((line = reader.readLine()) != null) { 
       buffer.append(line); 

      } 
      String finalJson = buffer.toString(); 
      JSONObject parentObject = new JSONObject(finalJson); 
      JSONArray parentArray = parentObject.getJSONArray("result");//creates array of Roomates of the selected apartment 


      for (int i = 0; i < parentArray.length(); i++) { 
       JSONObject finalObject = parentArray.getJSONObject(i);//get the cuttent json object which is representaion of roomate object 

       String Fname = finalObject.getString("firstName"); 
       String Lname = finalObject.getString("lastName"); 
       String phone = finalObject.getString("phone"); 

       RoomateModel item = new RoomateModel(Fname, Lname, phone);//creates roomates model with the current item data from the array 
       tmpList.add(item);//adds the roomate to the list of roomates 

       //add the roomates to local data base 

       db.addRoomate(item,apartment); 
      } 

      return null; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } finally { 
      if (connection != null) { 
       connection.disconnect(); 
      } 
      try { 
       if (reader != null) { 
        reader.close(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 
     completeList = tmpList; 

    } 
    public ArrayList<RoomateModel> getList(){ 
     return completeList; 
    } 

} 

を完了し、これが最善の方法は、UI上で更新を実行することで、それを使用するためには、リストを取得しようとイムクラスが、その検索ヌル

public class roomatesScreen extends Activity { 
    ListView items; 
    ArrayList<RoomateModel> list; //list to compare with the list rerived from GetRoomatesListActivity 
    RoomatesAdapter adapter; 
    DBHelper db; 
    ApartmentModel apartment; 
    SharedPreferences preferences; 
    GetRoomatesListActivity r; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.roomates_list); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    items = (ListView) findViewById(R.id.roomatesList); 
    db = DBHelper.getInstance(this); 

    Bundle bundle = getIntent().getExtras(); 
    int number = bundle.getInt("number"); 
    apartment = new ApartmentModel(number);// creates apartment model with the user's apartment number 
    final String num = Integer.toString(number); 
    r = new GetRoomatesListActivity(this); 
    r.execute("this is the link to my query" + num); 
    list = r.getList(); //here list is null 
    adapter = new RoomatesAdapter(roomatesScreen.this, list); 
    items.setAdapter(adapter);//here application crushes because of nullPointerExpeption 

答えて

2

ですAsyncTaskのPostExecuteメソッドにあります。コントロールにアクセスしているときは、コントロールはdoInBackgroundメソッド内にあります。だからあなたのリストはその時nullです。 onPostExecute()にこの

@Override 
protected void onPostExecute(String s) { 
    super.onPostExecute(s); 

    adapter = new RoomatesAdapter(roomatesScreen.this, tmpList); 
    items.setAdapter(adapter);/ 

} 

を置きます。

第二ソリューション

あなたはそれがadaptorに設定されているリストを初期化します。以下のような:onPostExecute()

list = new ArrayList(); 

、残りの作業(リストを更新し、アダプタオブジェクトにnotifyDataSetChanged()を呼び出します)。

+0

私は「内「getRoomatesListActivity」内部クラスを行うならば、アダプタやアイテムのリストは、「RoomatesScreen」クラスで定義されていますRoomatesScreen "クラスで動作します。しかし、私は – ATT

+0

そのような場合はAsyncTaskのコンストラクタを作成し、コンテキストを更新してリストをリフレッシュするためのその他の情報を渡すことができます。 –

0

変更doInBackground()メソッドの戻り値の型

public class GetRoomatesListActivity extends AsyncTask<String, String, ArrayList<Object>> { 
private ArrayList<RoomateModel> tmpList; 
private ArrayList<RoomateModel> completeList; 
DBHelper db; 
Context context; 

public GetRoomatesListActivity(Context context){ 
    this.context = context; 
} 

@Override 
protected ArrayList<Object> doInBackground(String... params) { 

    db = DBHelper.getInstance(context); 
    HttpURLConnection connection = null; 
    BufferedReader reader = null; 
    tmpList = new ArrayList<RoomateModel>(); 
    try { 
     URL url = new URL(params[0]); 
     connection = (HttpURLConnection) url.openConnection(); 
     connection.connect(); 

     InputStream stream = connection.getInputStream(); 

     reader = new BufferedReader(new InputStreamReader(stream)); 
     StringBuffer buffer = new StringBuffer(); 

     String line = ""; 

     while ((line = reader.readLine()) != null) { 
      buffer.append(line); 

     } 
     String finalJson = buffer.toString(); 
     JSONObject parentObject = new JSONObject(finalJson); 
     JSONArray parentArray = parentObject.getJSONArray("result");//creates array of Roomates of the selected apartment 


     for (int i = 0; i < parentArray.length(); i++) { 
      JSONObject finalObject = parentArray.getJSONObject(i);//get the cuttent json object which is representaion of roomate object 

      String Fname = finalObject.getString("firstName"); 
      String Lname = finalObject.getString("lastName"); 
      String phone = finalObject.getString("phone"); 

      RoomateModel item = new RoomateModel(Fname, Lname, phone);//creates roomates model with the current item data from the array 
      tmpList.add(item);//adds the roomate to the list of roomates 

      //add the roomates to local data base 

      db.addRoomate(item,apartment); 
     } 

     return null; 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } finally { 
     if (connection != null) { 
      connection.disconnect(); 
     } 
     try { 
      if (reader != null) { 
       reader.close(); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return null; 
} 

@Override 
protected void onPostExecute(String s) { 
    super.onPostExecute(s); 
    completeList = tmpList; 

} 
public ArrayList<RoomateModel> getList(){ 
    return completeList; 
} 

}

関連する問題