2015-10-15 165 views
1

我是新來android應用程序開發,目前正在編寫的活動添加片段的活動。錯誤:膨脹類片段

我收到此錯誤:

10-15 18:26:32.165 24020-24020/? E/AndroidRuntime: FATAL EXCEPTION: main 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime: Process: com.ranapps.listing_agent_trail1, PID: 24020 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ranapps.listing_agent_trail1/com.ranapps.listing_agent_trail1.MainActivity}: android.view.InflateException: Binary XML file line #30: Error inflating class fragment 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2264) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.app.ActivityThread.access$1100(ActivityThread.java:141) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:102) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:136) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5336) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at java.lang.reflect.Method.invokeNative(Native Method) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:515) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:873) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at dalvik.system.NativeStart.main(Native Method) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime: Caused by: android.view.InflateException: Binary XML file line #30: Error inflating class fragment 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.view.LayoutInflater.rInflate(LayoutInflater.java:775) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.view.LayoutInflater.inflate(LayoutInflater.java:512) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.view.LayoutInflater.inflate(LayoutInflater.java:409) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.view.LayoutInflater.inflate(LayoutInflater.java:358) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.app.Activity.setContentView(Activity.java:1968) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at com.ranapps.listing_agent_trail1.MainActivity.onCreate(MainActivity.java:16) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.app.Activity.performCreate(Activity.java:5302) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2228) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313)  
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.app.ActivityThread.access$1100(ActivityThread.java:141)  
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)  
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:102)  
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:136)  
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5336)  

我MainActivity.java:

package com.ranapps.listing_agent_trail1; 


import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.support.v4.app.FragmentActivity; 


public class MainActivity extends FragmentActivity { 

    private Toolbar toolbar; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if(findViewById(R.id.footer) != null){ 

      if(savedInstanceState != null) 
       return; 
     } 

     Footer bottom1 = new Footer(); 

     bottom1.setArguments(getIntent().getExtras()); 

     getSupportFragmentManager().beginTransaction() 
        .add(R.id.footer, bottom1).commit(); 

    } 

} 

我Footer.java:

public class Footer extends Fragment { 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     //return super.onCreateView(inflater, container, savedInstanceState); 

     return inflater.inflate(R.layout.footer_bar, container, false); 
    } 
} 

main_activity.xml

<fragment 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    android:id="@+id/footer" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    /> 

我正在關注教程http://developer.android.com/training/basics/fragments/creating.html

我搜索了所有類似的問題,但沒有一個能解決問題。 謝謝。

+0

確保您的庫和項目具有相同的android目標版本。確保你實際上在增加支持片段類 – Calvin

回答

3

你有沒有指定的屬性android:name在你的佈局爲<fragment標籤。它必須包含片段的完整合格路徑。例如。

<fragment android:name="com.example.fragments.Footer" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/footer" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
/>