機器人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? (初始化變量裏面的函數)
我英語不成熟。 對不起。
你在哪兒綁定你的XML? –
從'LayoutInflater'中移除註釋。 – Blasanka
如果在xml中指定類名,是不是綁定? –