我正在尝试添加包含TabLayout
视图的片段,但我一直收到错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.design.widget.TabLayout$Tab android.support.design.widget.TabLayout.newTab()' on a null object reference
at ca.rev.revcore.MainActivity.onCreate(MainActivity.java:76)
这是我要添加的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rev_tablayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/RevStyle_PlaceNewBagBttn">
<android.support.design.widget.TabItem
android:id="@+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab1" />
</android.support.design.widget.TabLayout>
这就是我试图添加它的方式:
TabLayout tabLayout = (TabLayout) findViewById(R.id.rev_tablayout);
tabLayout.addTab(tabLayout.newTab().setText("Upload"));
tabLayout.addTab(tabLayout.newTab().setText("Pics"));
tabLayout.addTab(tabLayout.newTab().setText("Vids"));
tabLayout.addTab(tabLayout.newTab().setText("Papers"));
tabLayout.getTabAt(0).setIcon(R.drawable.ic_publish_black_24dp);
tabLayout.getTabAt(1).setIcon(R.drawable.ic_menu_camera);
tabLayout.getTabAt(2).setIcon(R.drawable.ic_videocam_black_24dp);
tabLayout.getTabAt(3).setIcon(R.drawable.ic_airplay_black_24dp);
我能错过什么?
Vielen dank im voraus。
答案 0 :(得分:3)
如果您正在使用片段。变化
TabLayout tabLayout = (TabLayout) findViewById(R.id.rev_tablayout);
到
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.rev_tablayout);
view
是一个对象,它会使您的布局文件膨胀并以onCreateView()
方法返回视图。
答案 1 :(得分:1)
如果您正在使用片段。在你的oncreate视图中绑定这样的控件,你在那里夸大你的布局
View rootView = inflater.inflate(R.layout.fragment_, container, false);
TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tablayout);
答案 2 :(得分:1)
覆盖Fragment中的onCreateView()方法,并对Fragment进行膨胀 布局使用LayoutInflater Class对象并通过传递 ViewGroup参数,即片段所在的活动 嵌入式。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:background="@color/transparent_black"
tools:context="il.co.cambium.sport5radio.fragments.MediaPlayerFragment">
<!-- Loader container -->
<RelativeLayout
android:id="@+id/mediaPlayerLoader"
android:layout_width="124dp"
android:layout_height="124dp"
android:layout_centerInParent="true"
android:visibility="gone">
<!-- Circular image view -->
<com.mikhaellopez.circularimageview.CircularImageView
android:layout_width="122dp"
android:layout_height="122dp"
android:layout_centerInParent="true"
android:src="@drawable/icon"
app:civ_border="true"/>
<!-- Loader-->
<fr.castorflex.android.circularprogressbar.CircularProgressBar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:indeterminate="true"
app:cpb_color="@color/colorPrimary"
app:cpb_max_sweep_angle="300"
app:cpb_min_sweep_angle="10"
app:cpb_stroke_width="5dp"/>
</RelativeLayout>
<!-- Video display -->
<SurfaceView
android:id="@+id/mediaPlayerSurfaceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="invisible"/>
<!-- Controls -->
<RelativeLayout
android:id="@+id/controlsRootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
<!-- Top Bar -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentTop="true"
android:background="@color/transparent_black"
android:orientation="horizontal">
<!-- Minimize button + collapsed play button -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<!-- Close icon -->
<ImageButton
android:id="@+id/videoBackBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:background="@drawable/round_ripple_effect_player"
android:scaleType="centerInside"
android:src="@mipmap/player_ic_minimize"
android:tint="@color/colorPrimaryDark"/>
<!-- Button play -->
<ImageButton
android:id="@+id/collapsedPlayPauseBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/round_ripple_effect_player"
android:padding="5dp"
android:scaleType="centerInside"
android:src="@drawable/icon_pause"
android:visibility="gone"/>
</LinearLayout>
<!-- Title -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="24dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="9dp"
android:layout_weight="1"
android:focusable="true"
android:focusableInTouchMode="true">
<TextView
android:id="@+id/videoPlayerTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black"
android:textSize="20dp"/>
</LinearLayout>
<!-- Live dot -->
<ImageView
android:id="@+id/fragmentMediaPlayerBlinkingCircle"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_gravity="center_vertical"
android:scaleType="centerInside"
android:src="@drawable/ic_circle"
android:visibility="gone"/>
<!-- Poster -->
<ImageView
android:id="@+id/mediaImage"
android:layout_width="75dp"
android:layout_height="48dp"
android:scaleType="centerCrop"
android:src="@drawable/icon"/>
</LinearLayout>
<!-- Bottom controls -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/transparent_black"
android:layoutDirection="ltr"
android:orientation="vertical"
android:paddingBottom="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<!--<ImageButton
android:id="@+id/media_prev"
style="@android:style/MediaButton.Previous"/>-->
<!--<ImageButton
android:id="@+id/media_rew"
style="@android:style/MediaButton.Rew"/>-->
<!--<ImageButton
android:id="@+id/media_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@mipmap/notification_ic_play"
android:background="@drawable/ripple_effect_player"
android:visibility="gone"/>
style="@android:style/MediaButton.Play"-->
<ImageButton
android:id="@+id/media_play_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:background="@drawable/round_ripple_effect_player"
android:src="@drawable/icon_pause"/>
<!--style="@android:style/MediaButton.Pause"-->
<!--<ImageButton
android:id="@+id/media_ffwd"
style="@android:style/MediaButton.Ffwd"/>-->
<!-- <ImageButton
android:id="@+id/media_next"
style="@android:style/MediaButton.Next"/>-->
</LinearLayout>
<LinearLayout
android:id="@+id/fragmentMediaPlayerProgressContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/media_position"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:textColor="@color/black_semi_transparent"
android:textSize="14sp"
android:textStyle="bold"/>
<SeekBar
android:id="@+id/media_progress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="4dp"
android:theme="@style/MyProgressBar"/>
<TextView
android:id="@+id/media_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:textColor="@color/black_semi_transparent"
android:textSize="14sp"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<强>演示强>
TabLayout tabLayout = (TabLayout) your_View_OBJ.findViewById(R.id.rev_tablayout);
答案 3 :(得分:0)
在activity_main xml中是否带有id rev_tablayout的标签布局,还是在片段的布局中?请检查您尝试访问tablayout的位置。