HomeActivity类的空指针异常

时间:2014-05-20 10:25:17

标签: android nullpointerexception

运行代码时出现空指针异常。

堆栈跟踪:

 E/AndroidRuntime(1659): FATAL EXCEPTION: main
 E/AndroidRuntime(1659): Process: com.fth.android, PID: 1659
 E/AndroidRuntime(1659): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fth.android/com.sit.fth.activity.HomeActivity}: java.lang.NullPointerException
 E/AndroidRuntime(1659):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
 E/AndroidRuntime(1659):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
 E/AndroidRuntime(1659):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
 E/AndroidRuntime(1659):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
 E/AndroidRuntime(1659):    at android.os.Handler.dispatchMessage(Handler.java:102)
 E/AndroidRuntime(1659):    at android.os.Looper.loop(Looper.java:136)
 E/AndroidRuntime(1659):    at android.app.ActivityThread.main(ActivityThread.java:5017)
 E/AndroidRuntime(1659):    at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(1659):    at java.lang.reflect.Method.invoke(Method.java:515)
 E/AndroidRuntime(1659):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 E/AndroidRuntime(1659):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 E/AndroidRuntime(1659):    at dalvik.system.NativeStart.main(Native Method)
 E/AndroidRuntime(1659): Caused by: java.lang.NullPointerException
 E/AndroidRuntime(1659):    at com.sit.fth.activity.HomeActivity.onCreate(HomeActivity.java:103)
 E/AndroidRuntime(1659):    at android.app.Activity.performCreate(Activity.java:5231)
 E/AndroidRuntime(1659):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
 E/AndroidRuntime(1659):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
 E/AndroidRuntime(1659):    ... 11 more

HomeActivity.java:

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;

public class HomeActivity extends BaseActivity implements
        VideoFragment.OnVideoSelectedListener,

        AboutFragment.OnAboutFragmentSelectedListener,

        AnnouncenentFragment.OnAnnouncementSelectedListener,

        GalleryFragment.OnGalItemSelectedListener,

        MagzineFragment.OnMagzineSelectedListener {


    private ActionBar actionabar;
    private ViewPager viewpager;
    private LiveStreamFragment liveStreamFragment;

    private AppData appData;
    private FragmentManager fm;
    private MyFragmentPagerAdapter fragmentPagerAdapter;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);

        //setContentView(R.layout.base_fragment);
        setContentView(R.layout.activity_main);

        appData = ((GemsApplication)this.getApplication()).getAppData();

        actionabar = getActionBar();

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);


        liveStreamFragment = new LiveStreamFragment();

        viewpager = (ViewPager) findViewById(R.id.pager);
        fm = getSupportFragmentManager();



        ActionBar.TabListener tabListener = new ActionBar.TabListener() {

            @Override
            public void onTabReselected(Tab arg0,
                    android.app.FragmentTransaction arg1) {

            }

            @Override
            public void onTabSelected(Tab tab,
                    android.app.FragmentTransaction ft) {
                viewpager.setCurrentItem(tab.getPosition());

            }

            @Override
            public void onTabUnselected(Tab tab,
                    android.app.FragmentTransaction ft) {

            }
        };


        Tab tab = actionabar.newTab().setText(getString(R.string.str_home))
                .setTabListener(tabListener);  <------103rd Line Null pointer exception occurred here.
        actionabar.addTab(tab);
        tab = actionabar.newTab().setText(getString(R.string.str_video))
                .setTabListener(tabListener);
        actionabar.addTab(tab);
        tab = actionabar.newTab().setText(getString(R.string.str_gal))
                .setTabListener(tabListener);
        actionabar.addTab(tab);
        tab = actionabar.newTab().setText(getString(R.string.str_announcements))
                .setTabListener(tabListener);
        actionabar.addTab(tab);

        tab = actionabar.newTab()
                .setText(getString(R.string.str_magazines))
                .setTabListener(tabListener);
        actionabar.addTab(tab);

        tab = actionabar.newTab().setText(getString(R.string.str_conduct))
                .setTabListener(tabListener);
        actionabar.addTab(tab);  

    }

的strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">The Fathers House</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="str_home">HOME</string>
    <string name="str_video">VIDEO</string>
    <string name="str_announcements">ANNOUNCEMENTS</string>
    <string name="str_magazines">MAGAZINES</string>
    <string name="str_gal">GALLERY</string>
    <string name="str_conduct">CONDUCT</string>
    <string name="str_back">Back</string>
    <string name="api_host">http://tfhapp.fathershouse.in/api/</string>
    <string name="description"> This app would enrich you with the power of grace in the life of a believer and would change your life forever. God bless you</string>
    <string name="copyright">&#169; Splendio.com 2014. All rights reserved.</string>
    <string name="text">#FFFFFF</string>

</resources>

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.fth.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:name="com.sit.fth.app.GemsApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.sit.fth.activity.SplashActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.sit.fth.activity.HomeActivity"
            android:screenOrientation="portrait" >
        </activity>
        <activity android:name="com.sit.fth.activity.YoutubePlayActivity" >
        </activity>
    </application>

</manifest>

我不知道如何解决这些空指针异常。如果你知道如何解决这些问题,任何人都可以帮助我。谢谢。

2 个答案:

答案 0 :(得分:1)

getActionBar()返回null。似乎您已应用非操作栏主题。

确保您应用操作栏主题

android:theme="@android:style/Theme.Holo.Light"

答案 1 :(得分:0)

您尚未设置ActionBar,因此当您致电getActionBar();时,它会返回null。

您可以使用以下代码访问或获取ActionBar

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

另外,请确保您的主题未在XML布局中设置为Theme.NOTITLE,因为ActionBar需要主题或带有应用标题的活动。