我正在努力将这个片段添加到我的XML布局中。
在我的Activity XML布局中,我有以下内容:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical" >
<fragment
android:id="@+id/fragment_invitefriendsprogress"
android:name="tv.superawesome.club.android.ui.fragments.InviteFriendsProgressFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_invitefriendsprogress" />
</LinearLayout>
我收到一个奇怪的错误:
XML file line #205: Class is not a View android.support.v4.app.Fragment
第205行是:
<fragment
我错过了什么吗?
这里的参考是完整的XML布局代码:
<RelativeLayout
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:background="@drawable/actionbar" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="@drawable/small" />
<ImageView
android:id="@+id/star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:src="@drawable/starpink" />
<ImageView
android:id="@+id/man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/star"
android:layout_marginRight="25dp"
android:layout_toLeftOf="@+id/star"
android:src="@drawable/invitepink" />
</RelativeLayout>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/bar"
android:layout_alignParentTop="true"
android:background="@drawable/bg" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/inwrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="@drawable/bg"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="-10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="@color/white"
android:gravity="center|top"
android:orientation="vertical" >
<com.myapp.jimbob.android.ui.CustomTextView
android:id="@+id/inTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingLeft="18dp"
android:text="@string/intitle"
android:textSize="16sp" />
<com.myapp.jimbob.android.ui.CustomTextView
android:id="@+id/indescriptiob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:layout_marginTop="2dp"
android:text="@string/indescription"
android:textSize="12sp" />
<EditText
android:id="@+id/emailedittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:background="@color/white"
android:drawablePadding="10dp"
android:ems="10"
android:hint="@string/yoemail"
android:inputType="textEmailAddress"
android:padding="10dp"
android:textSize="12sp" >
<requestFocus />
</EditText>
<Button
android:id="@+id/addbutton"
style="@style/OButton"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:text="@string/addinvite"
android:textSize="14sp" />
<com.myapp.jimbob.android.ui.CustomTextView
android:id="@+id/mailsdescriptiom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:layout_marginTop="10dp"
android:text="@string/emailsofar"
android:textSize="12sp" />
<ScrollView
android:id="@+id/emailsscrollview"
android:layout_width="match_parent"
android:layout_height="92dp"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:layout_marginTop="8dp" >
<LinearLayout
android:id="@+id/emailslayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/light_gray"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/emailrow"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.myapp.jimbob.android.ui.CustomTextView
android:id="@+id/emailaddresstextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:paddingLeft="15dp"
android:text="PlaceHolder"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="12sp" />
<ImageView
android:id="@+id/cancelimageview"
android:layout_width="15dp"
android:layout_height="match_parent"
android:layout_weight="0.1"
android:src="@drawable/abs_clear" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/sendbutton"
style="@style/OButton"
android:layout_height="30dp"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:text="@string/mfriends"
android:textSize="14sp" />
<ProgressBar
android:id="@+id/friendindicator"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/inviteprogressbutton"
style="@style/OrangeSmallButton"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignRight="@+id/linearLayout1"
android:layout_below="@+id/linearLayout1"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="abc"
android:textSize="10sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical" >
<fragment
android:id="@+id/fragment_myfragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.myapp.jimbob.android.ui.fragments.MyFragment"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
<include layout="@layout/layout_toast" />
这里也是片段类:
public class MyFragment extends SherlockFragment {
private CustomTextView mTitle;
private CustomTextView mDescription;
private CustomTextView mStar5Description;
private CustomTextView mStar10Description;
private ImageView mStar1, mStar2, mStar3, mStar4, mStar5, mStar6, mStar7,
mStar8, mStar9, mStar10;
private Button mFriends5Button;
private Button mFriends10Button;
private Button mInviteFriendsButton;
private int inviteCount;
private View mRootView = null;
private ProgressBar mProgressBar;
private void setAlphaForView(View v, float alpha) {
AlphaAnimation animation = new AlphaAnimation(alpha, alpha);
animation.setDuration(0);
animation.setFillAfter(true);
v.startAnimation(animation);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (mRootView != null) {
ViewGroup parent = (ViewGroup) mRootView.getParent();
if (parent != null)
parent.removeView(mRootView);
}
try {
mRootView = inflater
.inflate(R.layout.fragment_invitefriendsprogress, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
mTitle = (CustomTextView) mRootView.findViewById(R.id.progressTitle);
mDescription = (CustomTextView) mRootView.findViewById(R.id.progressDescription);
mStar5Description = (CustomTextView) mRootView.findViewById(R.id.progress5starDescription);
mStar10Description = (CustomTextView) mRootView.findViewById(R.id.progress10StarDescription);
mStar1 = (ImageView) mRootView.findViewById(R.id.invitestar1);
mStar2 = (ImageView) mRootView.findViewById(R.id.invitestar2);
mStar3 = (ImageView) mRootView.findViewById(R.id.invitestar3);
mStar4 = (ImageView) mRootView.findViewById(R.id.invitestar4);
mStar5 = (ImageView) mRootView.findViewById(R.id.invitestar5);
mStar6 = (ImageView) mRootView.findViewById(R.id.invitestar6);
mStar7 = (ImageView) mRootView.findViewById(R.id.invitestar7);
mStar8 = (ImageView) mRootView.findViewById(R.id.invitestar8);
mStar9 = (ImageView) mRootView.findViewById(R.id.invitestar9);
mStar10 = (ImageView) mRootView.findViewById(R.id.invitestar10);
mFriends5Button = (Button) mRootView.findViewById(R.id.guarantee5button);
mFriends10Button = (Button) mRootView.findViewById(R.id.guarantee10button);
mProgressBar = (ProgressBar) mRootView.findViewById(R.id.invitesProgressBar);
inviteCount = BoxStatusCache.getInstance(mRootView.getContext().getApplicationContext()).inviteCount;
if (MyTheme.getTheme(mRootView.getContext()) == MyTheme.BOA) {
mRootView.findViewById(R.id.bglayout).setBackgroundResource(
R.drawable.bgawesome);
mTitle.setCustom(false);
mTitle.setCustom(true, CustomTextView.BOATITLEFONT);
mTitle.setTextColor(getResources().getColor(R.color.purple));
mDescription.setCustom(true, CustomTextView.BOADESCFONT);
mStar5Description.setCustom(true, CustomTextView.BOADESCFONT);
mStar10Description.setCustom(true, CustomTextView.BOADESCFONT);
mFriends5Button.setTypeface(CustomTypeface.getTypefaceBOADesc(mRootView.getContext().getApplicationContext()));
mFriends10Button.setTypeface(CustomTypeface.getTypefaceBOADesc(mRootView.getContext().getApplicationContext()));
} else {
mRootView.findViewById(R.id.bglayout).setBackgroundResource(
R.drawable.bg);
mTitle.setCustom(true);
mFriends5Button.setBackgroundResource(R.drawable.pinksmallbutton);
mFriends10Button.setBackgroundResource(R.drawable.pinksmallbutton);
mTitle.setCustom(true, CustomTextView.TITLEFONT);
mTitle.setTextColor(getResources().getColor(R.color.pink));
mDescription.setCustom(true, CustomTextView.DESCFONT);
mStar5Description.setCustom(true, CustomTextView.DESCFONT);
mStar10Description.setCustom(true, CustomTextView.DESCFONT);
mFriends5Button.setTypeface(CustomTypeface.getTypefaceDesc(mRootView.getContext().getApplicationContext()));
mFriends10Button.setTypeface(CustomTypeface.getTypefaceDesc(mRootView.getContext().getApplicationContext()));
}
mStar1.setImageResource(R.drawable.notinvited);
mStar2.setImageResource(R.drawable.notinvited);
mStar3.setImageResource(R.drawable.notinvited);
mStar4.setImageResource(R.drawable.notinvited);
mStar5.setImageResource(R.drawable.notinvited);
mStar6.setImageResource(R.drawable.notinvited);
mStar7.setImageResource(R.drawable.notinvited);
mStar8.setImageResource(R.drawable.notinvited);
mStar9.setImageResource(R.drawable.notinvited);
mStar10.setImageResource(R.drawable.notinvited);
// Uncomment this after finish debugging
if (inviteCount < 10) {
setAlphaForView(mFriends10Button, 0.5f);
mFriends10Button.setClickable(false);
if (inviteCount < 5) {
setAlphaForView(mFriends5Button, 0.5f);
mFriends5Button.setClickable(false);
}
}
final Context context = mRootView.getContext();
mFriends5Button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mProgressBar.setVisibility(View.VISIBLE);
setAlphaForView(mFriends5Button, 0.5f);
// TODO Auto-generated method stub
new AsyncTask<Void, Void, JSONObject>() {
@Override
protected JSONObject doInBackground(Void... params) {
try {
HashMap<Object, Object> data = new HashMap<Object, Object>();
if (MyTheme.getTheme(context) == MyTheme.TWO) {
data.put("site", 1);
} else {
data.put("site", 0);
}
data.put("invite_amount", 5);
LogUtils.log("redeem data: " + data.toString());
String response = HttpRequestController
.getInstance().doRequest(
ApiUrls.REDEEM_INVITATION, data,
Method.POST, MainActivity.token);
return new JSONObject(response);
} catch (Exception e) {
e.printStackTrace();
mProgressBar.setVisibility(View.INVISIBLE);
setAlphaForView(mFriends5Button, 1.0f);
return null;
}
}
@Override
protected void onPostExecute(JSONObject result) {
mProgressBar.setVisibility(View.INVISIBLE);
setAlphaForView(mFriends5Button, 1.0f);
try {
if (result.getBoolean("success")) {
makeGoodToastAndEatIt();
LogUtils.log("result MyActivity: "
+ result.toString());
getActivity().setResult(getActivity().RESULT_OK);
SyncUtils.forceUpdateProfile();
} else {
LogUtils.log("error_msg: " + result.toString());
if (result.getString("error_msg").contains(
"Not enough")) {
ToastAnimationUtils
.setUpAnimations(
getActivity(),
"Not enough invitations. Invite some more friends and try again!",
false);
} else {
ToastAnimationUtils
.setUpAnimations(
getActivity(),
"Try again later.",
false);
}
}
} catch (Exception e) {
e.printStackTrace();
ToastAnimationUtils
.setUpAnimations(
getActivity(),
"Try again later. ",
false);
}
}
}.execute();
}
});
mFriends10Button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mProgressBar.setVisibility(View.VISIBLE);
setAlphaForView(mFriends10Button, 0.5f);
// TODO Auto-generated method stub
new AsyncTask<Void, Void, JSONObject>() {
@Override
protected JSONObject doInBackground(Void... params) {
try {
HashMap<Object, Object> data = new HashMap<Object, Object>();
if (MyTheme.getTheme(context) == MyTheme.BOA) {
data.put("site", 1);
} else {
data.put("site", 0);
}
data.put("invite_amount", 10);
LogUtils.log("redeem data: " + data.toString());
String response = HttpRequestController
.getInstance().doRequest(
ApiUrls.REDEEM_INVITATION, data,
Method.POST, MainActivity.token);
return new JSONObject(response);
} catch (Exception e) {
e.printStackTrace();
mProgressBar.setVisibility(View.INVISIBLE);
setAlphaForView(mFriends10Button, 1.0f);
return null;
}
}
@Override
protected void onPostExecute(JSONObject result) {
mProgressBar.setVisibility(View.INVISIBLE);
setAlphaForView(mFriends10Button, 1.0f);
try {
if (result.getBoolean("success")) {
makeMegaGoodToastAndEatIt();
LogUtils.log("result MyActivity: "
+ result.toString());
getActivity().setResult(MainActivity.RESULT_OK);
SyncUtils.forceUpdateProfile();
} else {
if (result.getString("error_msg").contains(
"Not enough")) {
ToastAnimationUtils
.setUpAnimations(
getActivity(),
"Not enough invitations. Invite some more friends and try again!",
false);
} else {
ToastAnimationUtils
.setUpAnimations(
getActivity(),
" Try again later.",
false);
}
}
} catch (Exception e) {
e.printStackTrace();
ToastAnimationUtils
.setUpAnimations(
getActivity(),
"Try again later. ",
false);
}
}
}.execute();
}
});
LogUtils.log("inviteCount: " + inviteCount);
if (inviteCount > 0 && MyTheme.getTheme(getActivity()) == MyTheme.TWO) {
mStar1.setImageResource(R.drawable.invited);
if (inviteCount > 1) {
mStar2.setImageResource(R.drawable.invited);
if (inviteCount > 2) {
mStar3.setImageResource(R.drawable.invited);
if (inviteCount > 3) {
mStar4.setImageResource(R.drawable.invited);
if (inviteCount > 4) {
mStar5.setImageResource(R.drawable.invited);
if (inviteCount > 5) {
mStar6.setImageResource(R.drawable.invited);
if (inviteCount > 6) {
mStar7.setImageResource(R.drawable.invited);
if (inviteCount > 7) {
mStar8.setImageResource(R.drawable.invited);
if (inviteCount > 8) {
mStar9.setImageResource(R.drawable.invited);
if (inviteCount > 9) {
mStar10.setImageResource(R.drawable.invited);
}
}
}
}
}
}
}
}
}
} else if (inviteCount > 0
&& MyTheme.getTheme(getActivity()) == MyTheme.ONE) {
mStar1.setImageResource(R.drawable.invited);
if (inviteCount > 1) {
mStar2.setImageResource(R.drawable.invited);
if (inviteCount > 2) {
mStar3.setImageResource(R.drawable.invited);
if (inviteCount > 3) {
mStar4.setImageResource(R.drawable.invited);
if (inviteCount > 4) {
mStar5.setImageResource(R.drawable.invited);
if (inviteCount > 5) {
mStar6.setImageResource(R.drawable.invited);
if (inviteCount > 6) {
mStar7.setImageResource(R.drawable.invited);
if (inviteCount > 7) {
mStar8.setImageResource(R.drawable.invited);
if (inviteCount > 8) {
mStar9.setImageResource(R.drawable.invited);
if (inviteCount > 9) {
mStar10.setImageResource(R.drawable.invited);
}
}
}
}
}
}
}
}
}
}
return mRootView;
}
}
完整的logcat:
01-03 13:36:18.840: E/ACRA(6144): ACRA caught a RuntimeException exception for com.myapp.jimbob.android. Building report.
01-03 13:36:19.252: E/AndroidRuntime(6144): FATAL EXCEPTION: main
01-03 13:36:19.252: E/AndroidRuntime(6144): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.jimbob.android/com.myapp.jimbob.android.ui.activities.MyActivity}: android.view.InflateException: Binary XML file line #205: Error inflating class fragment
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.os.Looper.loop(Looper.java:137)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-03 13:36:19.252: E/AndroidRuntime(6144): at java.lang.reflect.Method.invokeNative(Native Method)
01-03 13:36:19.252: E/AndroidRuntime(6144): at java.lang.reflect.Method.invoke(Method.java:525)
01-03 13:36:19.252: E/AndroidRuntime(6144): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-03 13:36:19.252: E/AndroidRuntime(6144): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-03 13:36:19.252: E/AndroidRuntime(6144): at dalvik.system.NativeStart.main(Native Method)
01-03 13:36:19.252: E/AndroidRuntime(6144): Caused by: android.view.InflateException: Binary XML file line #205: Error inflating class fragment
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
01-03 13:36:19.252: E/AndroidRuntime(6144): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
01-03 13:36:19.252: E/AndroidRuntime(6144): at com.actionbarsherlock.internal.ActionBarSherlockNative.setContentView(ActionBarSherlockNative.java:133)
01-03 13:36:19.252: E/AndroidRuntime(6144): at com.actionbarsherlock.app.SherlockActivity.setContentView(SherlockActivity.java:229)
01-03 13:36:19.252: E/AndroidRuntime(6144): at com.myapp.jimbob.android.ui.activities.MyActivity.onCreate(MyActivity.java:75)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.app.Activity.performCreate(Activity.java:5133)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-03 13:36:19.252: E/AndroidRuntime(6144): ... 11 more
01-03 13:36:19.252: E/AndroidRuntime(6144): Caused by: java.lang.ClassCastException: com.myapp.jimbob.android.ui.fragments.MyFragment cannot be cast to android.app.Fragment
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.app.Fragment.instantiate(Fragment.java:585)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.app.Fragment.instantiate(Fragment.java:560)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.app.Activity.onCreateView(Activity.java:4738)
01-03 13:36:19.252: E/AndroidRuntime(6144): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
01-03 13:36:19.252: E/AndroidRuntime(6144): ... 25 more
答案 0 :(得分:8)
而不是......
<android.support.v4.app.Fragment
android:id="@+id/fragment_myfragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.myapp.jimbob.android.ui.fragments.MyFragment"
tools:layout="@layout/fragment_myfragment" />
写得像
<fragment
android:id="@+id/fragment_myfragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.myapp.jimbob.android.ui.fragments.MyFragment" />
您不能直接引用Fragment
的xml中的类名,因为它不会扩展View
答案 1 :(得分:3)
更改为
<fragment
android:id="@+id/fragment_myfragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.myapp.jimbob.android.ui.fragments.MyFragment"
/>
您的MyFragment
可以延长Fragment
,您可以使用SupportLibrary
。如果你想使用低于api 11级及以下的片段,请确保你有正确的导入并确保你的活动扩展FragmentActivity
。
更多信息阅读
http://developer.android.com/guide/topics/ui/declaring-layout.html
和
http://developer.android.com/guide/components/fragments.html