LayoutInflater异常

时间:2016-07-28 10:53:20

标签: java android exception layout-inflater fresco

这是交易,我有一个RecycleView适配器,可以在onCreateViewHolder上扩展布局。当我尝试执行以下操作时:

  1. 创建的应用程序没有任何问题
  2. 进入多任务窗口
  3. 回到应用
  4. InflateException
  5. Fresco的SimpleDraweeView发生错误,该错误在xml中定义。

    <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/photo"
            android:layout_width="match_parent"
            android:layout_height="@dimen/image_nobar"
            android:layout_alignParentEnd="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:adjustViewBounds="true"
            android:contentDescription="@string/photo"
            fresco:placeholderImage="@drawable/fry"
            fresco:actualImageScaleType="focusCrop" />
    

    这就是我将适配器设置为RecycleView

    的方法
    list = new ArrayList<>();
    gagInfo gi = new gagInfo();
    list.add(gi.setEmpty());
    recList.setAdapter(new gagAdapter(list, context));
    populateRecyclerView(recList);
    
    private void populateRecyclerView(final RecyclerView rView) {
    
        new Thread(new Runnable() {
            @Override
            public void run() {
                SharedPreferences prefs = getSharedPreferences(GAGS, MODE_PRIVATE);
    
                Map<String, ?> allEntries = prefs.getAll();
                list = new ArrayList<>();
                JSONObject obj;
                String keyObject;
                Boolean first = true;
                for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                    String entryValue = entry.getValue().toString();
                    keyObject = entry.getKey();
    
                    try {
                        JSONObject entryObject = new JSONObject(entryValue);
                        obj = entryObject.getJSONObject("nameValuePairs");
                        gagInfo gi = new gagInfo();
                        Log.d("JSON", "Processing... " + keyObject);
    
                        try {
                            gi.title = obj.getString("title");
                            gi.likes = obj.getString("likes");
                            gi.comments = obj.getString("comments");
                            gi.saved_date = obj.getString("saved_date");
                            gi.file_path = obj.getString("file_path");
                            gi.photoId = obj.getString("photoId");
    
                            list.add(gi);
    
                            if (first) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        ca = new gagAdapter(list, context);
                                        rView.setAdapter(ca);
                                    }
                                });
                                first = false;
                            } else {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        ca.notifyItemInserted(list.size() - 1);
                                    }
                                });
                            }
    
                        } catch (JSONException e) {
                            e.printStackTrace();
                            SharedPreferences.Editor editor = prefs.edit();
                            editor.remove(keyObject);
                            editor.apply();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                Collections.sort(list);
            }
        }).start();
    }
    

    我试图在我将Adapter设置为RecycleView的Activity中覆盖onResume(主要原因:从onCreate恢复以避免传递null适配器):

    @Override
        protected void onResume() {
            String action = getIntent().getAction();
            if (action == null || !action.equals("Created")) {
                Log.v("HCA", "Restart");
                Intent intent = new Intent(this, HomeCardActivity.class);
                startActivity(intent);
                finish();
            } else {
                getIntent().setAction(null);
            }
            super.onResume();
        }
    

    这就是我在适配器中扩展布局的方法。如果失败,我也尝试重新布局。

    @Override
        public gagViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(viewGroup.getContext());
            String card = prefs.getString("card_view", "0");
            Integer layoutID;
    
            if (card.equals("0")) {
                layoutID = R.layout.card_layout;
            } else {
                layoutID = R.layout.card_layout_nowhitebar;
            }
    
            View itemView;
            int count = 0;
            int maxTries = 5;
    
            do try { //TODO: Why LayoutInflater fails after third try?
                itemView = LayoutInflater.
                        from(viewGroup.getContext()).
                        inflate(layoutID, viewGroup, false);
                if (!(itemView == null)) break;
            } catch (InflateException ie) {
                Log.e("LayoutInflater", ie.toString());
                if (++count == maxTries) throw ie;
            } while (true);
    
            return new gagViewHolder(itemView);
        }
    

    logcat的

    android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class <unknown>
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
                                                                      at com.denizugur.ninegagsaver.gagAdapter.onCreateViewHolder(gagAdapter.java:134)
                                                                      at com.denizugur.ninegagsaver.gagAdapter.onCreateViewHolder(gagAdapter.java:25)
                                                                      at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5779)
                                                                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5003)
                                                                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4913)
                                                                      at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
                                                                      at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
                                                                      at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
                                                                      at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
                                                                      at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3260)
                                                                      at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3069)
                                                                      at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3518)
                                                                      at android.view.View.layout(View.java:17938)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5812)
                                                                      at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
                                                                      at android.view.View.layout(View.java:17938)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5812)
                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
                                                                      at android.view.View.layout(View.java:17938)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5812)
                                                                      at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
                                                                      at android.view.View.layout(View.java:17938)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5812)
                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
                                                                      at android.view.View.layout(View.java:17938)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5812)
                                                                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)
                                                                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                      at android.view.View.layout(View.java:17938)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5812)
                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
                                                                      at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:3193)
                                                                      at android.view.View.layout(View.java:17938)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5812)
                                                                      at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2666)
                                                                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2367)
                                                                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
                                                                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
                                                                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
                                                                      at android.view.Choreographer.doCallbacks(Choreographer.java:695)
                                                                      at android.view.Choreographer.doFrame(Choreographer.java:631)
                                                                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                      at android.os.Looper.loop(Looper.java:158)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                   Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class <unknown>
                                                                    at android.view.LayoutInflater.createView(LayoutInfla
    

1 个答案:

答案 0 :(得分:2)

通过Activity(this)或(getActivity())的Adapter构造函数传递上下文。看起来像重新加载时viewGroup上下文为空。