2016-03-07 93 views
0

E/RecyclerView:沒有附加適配器;跳過佈局Recyclerview片段

我試圖從我的本地主機數據庫獲取信息到使用recyclerview我的選項卡活動,我知道這個問題已經回答我只是想讓別人看到我的代碼時出現此錯誤。

public class Clients extends Fragment { 

    private static final String TAG = "RecyclerViewExample"; 
    final String url ="http://192.168.202.112:443/lawfirmdb/get_client.php"; 
    RecyclerView recyclerView; 
    List<FeedItem> feedsList; 
    MyRecyclerAdapter adapter; 
    View view; 

    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_CLIENT = "clients"; 
    private static final String TAG_FIRST_NAME = "first_name"; 
    private static final String TAG_LAST_NAME = "last_name"; 


    public Clients() { 

    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     feedsList = new ArrayList<>(); 
     // Inflate the layout for this fragment 
     view = inflater.inflate(R.layout.fragment_clients, container, false); 
     recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview); 
     recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
     downloadClients(); 
     return view; 

    } 

    public void downloadClients() { 
     new ClientRetriver().execute(url); 
    } 

    class ClientRetriver extends AsyncTask<String, Void, Integer> { 
     ProgressDialog progressDialog = new ProgressDialog(getActivity()); 

     @Override 
     protected void onPreExecute() { 
      progressDialog.setMessage("Please Wait..."); 
      progressDialog.setIndeterminate(false); 
      progressDialog.setCancelable(false); 
      progressDialog.show(); 
     } 

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

      Integer success = 0; 
      HttpURLConnection urlConnection; 

      try { 
       URL url = new URL(params[0]); 
       urlConnection = (HttpURLConnection) url.openConnection(); 
       urlConnection.setRequestMethod("GET"); 

       int statusCode = urlConnection.getResponseCode(); 

       // 200 represents HTTP OK 
       if (statusCode == 200) { 
        BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); 
        StringBuilder response = new StringBuilder(); 
        String line; 
        while ((line = r.readLine()) != null) { 
         response.append(line); 
        } 
        parseResult(response.toString()); 
        success = 1; // Successful 

       } else { 
        success = 0; //"Failed to fetch data!"; 
       } 
      } catch (Exception e) { 
       Log.d(TAG, e.getLocalizedMessage()); 
      } 

      return success; 
     } 


     @Override 
     protected void onPostExecute(Integer success) { 
      // Download complete. Let us update UI 

      progressDialog.dismiss(); 

      if (success == 1) { 


       adapter = new MyRecyclerAdapter(getActivity(), feedsList); 
       recyclerView.setAdapter(adapter); 
      } else { 
       Toast.makeText(getActivity(), "Failed to fetch data!", Toast.LENGTH_LONG).show(); 
      } 

     } 

     private void parseResult(String success) { 
      try { 
       JSONObject response = new JSONObject(success); 
       JSONArray clients = response.optJSONArray("clients"); 
       //feedsList = new ArrayList<>(); 

       for (int i = 0; i < clients.length(); i++) { 
        JSONObject post = clients.optJSONObject(i); 
        FeedItem item = new FeedItem(); 
        item.setFirst_name(post.optString("first_name")); 
        item.setLast_name(post.optString("last_name")); 

        feedsList.add(item); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 

    } 
} 

my adapter is: 

     public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.CustomViewHolder> { 
    private List<FeedItem> feedItemList; 
    private Context mContext; 

    public MyRecyclerAdapter(Context context, List<FeedItem> feedItemList) {enter code here 
     this.feedItemList = feedItemList; 
     this.mContext = context; 

    } 

    @Override 
    public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
     View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_view, null); 

     CustomViewHolder viewHolder = new CustomViewHolder(view); 
     return viewHolder; 
    } 


    @Override 
    public void onBindViewHolder(CustomViewHolder customViewHolder, int i) { 
     FeedItem feedItem = feedItemList.get(i); 

     //Download image using picasso library 
    /* Picasso.with(mContext).load(feedItem.getThumbnail()) 
       .error(R.drawable.placeholder) 
       .placeholder(R.drawable.placeholder) 
       .into(customViewHolder.imageView); 
*/ 
     //Setting text view title 
     customViewHolder.ClientName.setText(Html.fromHtml(feedItem.getFirst_name())); 
     customViewHolder.ClientNumber.setText(Html.fromHtml(feedItem.getLast_name())); 
    } 

    @Override 
    public int getItemCount() { 
     return (null != feedItemList ? feedItemList.size() : 0); 
    } 

    public class CustomViewHolder extends RecyclerView.ViewHolder{ 
     protected TextView ClientName,ClientNumber; 

     public CustomViewHolder(View view) { 
      super(view); 

      this.ClientName = (TextView) view.findViewById(R.id.client_name); 
      this.ClientNumber = (TextView) view.findViewById(R.id.client_number); 
     } 

    } 

