2012-10-11 89 views
1

我尝试使用带有AndroidAnnotations和maven的片段。 它的工作与Android 4.0+,但后来我试图使它与Android 2.3.3工作,我不得不使用支持-V4行家库:支持Android片段,java.lang.NoSuchMethodException:片段(Context,AttributeSet)

<dependency> 
    <groupId>com.google.android</groupId> 
    <artifactId>support-v4</artifactId> 
    <version>r7</version> 
</dependency> 

我的片段被定义为:

<android.support.v4.app.Fragment android:id="@+id/myFragment" 
           android:layout_width="fill_parent" 
           android:layout_height="wrap_content" 
           class="com.bla.HeaderFragment_"/> 

和我的动态扩展FragmentActivity

我得到这个例外,当我运行该应用程序:

.....

Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class android.support.v4.app.Fragment 
at android.view.LayoutInflater.createView(LayoutInflater.java:508) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:408) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207) 
at android.app.Activity.setContentView(Activity.java:1657) 
at com.tagonsoft.codecamp.MainActivity_.setContentView(MainActivity_.java:46) 
at com.tagonsoft.codecamp.MainActivity_.onCreate(MainActivity_.java:31) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
... 11 more 

Caused by: java.lang.NoSuchMethodException: Fragment(Context,AttributeSet) 
at java.lang.Class.getMatchingConstructor(Class.java:643) 
at java.lang.Class.getConstructor(Class.java:472) 
at android.view.LayoutInflater.createView(LayoutInflater.java:480) 
... 22 more 

任何想法为什么?

+0

你的片段是否有一个空的构造函数? – dymmeh

+0

不,我尝试使用它与AndroidAnnotations像这里https://github.com/excilys/androidannotations/wiki/Enhance-Fragments – gdogaru

+2

使用'fragment'标签而不是'android.support.v4.app.Fragment'。 – Luksprog

回答

2

尝试<fragment>标记和name属性的类名称。此外,您使用的课​​程名称为com.bla.HeaderFragment_,看起来像是一个错字。

<fragment android:id="@+id/myFragment" 
      android:name="com.bla.HeaderFragment_" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
           /> 

更多示例here

+0

它似乎与_android:name_和_class_一起工作。来自支持库的文档提及_class_ http://developer.android.com/reference/android/app/Fragment.html#Layout。最后的下划线是由AndroidAnnotations生成的类。 – gdogaru

+0

我现在找不到,但是,我已经阅读过关于这个地方的信息,'name'将适用于新旧API。 –