2017-09-14 52 views
-1

這個片段是在ViewPager上,它做一個okhttp得到一個JSON響應。 JSON適應RecyclerView,當我點擊該recyclerview上的記錄時,它必須調用片段的putPasta()方法,但是當我執行此操作時,我得到NullPointerException。On適配器調用方法的片段

片段:

public class PersonalizzataTab extends Fragment{ 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    view = inflater.inflate(R.layout.personalizzata_layout, container, 

    PastaAdapter adapter = new PastaAdapter(getContext(), new PastaAdapter.AdapterInterface() { 
     @Override 
     public void putPasta() { 
      // the action called from adapter 
     } 
    }); 

    fab1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      SyncPasta syncPasta = new SyncPasta(); 
      syncPasta.execute(); 
     } 
    }); 

    return view; 
} 

class SyncPasta extends AsyncTask { 

    @Override 
    protected Object doInBackground(Object[] params) { 

     OkHttpClient client = new OkHttpClient(); 

     Request request = new Request.Builder() 
       .url("link of php file") 
       .get() 
       .addHeader("cache-control", "no-cache") 
       .addHeader("postman-token", "4681c54c-3bd9-3d92-42f6-d7f9a68b4044") 
       .build(); 

     try { 
      Response response = client.newCall(request).execute(); 
      setPasta(response.body().string()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 
} 

public void setPasta(final String aResult) { 
    getActivity().runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 


      final JSONArray[] jsonArray = {null}; 
      try { 
       jsonArray[0] = new JSONArray(aResult); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      Pasta pasta; 
      ArrayList<Pasta> arrayListPasta = new ArrayList<Pasta>(); 
      if (jsonArray[0] != null) { 
       for (int i = 0; i < jsonArray[0].length(); i++) { 

        JSONObject json = null; 
        try { 

         json = jsonArray[0].getJSONObject(i); 
         pasta = new Pasta(json.getString("pasta"), json.getDouble("prezzo"), json.getString("miniatura"), json.getString("miniature")); 
         arrayListPasta.add(pasta); 
         // Assign adapter to ListView 


         pastaAdapter = new PastaAdapter(getActivity(), arrayListPasta); 
         listViewBanner.setAdapter(pastaAdapter); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

       } 

      } 
     } 
    }); 
} 

適配器:

public class PastaAdapter extends RecyclerView.Adapter<PastaAdapter.MyViewHolder> { 

private Context mContext; 
private List<Pasta> listaPasta; 

AdapterInterface buttonListener; 

public PastaAdapter (Context context, AdapterInterface buttonListener) 
{ 
    this.mContext = context; 
    this.buttonListener = buttonListener; 
} 

public interface AdapterInterface { 
    void putPasta(); 
} 

public class MyViewHolder extends RecyclerView.ViewHolder { 
    public RelativeLayout layoutPasta; 


    public MyViewHolder(View view) { 
     super(view); 
     layoutPasta = (RelativeLayout) view.findViewById(R.id.layoutIngrediente); 
    } 
} 

public PastaAdapter(Context mContext, List<Pasta> listaPasta) { 
    this.mContext = mContext; 
    this.listaPasta = listaPasta; 
} 

@Override 
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_ingredienti_layout, parent, false); 
    return new MyViewHolder(itemView); 
} 

@Override 
public void onBindViewHolder(final MyViewHolder holder, int position) { 
    final Pasta pasta = listaPasta.get(position); 
    holder.layoutPasta.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
       buttonListener.putPasta(); 

      } 
     } 
    }); 

} 

@Override 
public int getItemCount() { 
    return listaPasta.size(); 
} 
} 

例外:

java.lang.NullPointerException: Attempt to invoke interface method 'void sgware.easyfood.PastaAdapter$AdapterInterface.buttonPressed()' on a null object reference 
at sgware.easyfood.PastaAdapter$1.onClick(PastaAdapter.java:97) 
at android.view.View.performClick(View.java:5226) 
at android.view.View$PerformClick.run(View.java:21266) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:168) 
at android.app.ActivityThread.main(ActivityThread.java:5845) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 
+0

你上哪兒去實現的接口調用? – SripadRaj

+0

是否有你在onCreateView中創建適配器和偵聽器而不是在onViewCreated中的原因? –

+0

您正在將適配器設置爲pastaAdapter = new PastaAdapter(getActivity(),arrayListPasta); listViewBanner.setAdapter(pastaAdapter);由於你的buttonListener在適配器是空的 –

回答

0

添加適配器構造那樣

public PastaAdapter(Context mContext, List<Pasta> listaPasta, AdapterInterface buttonListener) { 
     this.mContext = mContext; 
     this.listaPasta = listaPasta; 
    this.buttonListener=buttonListener; 
    } 

而在片段

pastaAdapter = new PastaAdapter(getActivity(), arrayListPasta, new PastaAdapter.AdapterInterface() { 
     @Override 
     public void putPasta() { 
      // the action called from adapter 
     } 
    }); 
    listViewBanner.setAdapter(pastaAdapter); 
+0

我這樣做,但它得到NullPointerException – GiacomoZ

+0

@GiacomoZ你是共享錯誤logcat你的接口不包含buttonPressed()方法 –

相關問題