使用 AndroidAnnotations 将片段附加到 Activity?

Attaching Fragment to Activity with AndroidAnnotations?

如何使用 AndroidAnnotations 将片段附加到 Activity?

我有一个 Activity 布局 XML。我还有一个布局为 XML.

的片段

如何将 Fragment 连接到 Activity?片段及其 content/layout 应显示在 Activity 布局 XML 文件的 <fragment ...> 标记内。

这是我的 类 和它们各自的 XML 布局(为了更好的可读性进行了修剪):

@EActivity(R.layout.activity_add_contact)
public class AddContactActivity extends AppCompatActivity {

    private final String TAG = getClass().getSimpleName();

    @FragmentById(R.id.add_contact_fragment)
    AddContactFragment addContactFragment;

    @Extra
    String contactName;

    @ViewById(R.id.toolbar)
    Toolbar toolbar;

    @AfterViews
    void createView() {
        setSupportActionBar(toolbar);

        setTitle(getString(R.string.new_contact_title, contactName.trim()));

    }

}

actvitiy_add_contact.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".AddContactActivity_">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <fragment
        android:id="@+id/add_contact_fragment"
        android:name="net.gazeapp.AddContactFragment_"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</android.support.design.widget.CoordinatorLayout>

这是片段:

@EFragment(R.layout.fragment_add_contact)
public class AddContactFragment extends Fragment implements DatePickerDialog.OnDateSetListener {
    private final String TAG = getClass().getSimpleName();

    @ViewById(R.id.nickname)
    EditText nickname;

    @ViewById(R.id.birthdate)
    EditText birthdate;

    public AddContactFragment() {
    }

    @AfterViews
    public void onCreateView() {
        // Hide Keyboard initially
        getActivity().getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
        );
    }
}

及其 XML 布局:

fragment_add_contact.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/scrollView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:overScrollMode="never"
    android:theme="@style/MyEditTextTheme">

<!-- FRAGMENT LAYOUT / INPUT FIELDS ETC -->

</ScrollView>

我不清楚如何将片段放入 Activity 中的 <fragment> 标记中。当我使用上面的代码 运行 它时,我收到此错误消息:

02-22 21:26:05.689 16267-16267/net.gazeapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.gazeapp, PID: 16267
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.gazeapp/net.gazeapp.AddContactActivity_}: android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class fragment
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
 at android.app.ActivityThread.-wrap11(ActivityThread.java)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:5417)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class fragment
 at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
 at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
 at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
 at net.gazeapp.AddContactActivity_.setContentView(AddContactActivity_.java:55)
 at net.gazeapp.AddContactActivity_.onCreate(AddContactActivity_.java:44)
 at android.app.Activity.performCreate(Activity.java:6251)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
 at android.os.Looper.loop(Looper.java:148) 
 at android.app.ActivityThread.main(ActivityThread.java:5417) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
 at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
 at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
 at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
 at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
 at net.gazeapp.AddContactActivity_.setContentView(AddContactActivity_.java:55) 
 at net.gazeapp.AddContactActivity_.onCreate(AddContactActivity_.java:44) 
 at android.app.Activity.performCreate(Activity.java:6251) 
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
 at android.os.Looper.loop(Looper.java:148) 
 at android.app.ActivityThread.main(ActivityThread.java:5417) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.ClassCastException: net.gazeapp.AddContactActivity_@cba10e4 must implement MyListFragment.OnContactObjectChangedListener
 at net.gazeapp.AddContactFragment.onAttach(AddContactFragment.java:161)
 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1019)
 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1226)
 at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1328)
 at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2284)
 at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
 at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:314)
 at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
 at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
 at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
 at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
 at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
 at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
 at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
 at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
 at net.gazeapp.AddContactActivity_.setContentView(AddContactActivity_.java:55) 
 at net.gazeapp.AddContactActivity_.onCreate(AddContactActivity_.java:44) 
 at android.app.Activity.performCreate(Activity.java:6251) 
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
 at android.os.Looper.loop(Looper.java:148) 
 at android.app.ActivityThread.main(ActivityThread.java:5417) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

感谢您的帮助。

问题出在这里:

Caused by: java.lang.ClassCastException: net.gazeapp.AddContactActivity_@cba10e4 must implement MyListFragment.OnContactObjectChangedListener
 at net.gazeapp.AddContactFragment.onAttach(AddContactFragment.java:161)

您没有在您的问题中分享 AddContactFragment.onAttach() 方法,但您似乎正在将 Activity 转换为 OnContactObjectChangedListener。但是,Activity 没有实现该接口,这就是您收到此异常的原因。