机器人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中指定类名,是不是绑定? –