我最近回到开发我正在处理的应用程序,并在尝试运行时遇到问题。该应用程序在运行之前运行良好,因此我对此感到有些困惑。
Log cat:
07-30 12:07:53.119: E/AndroidRuntime(8070): FATAL EXCEPTION: main
07-30 12:07:53.119: E/AndroidRuntime(8070): java.lang.RuntimeException: Unable to start activity ComponentInfo{simcas.fartberegneren/simcas.fartberegneren.MainActivity}: java.lang.NullPointerException
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.ActivityThread.access$700(ActivityThread.java:159)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.os.Looper.loop(Looper.java:137)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.ActivityThread.main(ActivityThread.java:5419)
07-30 12:07:53.119: E/AndroidRuntime(8070): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:07:53.119: E/AndroidRuntime(8070): at java.lang.reflect.Method.invoke(Method.java:525)
07-30 12:07:53.119: E/AndroidRuntime(8070): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
07-30 12:07:53.119: E/AndroidRuntime(8070): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
07-30 12:07:53.119: E/AndroidRuntime(8070): at dalvik.system.NativeStart.main(Native Method)
07-30 12:07:53.119: E/AndroidRuntime(8070): Caused by: java.lang.NullPointerException
07-30 12:07:53.119: E/AndroidRuntime(8070): at simcas.fartberegneren.MyAdapter.<init>(MyAdapter.java:38)
07-30 12:07:53.119: E/AndroidRuntime(8070): at simcas.fartberegneren.MainActivity.onCreate(MainActivity.java:32)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.Activity.performCreate(Activity.java:5372)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
07-30 12:07:53.119: E/AndroidRuntime(8070): ... 11 more
view_pager.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/view_pager" />
</LinearLayout>
MyAdapter.java:
public class MyAdapter extends FragmentStatePagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
public MyAdapter(FragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = activity.getActionBar();
mViewPager = pager;
mViewPager.setAdapter(this); // This is line 38 causing the nullpointer
mViewPager.setOnPageChangeListener(this);
MainActivity.java:
public class MainActivity extends FragmentActivity implements TabListener {
static Context context;
private MyAdapter myAdapter;
private ViewPager myViewPager;
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_pager);
myViewPager = new ViewPager(this);
myViewPager = (ViewPager) findViewById(R.id.view_pager);
final ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
myAdapter = new MyAdapter(this, myViewPager); // This is line 32
myAdapter.addTab(actionbar.newTab().setText(R.string.menu_speed), SpeedZonesFragment.class, null);
myAdapter.addTab(actionbar.newTab().setText(R.string.menu_dist), DistanceFragment.class, null);
myAdapter.addTab(actionbar.newTab().setText(R.string.menu_fuel), FuelConsumptionFragment.class, null);
我在这里缺少什么?
应该注意的是,自从我半年前在我的设备上成功运行以来,代码没有改变。
编辑:
看来,即使我添加了一些代码,nullpointer仍然来自相同的两行,尽管行已经改变了!发生了什么事?
答案 0 :(得分:2)
使用null public MyAdapter(FragmentActivity activity, ViewPager pager)
调用构造函数pager
。
在此处提出一个断点:
myViewPager = (ViewPager) findViewById(R.id.view_pager);
检查它是否正在返回您的寻呼机对象,并检查xml view_pager.xml
是否有寻呼机ID,确保它是view_pager
。
编辑:我看到了您发布的XML,请确保其名称为view_pager.xml
,因为您没有发布。
答案 1 :(得分:1)
试试这个: - 在您设置适配器的地方放置try catch块。如果可能的话,在Main Activity中取一个静态变量。
public static int selectedTab;
并在
中@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
selectedTab = tab.getPosition();
Log.d("Selected Tab is", ">" + selectedTab);
}
和您设置适配器的地方,放置这种类型的代码。在我的情况下,我有7个选项卡,每个选项卡都有视图寻呼机。所以在第四个选项卡上我把这个代码忽略了Null指针异常。实际上,这是因为,如果在第一个选项卡上数据未到达,我们将移动到第4或第5个选项卡,之后如果数据将出现在第一个选项卡上。然后我们得到NullPointer异常。对不起我的英文,我希望你能理解。
if (MainActivity.selectedTab == 0 || MainActivity.selectedTab == 1|| MainActivity.selectedTab == 2
|| MainActivity.selectedTab == 6) {
} else {
try {
adapter = new GalleryAdapter(getActivity(),
R.layout.row_gallery, mGridArray);
mGridView.setAdapter(adapter);
} catch (NullPointerException e) {
e.printStackTrace();
}
}