我收到了此错误消息状态android.view.InflateException: Binary XML file line #9: Error inflating class fragment
。是因为我没有将其扩展到fragment
中的MessagesFragment
吗?顺便说一句,我的应用程序使用TabHost
。
FirstFragment.java
package com.xxxxx.yyyyy.match;
import com.xxxxx.yyyyy.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment {
public FirstFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = LayoutInflater.from(getActivity()).inflate(R.layout.first_fragment,
null);
return v;
}
}
first_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/darker_gray"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".FirstFragment" >
<fragment
android:name="com.xxxxx.yyyyy.chat.MessagesFragment"
android:id="@+id/msg_list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</RelativeLayout>
MessagesFragment.java
package com.xxxxx.yyyyy.chat;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.xxxxx.yyyyy.R;
import com.xxxxx.yyyyy.library.chat.DataProvider;
import android.app.Activity;
import android.app.ListFragment;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class MessagesFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final DateFormat[] df = new DateFormat[] {
DateFormat.getDateInstance(), DateFormat.getTimeInstance()};
private OnFragmentInteractionListener mListener;
private SimpleCursorAdapter adapter;
private Date now;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
now = new Date();
adapter = new SimpleCursorAdapter(getActivity(),
R.layout.chat_list_item,
null,
new String[]{DataProvider.COL_MSG, DataProvider.COL_AT},
new int[]{R.id.text1, R.id.text2},
0);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
switch(view.getId()) {
case R.id.text1:
LinearLayout root = (LinearLayout) view.getParent().getParent();
if (cursor.getString(cursor.getColumnIndex(DataProvider.COL_FROM)) == null) {
root.setGravity(Gravity.RIGHT);
root.setPadding(50, 10, 10, 10);
} else {
root.setGravity(Gravity.LEFT);
root.setPadding(10, 10, 50, 10);
}
break;
case R.id.text2:
TextView tv = (TextView) view;
tv.setText(getDisplayTime(cursor.getString(columnIndex)));
return true;
}
return false;
}
});
setListAdapter(adapter);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setDivider(null);
Bundle args = new Bundle();
args.putString(DataProvider.COL_EMAIL, mListener.getProfileEmail());
getLoaderManager().initLoader(0, args, this);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
public String getProfileEmail();
}
private String getDisplayTime(String datetime) {
try {
Date dt = sdf.parse(datetime);
if (now.getYear()==dt.getYear() && now.getMonth()==dt.getMonth() && now.getDate()==dt.getDate()) {
return df[1].format(dt);
}
return df[0].format(dt);
} catch (ParseException e) {
return datetime;
}
}
//----------------------------------------------------------------------------
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String profileEmail = args.getString(DataProvider.COL_EMAIL);
CursorLoader loader = new CursorLoader(getActivity(),
DataProvider.CONTENT_URI_MESSAGES,
null,
DataProvider.COL_FROM + " = ? or " + DataProvider.COL_TO + " = ?",
new String[]{profileEmail, profileEmail},
DataProvider.COL_AT + " DESC");
return loader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
}
logcat的输出
03-06 16:52:05.729: E/AndroidRuntime(5419): FATAL EXCEPTION: main
03-06 16:52:05.729: E/AndroidRuntime(5419): android.view.InflateException: Binary XML file line #9: Error inflating class fragment
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-06 16:52:05.729: E/AndroidRuntime(5419): at com.xxxxx.yyyyy.FirstFragment.onCreateView(FirstFragment.java:38)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.os.Handler.handleCallback(Handler.java:730)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.os.Handler.dispatchMessage(Handler.java:92)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.os.Looper.loop(Looper.java:137)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.app.ActivityThread.main(ActivityThread.java:5136)
03-06 16:52:05.729: E/AndroidRuntime(5419): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 16:52:05.729: E/AndroidRuntime(5419): at java.lang.reflect.Method.invoke(Method.java:525)
03-06 16:52:05.729: E/AndroidRuntime(5419): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-06 16:52:05.729: E/AndroidRuntime(5419): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-06 16:52:05.729: E/AndroidRuntime(5419): at dalvik.system.NativeStart.main(Native Method)
03-06 16:52:05.729: E/AndroidRuntime(5419): Caused by: java.lang.ClassCastException: com.xxxxx.yyyyy.HomeActivity@419d4fa8 must implement OnFragmentInteractionListener
03-06 16:52:05.729: E/AndroidRuntime(5419): at com.xxxxx.yyyyy.chat.MessagesFragment.onAttach(MessagesFragment.java:49)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:848)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1039)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1141)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.app.Activity.onCreateView(Activity.java:4746)
03-06 16:52:05.729: E/AndroidRuntime(5419): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
03-06 16:52:05.729: E/AndroidRuntime(5419): ... 20 more
请指教。谢谢。
答案 0 :(得分:1)
我在你的布局代码中看到了这一行:
android:layout_height="0dip"
android:layout_weight="1"
RelativeLayout
不支持weight
属性。 MessageFragment
的高度始终为0.因此,您应将layout_height
设置为match_parent
。
如果可能,也会发布错误的堆栈跟踪。
此外,您在FirstFragment中使用了支持库中的Fragment,但在MessageFragment中没有使用。它必须是一个需要修复的拼写错误,否则会在API小于11的设备上出错。
修改:您的HomeActivity
似乎没有实施OnFragmentInteractionListener
,这会在onAttach回调中抛出ClassCastException
。
答案 1 :(得分:0)
在setListAdapter(adapter);
onActivityCreated()
检查生命周期方法
http://developer.android.com/guide/components/fragments.html
在初始化片段之前onCreate()调用;
也改变了这个
View v = LayoutInflater.from(getActivity()).inflate(R.layout.first_fragment,
false);
答案 2 :(得分:0)
在MessagesFragment
onAttach
中,您的com.xxxxx.yyyyy.HomeActivity
将作为活动传入。
然后,在以下代码中,您希望HomeActivity
也是OnFragmentInteractionListener
mListener = (OnFragmentInteractionListener) activity;
要解决此问题,您需要制作HomeActivity
public class HomeActivity extends Activity implements OnFragmentInteractionListener
答案 3 :(得分:0)
不确定这是否能解决这个问题,但我有一个类似的问题,我无法相信错过它/修复是多么容易。
如果您检查xml文件,您将看到标记为:
<fragment
android:name="com.xxxxx.yyyyy.chat.MessagesFragment"
android:id="@+id/msg_list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
尝试简单地将'Fragment'中的'f'大写,以便XML Auto complete能够识别出你可能意味着(android.support.v4.app.Fragment)