使用AndroidAnnotations将片段附加到活动?

时间:2016-02-22 20:36:45

标签: android-fragments android-fragmentactivity android-annotations

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

我有一个带有布局XML的Activity。我还有一个带有布局XML的片段。

如何将片段连接到活动? Fragment及其内容/布局应显示在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>

我不清楚如何将Fragment放入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) 

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

问题在于:

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没有实现该接口,这就是您获得此异常的原因。