view.findViewById() returns 空
view.findViewById() returns null
我已经使用 FrameLayout
来显示叠加屏幕。在实例化 RelativeLayout 时,findViewById returns null。
这是 xml 文件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<me.relex.circleindicator.CircleIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginBottom="48dp"
android:layout_gravity="bottom"
app:ci_drawable="@drawable/orange_radius"
app:ci_drawable_unselected="@drawable/white_radius"/>
</RelativeLayout>
<!--Below is the transparent layout positioned at startup -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#70000000"
android:id="@+id/topLayout">
<ImageView
android:id="@+id/ivInstruction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="25dp"
android:layout_marginRight="15dp"
android:clickable="false"
android:paddingLeft="20dip"
android:scaleType="center"
android:src="@drawable/home" />
</RelativeLayout>
片段代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = getActivity().getLayoutInflater().inflate(R.layout.our_work_layout, null);
topLayout = (RelativeLayout) mView.findViewById(R.id.topLayout);
if (isFirstTime()) {
topLayout.setVisibility(View.INVISIBLE);
}
ViewPager viewpager = (ViewPager) mView.findViewById(R.id.viewpager);
// mPager.setAdapter(mAdapter);
ImageAdapter adapter = new ImageAdapter(act);
viewpager.setAdapter(adapter);
CircleIndicator indicator = (CircleIndicator) mView.findViewById(R.id.indicator);;
indicator.setViewPager(viewpager);
return mView;
}
private boolean isFirstTime()
{
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean ranBefore = preferences.getBoolean("RanBefore", false);
if (!ranBefore) {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", true);
editor.commit();
topLayout.setVisibility(View.VISIBLE);
topLayout.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
topLayout.setVisibility(View.INVISIBLE);
return false;
}
});
}
return ranBefore;
}
您的问题在这里:
ViewPager viewpager = (ViewPager) mView.findViewById(R.id.viewpager);
此布局中没有"viewpager"。您使用了 ID "pager"。
将此行更正为以下内容:
ViewPager viewpager = (ViewPager) mView.findViewById(R.id.pager);
我已经使用 FrameLayout
来显示叠加屏幕。在实例化 RelativeLayout 时,findViewById returns null。
这是 xml 文件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<me.relex.circleindicator.CircleIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginBottom="48dp"
android:layout_gravity="bottom"
app:ci_drawable="@drawable/orange_radius"
app:ci_drawable_unselected="@drawable/white_radius"/>
</RelativeLayout>
<!--Below is the transparent layout positioned at startup -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#70000000"
android:id="@+id/topLayout">
<ImageView
android:id="@+id/ivInstruction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="25dp"
android:layout_marginRight="15dp"
android:clickable="false"
android:paddingLeft="20dip"
android:scaleType="center"
android:src="@drawable/home" />
</RelativeLayout>
片段代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = getActivity().getLayoutInflater().inflate(R.layout.our_work_layout, null);
topLayout = (RelativeLayout) mView.findViewById(R.id.topLayout);
if (isFirstTime()) {
topLayout.setVisibility(View.INVISIBLE);
}
ViewPager viewpager = (ViewPager) mView.findViewById(R.id.viewpager);
// mPager.setAdapter(mAdapter);
ImageAdapter adapter = new ImageAdapter(act);
viewpager.setAdapter(adapter);
CircleIndicator indicator = (CircleIndicator) mView.findViewById(R.id.indicator);;
indicator.setViewPager(viewpager);
return mView;
}
private boolean isFirstTime()
{
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean ranBefore = preferences.getBoolean("RanBefore", false);
if (!ranBefore) {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", true);
editor.commit();
topLayout.setVisibility(View.VISIBLE);
topLayout.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
topLayout.setVisibility(View.INVISIBLE);
return false;
}
});
}
return ranBefore;
}
您的问题在这里:
ViewPager viewpager = (ViewPager) mView.findViewById(R.id.viewpager);
此布局中没有"viewpager"。您使用了 ID "pager"。
将此行更正为以下内容:
ViewPager viewpager = (ViewPager) mView.findViewById(R.id.pager);