添加布局以在单独的类中查看

时间:2013-11-19 19:11:02

标签: android android-layout

我正在使用卡片UI库。我有一个类(“MyCard”),它有自己的视图膨胀。我从我的主要活动中打电话给那个班级。我想要做的是为该类中的膨胀布局添加相对布局,但我无法弄清楚如何去做。

这是mycard类:

public class MyCard extends Card {


    public MyCard(String title) {
        super(title);
    }

    @Override
    public View getCardContent(Context context) {
        View view = LayoutInflater.from(context).inflate(
                R.layout.mainactivity_contact_header, null);

        ((TextView) view.findViewById(R.id.tv_first_letter)).setText(title);

        LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.ll_card_header);

        return view;
    }
}

我喜欢做的是:

从我的其他课程中,能够按照“mycard.addViewToCard(RelativeLayout)”的方式做点什么

这甚至可能吗?

所以我给了它一个镜头,我得到一个空指针异常。有什么想法吗?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // the layout view for this fragment
    View view = inflater.inflate(R.layout.main_contacts_fragment,
            container, false);
    // inflate the view to add contact info (later to be added to 'view')
    View contactRow = LayoutInflater.from(getActivity().getBaseContext())
            .inflate(R.layout.mainactivity_contact_row, null);
    // The relative layout that houses the textview/imageview for contactRow
    RelativeLayout relativeLayout = (RelativeLayout) contactRow
            .findViewById(R.id.rl_main_fragment_root);
    // TextView for the contact name in contactRow
    TextView textView = (TextView) contactRow
            .findViewById(R.id.tv_contact_list_name);

    // Add the cards ui cardview
    CardUI mCardView = (CardUI) view.findViewById(R.id.cardsview);
    mCardView.setSwipeable(false);
    MyCard myCard = new MyCard("Contacts");
    // make a call to the linear layout in the cards UI layout so we can add
    // views later
    LinearLayout linearLayout = (LinearLayout) myCard.getCardContent(
            getActivity()).findViewById(R.id.ll_card_header);

    char startingChar = '0';
    for (int i = 0; i < contactArray.size(); i++) {
        // Create new card for new letter
        String name = contactArray.get(i).mName;
        if (name.charAt(0) != startingChar) {
            startingChar = name.charAt(0);

            myCard = new MyCard(Character.toString(startingChar));
        }
        // this is the textView for the contact name in contactRow nested in
        // 'relativeLayout'
        textView.setText(name);
        //  add relativeLayout to the parent linearLayout from myCard
            //  the line below is Line 77
        linearLayout.addView(relativeLayout);
        // add myCard to the cardView
        mCardView.addCard(myCard);
        mCardView.refresh();
    }

    return view;
}

mainactivity_contact_header.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_card_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:paddingLeft="8dp"
        android:paddingRight="8dp" >

        <TextView
            android:id="@+id/tv_first_letter"
            style="@style/CardTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="title" />
    </LinearLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="4dp"
        android:background="@color/stroke" />

</LinearLayout>

和例外:

11-19 20:09:46.052: E/AndroidRuntime(790): FATAL EXCEPTION: main
11-19 20:09:46.052: E/AndroidRuntime(790): java.lang.NullPointerException
11-19 20:09:46.052: E/AndroidRuntime(790):  at android.view.ViewGroup.addView(ViewGroup.java:3148)
11-19 20:09:46.052: E/AndroidRuntime(790):  at android.view.ViewGroup.addView(ViewGroup.java:3131)
11-19 20:09:46.052: E/AndroidRuntime(790):  at com.psesto.contactcards.main.MainListFragment.onCreateView(MainListFragment.java:77)
11-19 20:09:46.052: E/AndroidRuntime(790):  at android.app.Fragment.performCreateView(Fragment.java:1695)
11-19 20:09:46.052: E/AndroidRuntime(790):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
11-19 20:09:46.052: E/AndroidRuntime(790):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
11-19 20:09:46.052: E/AndroidRuntime(790):  at android.app.BackStackRecord.run(BackStackRecord.java:682)
11-19 20:09:46.052: E/AndroidRuntime(790):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
11-19 20:09:46.052: E/AndroidRuntime(790):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
11-19 20:09:46.052: E/AndroidRuntime(790):  at android.os.Handler.handleCallback(Handler.java:725)
11-19 20:09:46.052: E/AndroidRuntime(790):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-19 20:09:46.052: E/AndroidRuntime(790):  at android.os.Looper.loop(Looper.java:137)
11-19 20:09:46.052: E/AndroidRuntime(790):  at android.app.ActivityThread.main(ActivityThread.java:5041)
11-19 20:09:46.052: E/AndroidRuntime(790):  at java.lang.reflect.Method.invokeNative(Native Method)
11-19 20:09:46.052: E/AndroidRuntime(790):  at java.lang.reflect.Method.invoke(Method.java:511)
11-19 20:09:46.052: E/AndroidRuntime(790):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-19 20:09:46.052: E/AndroidRuntime(790):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-19 20:09:46.052: E/AndroidRuntime(790):  at dalvik.system.NativeStart.main(Native Method)

由于

1 个答案:

答案 0 :(得分:0)

好像你的relativeLayout为null,在执行

之前检查它是否为空

linearLayout.addView(relativeLayout);

您将在LogCat输出中看到

或NullPointerException。