每当我试图调用viewpager.setAdapter
时,我都会遇到问题,每当我运行我的应用程序时,它都会崩溃。MainActiviy.java:46
,当我点击我的日志猫上的蓝色链接时,它需要我到我调用的viewpager.setAdapter
方法。以下是我的代码。
MainActivity
上课
public class MainActivity extends FragmentActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_appbar);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
private static final String TAG_START_CAMERA = "Start Camera";
private static final String TAG_START_MUSIC = "Start Music";
private static final String TAG_START_USER_MENU = "Start User Menu";
private Toolbar toolbar;
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager()));***"This is the error line"***
PagerSlidingTabStrip tabStrip = (PagerSlidingTabStrip)findViewById(R.id.tabs);
tabStrip.setViewPager(viewPager); } }
activity_main.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_alignParentEnd="true"
android:background="@color/primaryColor"
app:pstsDividerColor="@color/colorPrimarySecondary"
app:pstsIndicatorColor="@color/colorPrimarySecondary"
app:pstsDeactivateIconColor="#66323232"
app:pstsActivateIconColor="@color/colorPrimarySecondary"
app:pstsTabSwitch="true"
app:pstsIndicatorHeight="2dp"
app:pstsShouldExpand="true"
app:pstsUnderlineHeight="2dp"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/app_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="13dp" /> </RelativeLayout>
我的LogCat
06-29 08:33:11.034 24717-24717/? E/AndroidRuntime:FATAL EXCEPTION:main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lilstiikz.wehr/com.example.lilstiikz.wehr.MainActivity}: java.lang.NullPointerException Caused by: java.lang.NullPointerException
at com.example.lilstiikz.wehr.MainActivity.onCreate(MainActivity.java:46)
at android.app.Activity.performCreate(Activity.java:5179)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4950)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
问题是viewPager
为空,因为findViewById(R.id.pager)
返回null。反过来的原因是,正如Harish上面所说,你用setContentView
设置了错误的布局。
将来,您可以通过添加以下内容来自行调试NullPointerException
if(viewPager == null){ Log.e("MainActivity", "viewPager should not be null"); }
您怀疑可能导致异常的任何内容。
答案 1 :(得分:0)
将setContentView(R.layout.activity_main_appbar);
更改为setContentView(R.layout.activity_main);
更新:
对于多页滑块标签条。
com.astuetz.PagerSlidingTabStrip tabStrip = (com.astuetz.PagerSlidingTabStrip)findViewById(R.id.tabs);
tabStrip.setViewPager(viewPager);