這是我的logcat輸出

   03-07 10:21:53.397 1768-1768/com.example.dennism501.lawfirmofficeassistant I/art﹕ Late-enabling -Xcheck:jni 
03-07 10:21:53.609 1768-1813/com.example.dennism501.lawfirmofficeassistant D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true 
03-07 10:21:53.612 1768-1768/com.example.dennism501.lawfirmofficeassistant D/﹕ HostConnection::get() New Host Connection established 0xb42e7870, tid 1768 
03-07 10:21:53.629 1768-1768/com.example.dennism501.lawfirmofficeassistant D/Atlas﹕ Validating map... 
03-07 10:21:54.284 1768-1813/com.example.dennism501.lawfirmofficeassistant D/libEGL﹕ loaded /system/lib/egl/libEGL_emulation.so 
03-07 10:21:54.296 1768-1813/com.example.dennism501.lawfirmofficeassistant D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_emulation.so 
03-07 10:21:54.308 1768-1813/com.example.dennism501.lawfirmofficeassistant D/libEGL﹕ loaded /system/lib/egl/libGLESv2_emulation.so 
03-07 10:21:54.322 1768-1813/com.example.dennism501.lawfirmofficeassistant D/﹕ HostConnection::get() New Host Connection established 0xaf039480, tid 1813 
03-07 10:21:54.431 1768-1813/com.example.dennism501.lawfirmofficeassistant I/OpenGLRenderer﹕ Initialized EGL, version 1.4 
03-07 10:21:54.485 1768-1813/com.example.dennism501.lawfirmofficeassistant D/OpenGLRenderer﹕ Enabling debug mode 0 
03-07 10:21:54.499 1768-1813/com.example.dennism501.lawfirmofficeassistant W/EGL_emulation﹕ eglSurfaceAttrib not implemented 
03-07 10:21:54.499 1768-1813/com.example.dennism501.lawfirmofficeassistant W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xaf035780, error=EGL_SUCCESS 
03-07 10:21:54.516 1768-1768/com.example.dennism501.lawfirmofficeassistant I/Choreographer﹕ Skipped 46 frames! The application may be doing too much work on its main thread. 
03-07 10:21:55.618 1768-1768/com.example.dennism501.lawfirmofficeassistant I/Choreographer﹕ Skipped 65 frames! The application may be doing too much work on its main thread. 
03-07 10:22:01.554 1768-1768/com.example.dennism501.lawfirmofficeassistant E/RecyclerView﹕ No adapter attached; skipping layout 
03-07 10:22:01.698 1768-1813/com.example.dennism501.lawfirmofficeassistant W/EGL_emulation﹕ eglSurfaceAttrib not implemented 
03-07 10:22:01.698 1768-1813/com.example.dennism501.lawfirmofficeassistant W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xb436ddc0, error=EGL_SUCCESS 
03-07 10:22:02.343 1768-1813/com.example.dennism501.lawfirmofficeassistant W/EGL_emulation﹕ eglSurfaceAttrib not implemented 
03-07 10:22:02.343 1768-1813/com.example.dennism501.lawfirmofficeassistant W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xb436de00, error=EGL_SUCCESS 
03-07 10:22:02.373 1768-1813/com.example.dennism501.lawfirmofficeassistant V/RenderScript﹕ 0xb41a7000 Launching thread(s), CPUs 4 

