我想将我的应用程序的支持添加到Android Foryo。
但我似乎无法解决问题。我使用Google和StackOverflow进行了很多研究。我唯一得到的是我必须使用<android.support.v4.app.fragment
代替<fragment
使用支持库,但这也行不通!
每当我尝试运行应用程序时,它都会崩溃,我在LogCat中收到此错误。
android.view.InflateException: Binary XML file line #40: Error inflating class android.support.v4.app.fragment
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
<LinearLayout
android:id="@+id/settings_fragment_layout"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FFffffff"
android:orientation="vertical" >
<android.support.v4.app.fragment
android:id="@+id/settings_fragment_section"
android:name="com.example.SettingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
爪哇
/**
*
*/
package com.example;
import android.app.TabActivity;
import android.os.Bundle;
/**
* @author
*
*/
public class MainActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
SettingsFragment.java 只是......
package com.example;
import android.os.Bundle;
import com.nozzha.support.v4.preference.PreferenceFragment;
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_settings);
}
}
logcat的
11-22 11:19:54.141: E/AndroidRuntime(1197): FATAL EXCEPTION: main
11-22 11:19:54.141: E/AndroidRuntime(1197): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MainActivity}: android.view.InflateException: Binary XML file line #40: Error inflating class android.support.v4.app.fragment
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.os.Looper.loop(Looper.java:123)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-22 11:19:54.141: E/AndroidRuntime(1197): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 11:19:54.141: E/AndroidRuntime(1197): at java.lang.reflect.Method.invoke(Method.java:521)
11-22 11:19:54.141: E/AndroidRuntime(1197): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-22 11:19:54.141: E/AndroidRuntime(1197): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-22 11:19:54.141: E/AndroidRuntime(1197): at dalvik.system.NativeStart.main(Native Method)
11-22 11:19:54.141: E/AndroidRuntime(1197): Caused by: android.view.InflateException: Binary XML file line #40: Error inflating class android.support.v4.app.fragment
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.createView(LayoutInflater.java:503)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-22 11:19:54.141: E/AndroidRuntime(1197): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.Activity.setContentView(Activity.java:1647)
11-22 11:19:54.141: E/AndroidRuntime(1197): at com.example.MainActivity.onCreate(MainActivity.java:19)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-22 11:19:54.141: E/AndroidRuntime(1197): ... 11 more
11-22 11:19:54.141: E/AndroidRuntime(1197): Caused by: java.lang.NoSuchMethodException: fragment(Context,AttributeSet)
11-22 11:19:54.141: E/AndroidRuntime(1197): at java.lang.Class.getMatchingConstructor(Class.java:660)
11-22 11:19:54.141: E/AndroidRuntime(1197): at java.lang.Class.getConstructor(Class.java:477)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.createView(LayoutInflater.java:475)
11-22 11:19:54.141: E/AndroidRuntime(1197): ... 22 more
答案 0 :(得分:1)
这是因为您的Activity不会扩展基本活动FragmentActivity
。如果您使用基于支持的片段,则您的活动必须扩展FragmentActivity
。
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
答案 1 :(得分:0)
请尝试添加构造函数。 public void SettingsFragment(){}。 Activity需要它来正确初始化片段。