我有一个应用程序,包括简单的抽屉导航和菜单列表。 在其中一个菜单中,我想添加一个带有Tab布局的页面 - this上的可滑动视图 - 这个应用程序正在运行,没有任何错误。
我决定在其中一个菜单中放一个按钮,当按下它时会将用户引导到TabLayout的MainActivity中。
现在在我的主项目中我有两个MainActivity类,一个是MainActivity.java(这是NavDrawer的)和MainActivity2.java(这是TabLayout的),我还设法将所有必需的文件复制粘贴到我的主项目包括适配器。
我将按钮放在HomeFragment.java中,如下所示:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
Button mButton = (Button) rootView.findViewById(R.id.button1);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// here you set what you want to do when user clicks your button,
Intent intent = new Intent(getActivity(), MainActivity2.class);
startActivity(intent);
}
});
return rootView;
}
}
但不幸的是,当我点击按钮时,应用程序崩溃显示此logcat
04-08 05:58:52.106: E/AndroidRuntime(1317): FATAL EXCEPTION: main
04-08 05:58:52.106: E/AndroidRuntime(1317): Process: my.jlm, PID: 1317
04-08 05:58:52.106: E/AndroidRuntime(1317): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.jlm/my.jlm.MainActivity2}: java.lang.NullPointerException
04-08 05:58:52.106: E/AndroidRuntime(1317): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
04-08 05:58:52.106: E/AndroidRuntime(1317): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
04-08 05:58:52.106: E/AndroidRuntime(1317): at android.app.ActivityThread.access$700(ActivityThread.java:135)
04-08 05:58:52.106: E/AndroidRuntime(1317): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
04-08 05:58:52.106: E/AndroidRuntime(1317): at android.os.Handler.dispatchMessage(Handler.java:102)
04-08 05:58:52.106: E/AndroidRuntime(1317): at android.os.Looper.loop(Looper.java:137)
04-08 05:58:52.106: E/AndroidRuntime(1317): at android.app.ActivityThread.main(ActivityThread.java:4998)
04-08 05:58:52.106: E/AndroidRuntime(1317): at java.lang.reflect.Method.invokeNative(Native Method)
04-08 05:58:52.106: E/AndroidRuntime(1317): at java.lang.reflect.Method.invoke(Method.java:515)
04-08 05:58:52.106: E/AndroidRuntime(1317): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-08 05:58:52.106: E/AndroidRuntime(1317): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-08 05:58:52.106: E/AndroidRuntime(1317): at dalvik.system.NativeStart.main(Native Method)
04-08 05:58:52.106: E/AndroidRuntime(1317): Caused by: java.lang.NullPointerException
04-08 05:58:52.106: E/AndroidRuntime(1317): at my.jlm.MainActivity2.onCreate(MainActivity2.java:32)
它说错误可能是由第32行的MainActivity2.java引起的,但我不知道该如何处理它
这是第32行
viewPager.setAdapter(mAdapter);
这是我完整的MainActivity2
public class MainActivity2 extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
感谢您的好时光。
答案 0 :(得分:1)
viewPager
是null
。你初始化它是这样的:
viewPager = (ViewPager) findViewById(R.id.pager);
如果找到,则 findViewById
会返回视图,否则会返回null
。
public View findViewById(int id)在API级别1中添加
查找由XML中的id属性标识的视图 在onCreate(Bundle)处理。返回
The view if found or null otherwise.
您的问题是找不到视图,可能是因为它尚未创建。