這是我的新的logcat出的onCreate

 03-08 04:51:11.039 10429-10473/com.example.dennism501.lawfirmofficeassistant D/﹕ HostConnection::get() New Host Connection established 0xb439bd40, tid 10473 
03-08 04:51:11.056 10429-10473/com.example.dennism501.lawfirmofficeassistant I/OpenGLRenderer﹕ Initialized EGL, version 1.4 
03-08 04:51:11.077 10429-10473/com.example.dennism501.lawfirmofficeassistant D/OpenGLRenderer﹕ Enabling debug mode 0 
03-08 04:51:11.094 10429-10473/com.example.dennism501.lawfirmofficeassistant W/EGL_emulation﹕ eglSurfaceAttrib not implemented 
03-08 04:51:11.094 10429-10473/com.example.dennism501.lawfirmofficeassistant W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xb43e1120, error=EGL_SUCCESS 
03-08 04:51:11.351  753-779/system_process I/ActivityManager﹕ Displayed com.example.dennism501.lawfirmofficeassistant/.activities.MainActivity: +3s265ms 
03-08 04:51:11.650  753-824/system_process D/TaskPersister﹕ removeObsoleteFile: deleting file=152_task.xml 
03-08 04:51:18.306  753-811/system_process W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client 
03-08 04:51:18.329  753-1020/system_process V/WindowManager﹕ not Base app: Adding window Window{39534f2d u0 com.example.dennism501.lawfirmofficeassistant/com.example.dennism501.lawfirmofficeassistant.activities.MainActivity} at 3 of 8 
03-08 04:51:18.351 10429-10532/com.example.dennism501.lawfirmofficeassistant D/RecyclerViewExample﹕ Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference 
03-08 04:51:18.410 10429-10473/com.example.dennism501.lawfirmofficeassistant W/EGL_emulation﹕ eglSurfaceAttrib not implemented 
03-08 04:51:18.410 10429-10473/com.example.dennism501.lawfirmofficeassistant W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa2121380, error=EGL_SUCCESS 
03-08 04:51:18.626  753-1338/system_process W/InputMethodManagerService﹕ Window already focused, ignoring focus gain of: [email protected] attribute=null, token = [email protected] 
03-08 04:51:18.698 10429-10473/com.example.dennism501.lawfirmofficeassistant W/EGL_emulation﹕ eglSurfaceAttrib not implemented 
03-08 04:51:18.698 10429-10473/com.example.dennism501.lawfirmofficeassistant W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa2036480, error=EGL_SUCCESS 
03-08 04:51:18.787 10429-10473/com.example.dennism501.lawfirmofficeassistant V/RenderScript﹕ 0xb41ac400 Launching thread(s), CPUs 4 
+0

你從url獲得成功嗎? –

+0

不,但如果我運行php腳本它返回結果在瀏覽器 – droid501

+0

請上傳您的日誌文件 –

回答

0

您收到此錯誤添加空白適配器,因爲你的這種情況從來沒有setisfied後

if (success == 1) { 


       adapter = new MyRecyclerAdapter(getActivity(), feedsList); 
       recyclerView.setAdapter(adapter); 
      } 
+0

如何解決它?新手問題,我知道... – droid501

0

首先設置適配器,然後在加載後更新數據。

1.設置空適配器

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    feedsList = new ArrayList<>(); 
    // Inflate the layout for this fragment 
    view = inflater.inflate(R.layout.fragment_clients, container, false); 
    recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview); 
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
    recyclerView.setAdapter(new MyRecyclerAdapter(getActivity(), feedsList)); 
    downloadClients(); 
    return view; 

} 

2.更新適配器後執行

if (success == 1) { 
      adapter.updateFeedList(feedLists); 
     } else { 
      Toast.makeText(getActivity(), "Failed to fetch data!", Toast.LENGTH_LONG).show(); 
     } 

3.把這個方法您轉接器內

public void updateFeedList(List<feedItem> feedList){ 
    if(feedItemList==null) 
     feedItemList = new ArrayList<>(); 
    feedItemList.addAll(feedList); 
    notifyDataSetChanged(); 
} 

我希望它能爲你工作。

+0

謝謝讓我試試看 – droid501