Android java。片段页面上的lang.NullPointerException
我知道NullPointerException有很多线程,我也知道这是由某个空值引起的,但我是Android开发(以及Java)的新手,我第一次尝试运行一个Tabs示例,所以我不是很实用
这个例子是:https://github.com/codepath/安卓_guides/wiki/Google-Play-Style-Tabs-using-TabLayout
当我启动应用程序时,它崩溃并返回以下错误:
9892-9892/com.mycompany.myfirstapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.myfirstapp/com.mycompany.myfirstapp.TabActivity}: java.lang.NullPointerException
at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at 安卓.app.ActivityThread.access$600(ActivityThread.java:141)
at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at 安卓.os.Handler.dispatchMessage(Handler.java:99)
at 安卓.os.Looper.loop(Looper.java:137)
at 安卓.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.mycompany.myfirstapp.TabActivity.onCreate(TabActivity.java:20)
at 安卓.app.Activity.performCreate(Activity.java:5104)
at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at 安卓.app.ActivityThread.access$600(ActivityThread.java:141)
at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at 安卓.os.Handler.dispatchMessage(Handler.java:99)
at 安卓.os.Looper.loop(Looper.java:137)
at 安卓.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
这是/res/layout/activity\u tab\u测试。xml
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical">
<安卓.support.design.widget.TabLayout
安卓:id="@+id/sliding_tabs"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
app:tabMode="scrollable" />
<安卓.support.v4.view.ViewPager
安卓:id="@+id/viewpager"
安卓:layout_width="match_parent"
安卓:layout_height="0px"
安卓:layout_weight="1"
安卓:background="@安卓:color/white" />
</LinearLayout>
这是/res/layout/fragment\u页面。xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:gravity="center" />
PageFragment。java
package com.mycompany.myfirstapp;
import 安卓.support.v4.app.Fragment;
import 安卓.os.Bundle;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.TextView;
public class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page, container, false);
TextView textView = (TextView) view;
textView.setText("Fragment #" + mPage);
return view;
}
}
样本碎片页雷达。java
package com.mycompany.myfirstapp;
import 安卓.support.v4.app.Fragment;
import 安卓.support.v4.app.FragmentManager;
import 安卓.content.Context;
import 安卓.support.v4.app.FragmentPagerAdapter;
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" };
private Context context;
public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Fragment getItem(int position) {
return PageFragment.newInstance(position + 1);
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
tab活动。java(示例的主要活动)
package com.mycompany.myfirstapp;
import 安卓.os.Bundle;
import 安卓.support.design.widget.TabLayout;
import 安卓.support.v4.app.FragmentActivity;
import 安卓.support.v4.view.ViewPager;
public class TabActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_page);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),
TabActivity.this));
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
}
空值可以在哪里
# 1 楼答案
根据您发布的内容,您的ViewPager属于
activity_tab_test.xml
而不是fragment_page.xml
,它只包含一个TextView
。改变与