2017-08-15 51 views
0

機器人findViewById方法返回的值是空機器人findViewById是空的LinearLayout

這種觀點是自定義類

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"/> 

觀看活動的Java(片段)

@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; 
    } 

裏面的xml是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); 
    } 
... 

爲什麼空變量mBtnLogout? (初始化變量裏面的函數)

我英語不成熟。 對不起。

+0

你在哪兒綁定你的XML? –

+0

從'LayoutInflater'中移除註釋。 – Blasanka

+0

如果在xml中指定類名,是不是綁定? –

回答

1
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; 
} 

該按鈕的新充氣視圖的一部分。由於膨脹結果不適用於該活動,但activity.findViewById找不到它。

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

的Un評論這條線

0
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); 
     ... 
    } 
} 
1

你必須在註釋行吹氣。刪除從註釋行

0

讓你init(AttributeSet attrs)方法

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

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

你可以得到孩子意見onViewAdded以下更改。如果您已經在佈局中定義了您的子視圖。

@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; 
    } 
}