片段页面上的Android java.lang.NullPointerException

时间:2015-09-22 10:40:04

标签: java android android-fragments nullpointerexception

我知道NullPointerException有很多线程,我也知道这是由某个地方的空值引起的,但我是Android开发中的新手(以及Java),我试图运行第一次使用Tabs示例,所以我不太实际。

例如:https://github.com/codepath/android_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 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.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.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.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 android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.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.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

这是 /res/layout/activity_tab_test.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.TabLayout
    android:id="@+id/sliding_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMode="scrollable" />

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1"
    android:background="@android:color/white" />

</LinearLayout>

这是 /res/layout/fragment_page.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />

PageFragment.java

package com.mycompany.myfirstapp;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.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;
}
}

SampleFragmentPagerAdapter.java

package com.mycompany.myfirstapp;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.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];
}
}

TabActivity.java (示例的MainActivity)

package com.mycompany.myfirstapp;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.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 个答案:

答案 0 :(得分:6)

根据您发布的内容,您的ViewPager属于activity_tab_test.xml,而不属于fragment_page.xmlTextView仅包含一个 setContentView(R.layout.fragment_page); 。改变

 setContentView(R.layout.activity_tab_test);

       var div = '<div class="row" id="flightsummary"><div class="col-md-6"><div class="summary"><p> Transportation Start Time =' + data[i]["TRANSPORT_START_TIME"]
     + '</p><p> Reporting Start Time =' + data[i]["REPORTING_START_TIME"] +
      '</p><p> Chocks Off Time =' + data[i]["CHOCKS_OFF_TIME"]
     + '</p><p> Chocks On Time =' + data[i]["CHOCKS_ON_TIME"]
      + '</p><p>+Duty End Time =' + data[i]["DUTY_END_TIME"]
      + '</p><p> Flight Time =' + data[i]["FLIGHT_TIME"]
      + '</p><p> Flight Duty Period =' + data[i]["FLIGHT_DUTY_PERIOD"]
       + '</p></p></div></div><div class="col-md-6"><div class="summary summary-right"><p> Rest Period Start Time ='
       + data[i]["REST_PERIOD_START_TIME"]
       + '</p><p> Minimum Rest Hours ='
        + data[i]["MINIMUM_REST_HOURS"]
        + '</p><p> Next Transportation Start Time <span class="calendar"></span> ='
        + data[i]["NEXT_TRNS_START_TIME"]
        + '</p><p> Next Reporting Time <span class="calendar"></span> ='
        + data[i]["NEXT_REPORTING_TIME"]
         + '</p><p> Next Earliest Take Off <span class="calendar"></span> ='
         + data[i]["NEXT_EARLIEST_TAKE_OFF"] + '</p><p> Last Landing Time <span class="calendar"></span> =' 
         + data[i]["LAST_LANDING_TIME"]; +'</p></div></div></div>;';