2017-08-25 3 views
1

okhttpによってサーバーからJsonデータを取得しました。カスタムアダプタを使用してフラグメント内の recycleviewに表示したい場合は、次のエラーが表示されます。 。私はこのコードを実行するとokhttpとAsynctaskのRecyclerViewにデータを置くにはどうすればいいですか?

、logcatは私にfolloweingのMSGを伝える:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference 

私はlogcatに リストを印刷し、それがnullを示しています。 しかし、私はIEからいくつかのjsonデータを得ることができます。誰も助けてくれませんか?

public class customer extends AppCompatActivity { 

private static final String TAG = "MyDebug______ " ; 
private RecyclerView cutomerRecyclerView; 
private customerAdapter mAdapter; 
private ArrayList<custModal> list; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_customer); 
    Toolbar toolbar = (Toolbar)findViewById(R.id.custom_toolbar); 
    setSupportActionBar(toolbar); 

    new getHttpData().execute(); 
    Log.d(TAG, "the list is :" +list); 
    mAdapter = new customerAdapter(this,list); 
    cutomerRecyclerView = (RecyclerView)findViewById(R.id.customer_list); 
    cutomerRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 
    cutomerRecyclerView.setAdapter(mAdapter); 
    cutomerRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration(){ 
     @Override 
     public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state){ 
      super.onDraw(c,parent,state); 
      c.drawColor(R.color.colorDevider); 
     } 
     @Override 
     public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { 
      super.onDrawOver(c, parent, state); 
     } 
     @Override 
     public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 
      super.getItemOffsets(outRect, view, parent, state); 

      outRect.offset(0,1); 
     } 
    }); 
} 


public class customerAdapter extends RecyclerView.Adapter<customerAdapter.customerViewHolder>{ 
    private Context mcontext; 
    private ArrayList<custModal> mlist; 
    // private LayoutInflater inflater; 
    class customerViewHolder extends RecyclerView.ViewHolder{ 
     private TextView name,mem,cust_id; 

     public customerViewHolder(View view){ 
      super(view); 
      cust_id = (TextView)view.findViewById(R.id.cust_id); 
      name = (TextView)view.findViewById(R.id.cust_name); 
      mem = (TextView)view.findViewById(R.id.cust_mem); 
     } 
    } 
    public customerAdapter(Context context,ArrayList<custModal> list){ 
     this.mcontext = context; 
     this.mlist = list; 
    } 
    @Override 
    public customerViewHolder onCreateViewHolder(ViewGroup parent,int viewType){ 
     customerViewHolder holder = new customerViewHolder(LayoutInflater.from(
       customer.this).inflate(R.layout.customer_item,parent,false)); 
     return holder; 
    } 
    @Override 
    public void onBindViewHolder(customerViewHolder holder,int position){ 
     holder.cust_id.setText(mlist.get(position).getId()); 
     holder.name.setText(mlist.get(position).getName()); 
     holder.mem.setText(mlist.get(position).getMobiphone()); 
    } 
    @Override 
    public int getItemCount(){ return mlist.size();} 


} 

クラスcustModal {

private String id; 
    private String name; 
    private String gender; 
    private String mobiphone; 
    private String creat_time; 
    public void setId(String id) { 
     this.id = id; 
    } 
    public String getId() { 
     return id; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 
    public String getName() { 
     return name; 
    } 

    public void setGender(String gender) { 
     this.gender = gender; 
    } 
    public String getGender() { 
     return gender; 
    } 

    public void setMobiphone(String mobiphone) { 
     this.mobiphone = mobiphone; 
    } 
    public String getMobiphone() { 
     return mobiphone; 
    } 

    public void setCreat_time(String creat_time) { 
     this.creat_time = creat_time; 
    } 
    public String getCreat_time() { 
     return creat_time; 
    } 

} 



public class getHttpData extends AsyncTask<String,Integer,ArrayList<custModal> >{ 
    //private ArrayList<custModal> custlist; 

    @Override 

    // protected void onPreExecute(){} 

    @Override 

    protected ArrayList<custModal> doInBackground(String... params){ 
     String url = "http://aaaa.bbbbb.cn/index.php/mobi/customer/app_getlist"; 

     try{ 
      OkHttpClient client = new OkHttpClient(); 
      Request request = new Request.Builder() 
        .url(url) 
        .build(); 
      Response response = client.newCall(request).execute(); 
      String responseData = response.body().string(); 
      Log.d(TAG, "doInBackground: "+ responseData); 
      if(responseData != null){ 
       //return getCustFromJson(responseData); 
       Gson gson = new Gson(); 
       ArrayList<custModal> custlist = gson.fromJson(responseData, 
         new TypeToken<List<custModal>>() {}.getType() 
       ); 

       return custlist; 
      }else{ 
       return null; 
      } 
     }catch (IOException I){ 
      I.printStackTrace(); 
     } 

     return null; 
    } 
    @Override 

    protected void onPostExecute(ArrayList<custModal> custlist){ 
     if(!custlist.isEmpty()){ 
      super.onPostExecute(custlist); 
      list = custlist; 
      mAdapter.notifyDataSetChanged(); 
     } 
    } 

} 

}

+1

あなたは 'customerAdapter()'メソッドを呼び出していないか、 'list'引数に' null'を渡しています。このメソッドにブレークポイントを設定して、何が起きているのかを確認します。 – theFunkyEngineer

+0

あなたは正しく応答していますか? – DKV

+0

は、結果を取得した後にのみデータをバインドする必要があります。 – DKV

答えて

0

makeは必ずアダプター、アダプターの配列リストを初期化し、にアダプタを取り付けますrecycler in onCreate その後、httpを呼び出す必要がある場所で新しいyourAsyncTask()を実行する必要があります。 onBackgroundで配列リストに項目を追加し、postExecuteに通知するだけで通知データ変更メソッド

0

あなたは、アダプタのコンストラクタを介してリスト・リターン・ヌルのため、すべての操作を渡したときにあなたのリストがヌル状態です。

あなたのokhttpリクエストにコールバックを追加し、レスポンスでアダプタを構築できますか?いずれかの操作を実行する前にリストを更新する必要がありますが、どちらか一方の方法で実行してください。

0

私は方法onPostExecuteに次のコードを入れて、それは私はそれがこのsolution.like権利はないと思うworked.But:

 protected void onPostExecute(ArrayList<custModal> custlist){ 
     Log.d(TAG, "onPostExecute(Result result) called"); 
     Log.d(TAG, "onPostExecute: "+custlist); 
     mAdapter = new customerAdapter(customer.this,custlist); 
     cutomerRecyclerView = (RecyclerView)findViewById(R.id.customer_list); 
     cutomerRecyclerView.setLayoutManager(new LinearLayoutManager(customer.this)); 
     cutomerRecyclerView.setAdapter(mAdapter); 

    } 
+0

syncTaskを実装する一般的な関数をコーディングするにはどうしたらいいですか?申し訳ありませんが、私はAndroid上で新鮮な手です。 –

+0

あなたはただ非同期タスクを呼び出す必要がありますか、それともボタンをクリックすると呼び出されますか? –

+0

他のメソッドで呼び出すことができます。 –

関連する問題