切换android活动时出现空指针异常

时间:2014-04-09 10:19:35

标签: java android nullpointerexception android-activity splash-screen

我正在开发一个代码,用于在启动画面后运行视图寻呼机。

我编写了连接2个活动的代码,但是当我运行程序时,我一直收到nullPointerException。

第一项活动:

package lk.sidadiya.sidadiya;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class SplashScreen extends Activity {

    // Splash screen timer
    private static int SPLASH_TIME_OUT = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        new Handler().postDelayed(new Runnable() {

            /*
             * Showing splash screen with a timer. This will be useful when you
             * want to show case your app logo / company
             */

            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity
                Intent i = new Intent(SplashScreen.this, WelcomeScreen.class);
                startActivity(i);

                // close this activity
                finish();
            }
        }, SPLASH_TIME_OUT);
    }

}

第二项活动:

package lk.sidadiya.sidadiya;

import lk.sidadiya.tabswipe.adapter.TabsPagerAdapter;
import lk.sidadiya.sidadiya.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;


public class WelcomeScreen 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_screen);

        // 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) {
    }

}

清单:

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
         <!-- Splash screen -->
        <activity
            android:name="lk.sidadiya.sidadiya.SplashScreen"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="lk.sidadiya.sidadiya.WelcomeScreen"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Main activity -->

        <activity
            android:name="lk.sidadiya.sidadiya.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Log cat:

04-09 15:30:40.405: D/dalvikvm(14241): GC_FOR_ALLOC freed 53K, 5% free 8068K/8451K, paused 41ms, total 41ms
04-09 15:30:40.435: D/scheduleTraversals(14241): mTraversalScheduled:false
04-09 15:30:40.445: D/dalvikvm(14241): GC_CONCURRENT freed 4K, 5% free 8522K/8903K, paused 2ms+7ms, total 35ms
04-09 15:30:40.445: D/scheduleTraversals(14241): mTraversalScheduled:true
04-09 15:30:40.445: D/performTraversals(14241): mAdded:true
04-09 15:30:40.455: D/ViewRootImpl(14241): relayoutWindow()-viewVisibility: 0
04-09 15:30:40.495: I/Adreno200-EGL(14241): <qeglDrvAPI_eglInitialize:294>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_RB3.04.01.01.13.044_msm8960_JB_REL_RB3.1_CL3449569_release_AU (CL3449569)
04-09 15:30:40.495: I/Adreno200-EGL(14241): Build Date: 05/13/13 Mon
04-09 15:30:40.495: I/Adreno200-EGL(14241): Local Branch: 
04-09 15:30:40.495: I/Adreno200-EGL(14241): Remote Branch: m/jb_rel_rb3.1
04-09 15:30:40.495: I/Adreno200-EGL(14241): Local Patches: NONE
04-09 15:30:40.495: I/Adreno200-EGL(14241): Reconstruct Branch: AU_LINUX_ANDROID_JB_REL_RB3.04.01.01.13.044 + df3bc97 +  NOTHING
04-09 15:30:40.555: D/scheduleTraversals(14241): mTraversalScheduled:false
04-09 15:30:40.555: D/scheduleTraversals(14241): mTraversalScheduled:true
04-09 15:30:40.566: D/scheduleTraversals(14241): mTraversalScheduled:true
04-09 15:30:40.566: D/performTraversals(14241): mAdded:true
04-09 15:30:43.489: W/dalvikvm(14241): threadid=1: thread exiting with uncaught exception (group=0x40c81450)
04-09 15:30:43.489: E/AndroidRuntime(14241): FATAL EXCEPTION: main
04-09 15:30:43.489: E/AndroidRuntime(14241): java.lang.RuntimeException: Unable to start activity ComponentInfo{lk.sidadiya.sidadiya/lk.sidadiya.sidadiya.WelcomeScreen}: java.lang.NullPointerException
04-09 15:30:43.489: E/AndroidRuntime(14241):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2065)
04-09 15:30:43.489: E/AndroidRuntime(14241):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2090)
04-09 15:30:43.489: E/AndroidRuntime(14241):    at android.app.ActivityThread.access$600(ActivityThread.java:136)
04-09 15:30:43.489: E/AndroidRuntime(14241):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
04-09 15:30:43.489: E/AndroidRuntime(14241):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 15:30:43.489: E/AndroidRuntime(14241):    at android.os.Looper.loop(Looper.java:137)
04-09 15:30:43.489: E/AndroidRuntime(14241):    at android.app.ActivityThread.main(ActivityThread.java:4800)
04-09 15:30:43.489: E/AndroidRuntime(14241):    at java.lang.reflect.Method.invokeNative(Native Method)
04-09 15:30:43.489: E/AndroidRuntime(14241):    at java.lang.reflect.Method.invoke(Method.java:511)
04-09 15:30:43.489: E/AndroidRuntime(14241):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
04-09 15:30:43.489: E/AndroidRuntime(14241):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
04-09 15:30:43.489: E/AndroidRuntime(14241):    at dalvik.system.NativeStart.main(Native Method)
04-09 15:30:43.489: E/AndroidRuntime(14241): Caused by: java.lang.NullPointerException
04-09 15:30:43.489: E/AndroidRuntime(14241):    at lk.sidadiya.sidadiya.WelcomeScreen.onCreate(WelcomeScreen.java:33)
04-09 15:30:43.489: E/AndroidRuntime(14241):    at android.app.Activity.performCreate(Activity.java:5008)
04-09 15:30:43.489: E/AndroidRuntime(14241):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-09 15:30:43.489: E/AndroidRuntime(14241):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2029)
04-09 15:30:43.489: E/AndroidRuntime(14241):    ... 11 more

2 个答案:

答案 0 :(得分:3)

您的Manifest.xml文件中存在问题。您已将两个活动定义为Launcher。使用以下代码更改Manifest.xml,然后重试

   <activity
        android:name="lk.sidadiya.sidadiya.SplashScreen"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Black.NoTitleBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="lk.sidadiya.sidadiya.WelcomeScreen"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
    </activity>

答案 1 :(得分:0)

您必须指定哪个是您的默认启动器活动。因此您必须更改清单文件

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
         <!-- Splash screen -->
        <activity
            android:name="lk.sidadiya.sidadiya.SplashScreen"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT"
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="lk.sidadiya.sidadiya.WelcomeScreen"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Main activity -->

        <activity
            android:name="lk.sidadiya.sidadiya.MainActivity"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>