user7951064
user7951064

Reputation:

android findViewById is null in LinearLayout

android findViewById method return value is null

this view is custom class

view.xml

<include
        android:id="@+id/view_before_login"
        layout="@layout/view_mypage_before_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

view activity java (Fragment)

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

        mBefore = (MemberBeforeLoginView)rootView.findViewById(R.id.view_before_login);
        mAfter = (MemberAfterLoginView)rootView.findViewById(R.id.view_after_login);

        mBefore.SetStateListener(this);
        mAfter.SetStateListener(this);

        route();

        return rootView;
    }

inside xml is view_mypage_before_login.xml

<?xml version="1.0" encoding="utf-8"?>
<com.main.view.MemberAfterLoginView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageButton
        android:id="@+id/profile"
        android:layout_width="137dp"
        android:layout_height="121dp"
        app:srcCompat="@drawable/empty" />

    <Button
        android:id="@+id/logout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="logout" />
</com.main.view.MemberAfterLoginView>

com.main.view.MemberAfterLoginView.java

public class MemberAfterLoginView extends LinearLayout implements View.OnClickListener, MemberViewListener.MemberChildViewInterface {
    private MemberViewListener mMemberListener = null;

    private Button mBtnLogout = null;

    public MemberAfterLoginView(Context context) {
        super(context);
        initialize(context);
    }

    public MemberAfterLoginView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialize(context);
    }

    public MemberAfterLoginView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context);
    }

    private void initialize(Context context) {
        /*LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.view_mypage_after_login, null);
        */

        mBtnLogout = (Button) findViewById(R.id.logout);
    }
...

why null variable mBtnLogout ? (initialize function inside variable)

I am immature in English. Sorry.

Upvotes: 0

Views: 854

Answers (6)

Muthukrishnan Rajendran
Muthukrishnan Rajendran

Reputation: 11642

You can get the child views in onViewAdded. If you already defined your child views in Layout.

@Override
public void onViewAdded(View child) {
    super.onViewAdded(child);

    switch (child.getId()) {
        case R.id.profile:
            Log.d("", "addView: profile Added");
            break;
        case R.id.logout:
            ((Button)child).setText("New alue");
            break;
    }
}

Upvotes: 0

Developer
Developer

Reputation: 101

Make the following changes in your init(AttributeSet attrs) method

View view = LayoutInflater.from(getContext()).inflate(R.layout.view_mypage_after_login, this, true);

mBtnLogout = (Button) view.findViewById(R.id.logout);

Upvotes: 0

k3b
k3b

Reputation: 14755

private View initialize(Context context) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.view_mypage_after_login, null);

    mBtnLogout = (Button) v.findViewById(R.id.logout);
    return v;
}

The button is part of new inflated view. since the inflate result is not applied to the activity yet activity.findViewById cannot find it.

Upvotes: 1

user8219568
user8219568

Reputation:

You had inflater in comment line. Remove that from comment line

Upvotes: 1

Linh
Linh

Reputation: 60999

public class MemberAfterLoginView extends LinearLayout {

    private ImageView mBtnLogout;

    public MemberAfterLoginView(Context context) {
        this(context, null);
    }

    public MemberAfterLoginView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MemberAfterLoginView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(attrs);
    }

    private void init(AttributeSet attrs) {
        LayoutInflater.from(getContext()).inflate(R.layout.view_mypage_after_login, this, true);

        mBtnLogout = (Button) findViewById(R.id.logout);
        ...
    }
}

Upvotes: 0

user8256287
user8256287

Reputation: 177

/*LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.view_mypage_after_login, null);
        */

Un comment this line

Upvotes: 0

Related Questions