我正在为常用的 UI 元素使用片段。实现了一个简单的 POC 来检查并且它的工作很酷。但是当我整合到我的主要项目时,它会给出运行时错误(崩溃)
这是我的 Androidmainfiest.xml
<activity
android:name="com.draw.map.TestActivity"
android:label="@string/title_activity_test" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
我的片段 xml:fragment_text_view_bottom_slider.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/bottom_slider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<RelativeLayout
android:id="@+id/register_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible" >
<RelativeLayout
android:id="@+id/register_inner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/slider_button"
android:background="#ECEDF2"
android:onClick="goToProfile"
android:visibility="visible" >
<ImageView
android:id="@+id/dummy_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goToProfile"
android:src="@drawable/dp" />
<RelativeLayout
android:id="@+id/text_block"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="2dp"
android:layout_toRightOf="@+id/dummy_image" >
<TextView
android:id="@+id/register_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bottom_slider_register1" />
<TextView
android:id="@+id/register_text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/register_text1"
android:text="@string/bottom_slider_register2" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rlSlideAlert"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/slider_button"
android:background="#ECEDF2"
android:visibility="gone" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#50555C"
android:gravity="right" >
<TextView
android:id="@+id/bottom_slider_alert_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="left"
android:text="WEH is totally blocked"
android:textAlignment="textStart"
android:textColor="#FFFFFF"
android:textSize="20dp" />
<ImageView
android:id="@+id/slider_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="slideButtonOnClick"
android:src="@drawable/alert_1" />
</LinearLayout>
</RelativeLayout>
<ImageView
android:id="@+id/slider_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="slideButtonOnClick"
android:src="@drawable/menu_button" />
</RelativeLayout>
<LinearLayout
android:id="@+id/menu_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/register_main"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:visibility="gone" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goToProfile"
android:src="@drawable/dp" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="reportOnClick"
android:src="@drawable/report" />
<ImageView
android:id="@+id/route_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/route_tapover" />
<ImageView
android:id="@+id/more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/more" />
<ImageView
android:id="@+id/other_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="closeBottomMenu"
android:src="@drawable/close_button" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
我的片段 java 文件:TextViewBottomSlider.java
public class TextViewBottomSlider extends Fragment{
View view;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_text_view_bottom_slider,
container, false);
// intilizeUIElements();
return view;
}
}
我的活动 xml:activity_test.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<fragment
android:id="@+id/bottom_slider_fragment"
android:name="com.draw.map.TextViewBottomSlider"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
tools:layout="@layout/fragment_text_view_bottom_slider" />
</RelativeLayout>
我的活动文件:TestActivity.java
public class TestActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
}
我的日志猫:
08-24 06:05:12.714: E/AndroidRuntime(5355): FATAL EXCEPTION: main
08-24 06:05:12.714: E/AndroidRuntime(5355): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.draw.map/com.draw.map.TestActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
08-24 06:05:12.714: E/AndroidRuntime(5355): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-24 06:05:12.714: E/AndroidRuntime(5355): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-24 06:05:12.714: E/AndroidRuntime(5355): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-24 06:05:12.714: E/AndroidRuntime(5355): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-24 06:05:12.714: E/AndroidRuntime(5355): at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 06:05:12.714: E/AndroidRuntime(5355): at android.os.Looper.loop(Looper.java:123)
08-24 06:05:12.714: E/AndroidRuntime(5355): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-24 06:05:12.714: E/AndroidRuntime(5355): at java.lang.reflect.Method.invokeNative(Native Method)
08-24 06:05:12.714: E/AndroidRuntime(5355): at java.lang.reflect.Method.invoke(Method.java:521)
08-24 06:05:12.714: E/AndroidRuntime(5355): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
08-24 06:05:12.714: E/AndroidRuntime(5355): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
08-24 06:05:12.714: E/AndroidRuntime(5355): at dalvik.system.NativeStart.main(Native Method)
08-24 06:05:12.714: E/AndroidRuntime(5355): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
08-24 06:05:12.714: E/AndroidRuntime(5355): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
08-24 06:05:12.714: E/AndroidRuntime(5355): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
08-24 06:05:12.714: E/AndroidRuntime(5355): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
08-24 06:05:12.714: E/AndroidRuntime(5355): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
08-24 06:05:12.714: E/AndroidRuntime(5355): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
08-24 06:05:12.714: E/AndroidRuntime(5355): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210)
08-24 06:05:12.714: E/AndroidRuntime(5355): at android.app.Activity.setContentView(Activity.java:1647)
08-24 06:05:12.714: E/AndroidRuntime(5355): at com.draw.map.TestActivity.onCreate(TestActivity.java:12)
08-24 06:05:12.714: E/AndroidRuntime(5355): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-24 06:05:12.714: E/AndroidRuntime(5355): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-24 06:05:12.714: E/AndroidRuntime(5355): ... 11 more
08-24 06:05:12.714: E/AndroidRuntime(5355): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.draw.map-1.apk]
08-24 06:05:12.714: E/AndroidRuntime(5355): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
08-24 06:05:12.714: E/AndroidRuntime(5355): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
08-24 06:05:12.714: E/AndroidRuntime(5355): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
08-24 06:05:12.714: E/AndroidRuntime(5355): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
08-24 06:05:12.714: E/AndroidRuntime(5355): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
08-24 06:05:12.714: E/AndroidRuntime(5355): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
08-24 06:05:12.714: E/AndroidRuntime(5355): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
08-24 06:05:12.714: E/AndroidRuntime(5355): ... 20 more
在我的项目中包含其他库文件:* facebook android SDK * google play service * android support jar 4
谢谢你的时间