2014-02-21 77 views
0

我在我的智慧結束與此! 我想爲我的活動添加一個片段。我已閱讀了大量有關此錯誤的Stackoverflow文章,但沒有看到任何解決方案(擴展FragmentActivity,導入android.support.v4等)已經爲我解決了這個問題。Android活動錯誤 - 錯誤膨脹類片段

這是我看到的錯誤:

02-21 13:59:52.063: E/AndroidRuntime(2165): FATAL EXCEPTION: main 
02-21 13:59:52.063: E/AndroidRuntime(2165): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/net.myd.app.custom.MyActivity}: android.view.InflateException: Binary XML file line #57: Error inflating class Fragment 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.app.ActivityThread.access$600(ActivityThread.java:130) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.os.Looper.loop(Looper.java:137) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at java.lang.reflect.Method.invoke(Method.java:511) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at dalvik.system.NativeStart.main(Native Method) 
02-21 13:59:52.063: E/AndroidRuntime(2165): Caused by: android.view.InflateException: Binary XML file line #57: Error inflating class Fragment 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.app.Activity.setContentView(Activity.java:1867) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at net.myd.app.custom.MyActivity.onCreate(MyActivity.java:46) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.app.Activity.performCreate(Activity.java:5008) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  ... 11 more 
02-21 13:59:52.063: E/AndroidRuntime(2165): Caused by: java.lang.ClassNotFoundException: android.view.Fragment 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.view.LayoutInflater.createView(LayoutInflater.java:552) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:643) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685) 
02-21 13:59:52.063: E/AndroidRuntime(2165):  ... 22 more 

這是我使用的片段類:

package net.myd.app.custom; 

import com.example.myfirstapp.R; 

import android.annotation.TargetApi; 
import android.app.Fragment; 

import android.app.Fragment; 
import android.os.Build; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
public class MyFragment extends Fragment 
{ 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.my_custom_fragment, container, false); 
    } 
} 

這是我使用的活動:

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
public class MyActivity extends FragmentActivity 
{ 
    // ... class fields 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.my_custom_activity); 

     selectFragment(); 
    } 

    public void selectFragment() 
    { 
     Fragment fragment = new MyFragment(); 

     FragmentManager fm = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fm.beginTransaction(); 
     fragmentTransaction.replace(R.id.my_fragment, fragment); 
     fragmentTransaction.commit(); 
    } 

    // etc... 
} 

而在我的活動的XML,這是我如何引用片段:

(有一個stackoverflow文章,它建議將完整的類路徑添加到片段xml代碼,我試過這個,但也沒有工作)。

<Fragment 
      android:id="@+id/my_fragment" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

如果任何人都可以看到任何問題導致此錯誤,我會非常感激!

感謝

+0

您的xml類中存在錯誤 – rajshree

+0

錯誤是什麼? – b85411

+0

等待我看 – rajshree

回答

1

替換:

<Fragment 
      android:id="@+id/my_fragment" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

的東西,如:

<FrameLayout 
      android:id="@+id/my_fragment" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
+0

你是一個傳奇人物。修好了!謝謝。當5分鐘的限制結束時,我會打勾你的答案:) – b85411

0

使用的FrameLayout可以解決這個問題。

<FrameLayout 
    android:id="@+id/my_fragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:ignore="MergeRootFrame" />