LayoutInflater 异常
LayoutInflater Exception
这是交易,我有一个 RecycleView 适配器,它可以在 onCreateViewHolder
上扩展布局。当我尝试执行以下操作以发生这种情况时:
- 应用创建没有任何问题
- 开始处理多项任务 window
- 回到应用程序
- InflateException
在 xml 中定义的 Fresco SimpleDraweeView 发生错误。
<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();
}
我试图覆盖 Activity 中的 onResume
,我将适配器设置为 RecycleView(主要原因:从 onCreate
恢复以避免传递空适配器):
@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
通过 Adapter 构造函数从 Activity (this) 或 (getActivity()) 传递上下文。似乎重新加载时 viewGroup 上下文为空。
这是交易,我有一个 RecycleView 适配器,它可以在 onCreateViewHolder
上扩展布局。当我尝试执行以下操作以发生这种情况时:
- 应用创建没有任何问题
- 开始处理多项任务 window
- 回到应用程序
- InflateException
在 xml 中定义的 Fresco SimpleDraweeView 发生错误。
<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();
}
我试图覆盖 Activity 中的 onResume
,我将适配器设置为 RecycleView(主要原因:从 onCreate
恢复以避免传递空适配器):
@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
通过 Adapter 构造函数从 Activity (this) 或 (getActivity()) 传递上下文。似乎重新加载时 viewGroup 上下文为空。