我至少丢失了3个小时才能找到解决方案而没有结果......你能告诉我问题是什么以及如何解决?
活动布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background2"
tools:context="com.example.try.Sigiture"
tools:ignore="MergeRootFrame" >
<fragment
android:name="com.example.try.PlaceholderFragment"
android:id="@+id/fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
片段布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/sign_field"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
tools:context="com.example.try.Sigiture$PlaceholderFragment" >
</RelativeLayout>
活动类:
import android.app.Activity;
import android.os.Bundle;
public class Sigiture extends Activity {
private PlaceholderFragment placeholderFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sigiture);
}
}
片段类:
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
public class PlaceholderFragment extends Fragment implements OnClickListener {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_sigiture, container, false);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("Fragmend", "Click");
}
}
这就是LogCat中的内容:
07-08 18:55:28.772: W/dalvikvm(872): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-08 18:55:28.852: E/AndroidRuntime(872): FATAL EXCEPTION: main
07-08 18:55:28.852: E/AndroidRuntime(872): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.try/com.example.try.Sigiture}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
07-08 18:55:28.852: E/AndroidRuntime(872): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-08 18:55:28.852: E/AndroidRuntime(872): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-08 18:55:28.852: E/AndroidRuntime(872): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-08 18:55:28.852: E/AndroidRuntime(872): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-08 18:55:28.852: E/AndroidRuntime(872): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 18:55:28.852: E/AndroidRuntime(872): at android.os.Looper.loop(Looper.java:137)
07-08 18:55:28.852: E/AndroidRuntime(872): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-08 18:55:28.852: E/AndroidRuntime(872): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 18:55:28.852: E/AndroidRuntime(872): at java.lang.reflect.Method.invoke(Method.java:511)
07-08 18:55:28.852: E/AndroidRuntime(872): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-08 18:55:28.852: E/AndroidRuntime(872): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-08 18:55:28.852: E/AndroidRuntime(872): at dalvik.system.NativeStart.main(Native Method)
07-08 18:55:28.852: E/AndroidRuntime(872): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
07-08 18:55:28.852: E/AndroidRuntime(872): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
07-08 18:55:28.852: E/AndroidRuntime(872): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
07-08 18:55:28.852: E/AndroidRuntime(872): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-08 18:55:28.852: E/AndroidRuntime(872): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-08 18:55:28.852: E/AndroidRuntime(872): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-08 18:55:28.852: E/AndroidRuntime(872): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
07-08 18:55:28.852: E/AndroidRuntime(872): at android.app.Activity.setContentView(Activity.java:1881)
07-08 18:55:28.852: E/AndroidRuntime(872): at com.example.try.Sigiture.onCreate(Sigiture.java:13)
07-08 18:55:28.852: E/AndroidRuntime(872): at android.app.Activity.performCreate(Activity.java:5104)
07-08 18:55:28.852: E/AndroidRuntime(872): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-08 18:55:28.852: E/AndroidRuntime(872): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-08 18:55:28.852: E/AndroidRuntime(872): ... 11 more
07-08 18:55:28.852: E/AndroidRuntime(872): Caused by: java.lang.ClassCastException: com.example.try.PlaceholderFragment cannot be cast to android.app.Fragment
07-08 18:55:28.852: E/AndroidRuntime(872): at android.app.Fragment.instantiate(Fragment.java:585)
07-08 18:55:28.852: E/AndroidRuntime(872): at android.app.Fragment.instantiate(Fragment.java:560)
07-08 18:55:28.852: E/AndroidRuntime(872): at android.app.Activity.onCreateView(Activity.java:4709)
07-08 18:55:28.852: E/AndroidRuntime(872): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
07-08 18:55:28.852: E/AndroidRuntime(872): ... 21 more
我找到了一些答案,但他们没有帮助。请给我你的支持。我是第一次处理片段,我知道它们很重要,我需要学习如何使用它们。
答案 0 :(得分:1)
从<android.support.v4.app.Fragment...
更改xml文件中的<Fragment...
可能只是做了招数
与使用PlaceHolderFragment
的{{1}}一样,在XML中使用来自v4 support
的直接片段,因此异常