Android Studio将多个视图添加到主细节模板的细节片段

时间:2015-05-07 03:19:04

标签: android android-fragments android-activity

我正在学习Android编程,我需要帮助。我在Android Studio中创建了一个项目,我正在使用主详细信息模板。列表是人,详细信息应该是关于人的信息,例如他们的姓名和出生日期以及其他信息。默认情况下,fragment_person_detail文件只有一个id为person_detail的TextView。



<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/person_detail"
    style="?android:attr/textAppearanceLarge" android:layout_width="match_parent"
    android:layout_height="match_parent" android:padding="16dp" android:textIsSelectable="true"
    tools:context=".PersonDetailFragment" />
&#13;
&#13;
&#13;

模板中还包含一个activity_person_detail,它具有片段的框架布局

&#13;
&#13;
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/person_detail_container"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".PersonDetailActivity" tools:ignore="MergeRootFrame" />
&#13;
&#13;
&#13;

所以,由于布局是在一个单独的文件中,我对如何将另一个textview添加到片段感到困惑。当我尝试时,它告诉我我有多个根标签。

我想以生日的Textview为例,那么我该如何实现呢?

这是我的PersonDetailFragment

&#13;
&#13;
public class PersonDetailFragment extends Fragment {
    /**
     * The fragment argument representing the item ID that this fragment
     * represents.
     */
    public static final String ARG_ITEM_ID = "item_id";

    /**
     * The dummy content this fragment is presenting.
     */
    private DummyContent.DummyItem mItem;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public PersonDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments().containsKey(ARG_ITEM_ID)) {
            // Load the dummy content specified by the fragment
            // arguments. In a real-world scenario, use a Loader
            // to load content from a content provider.
            mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_person_detail, container, false);

        // Show the dummy content as text in a TextView.
        if (mItem != null) {
            ((TextView) rootView.findViewById(R.id.person_detail)).setText(mItem.content);
            ((TextView) rootView.findViewById(R.id.person_age)).setText(mItem.birthdate);
        }

        return rootView;
    }
}
&#13;
&#13;
&#13;

因此,在该文件的末尾,您可以看到我想引用id为person_age的TextView。

但现在我收到以下错误

&#13;
&#13;
05-06 20:43:42.160  14205-14205/com.dwarwick.finalproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.dwarwick.finalproject, PID: 14205
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.x.finalproject/com.x.finalproject.PersonDetailActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
            at com.dwarwick.finalproject.PersonDetailFragment.onCreateView(PersonDetailFragment.java:58)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
            at android.app.Activity.performStart(Activity.java:5953)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
&#13;
&#13;
&#13;

因为我做了以下更改。

&#13;
&#13;
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/person_detail_container"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".PersonDetailActivity" tools:ignore="MergeRootFrame">

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:id="@+id/person_birthdate"
        style="?android:attr/textAppearanceLarge" android:layout_width="match_parent"
        android:layout_height="match_parent" android:padding="16dp" android:textIsSelectable="true"
        tools:context=".PersonDetailFragment" />
    </FrameLayout>
&#13;
&#13;
&#13;

任何帮助都将不胜感激。

编辑:

因此,除了按照建议更改Activity布局外,我还必须修改PersonDetailFragment.java,如下所示

&#13;
&#13;
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_person_detail, container, false);
        //View textview = inflater.inflate(R.layout.activity_person_detail, container, false);

        // Show the dummy content as text in a TextView.
        if (mItem != null) {
            ((TextView) rootView.findViewById(R.id.person_detail)).setText(mItem.content);
            ((TextView) rootView.findViewById(R.id.person_age)).setText(mItem.birthdate);
        }

        return rootView;
    }
&#13;
&#13;
&#13;

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您的代码正在查找id为person_age的文本视图,然后为其设置值。您的布局现在只有一个元素(person_detail),因此您需要添加id为person_age的第二个textview。

您可能还想将父级更改为RelativeLayout,这样您就可以更轻松地定位到两个文本视图。尝试更新布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/person_detail_container"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".PersonDetailActivity" 
    tools:ignore="MergeRootFrame">

    <TextView         
        android:id="@+id/person_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:padding="16dp" 
        android:textIsSelectable="true"
        style="?android:attr/textAppearanceLarge" />

    <TextView         
        android:id="@+id/person_detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_below="@+id/person_age"
        android:padding="16dp" 
        android:textIsSelectable="true"
        style="?android:attr/textAppearanceLarge" />
</RelativeLayout>