扩展BaseActivity,在super.oncreate

时间:2018-02-12 19:29:01

标签: android

BaseActivity.java

    public class BaseActivity extends Activity{
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            DMCApplication.getInstance().addActivity(this);
        }

        @Override
        protected void onDestroy() {
            DMCApplication.getInstance().removeActivity(this);
            super.onDestroy();
        }
    }

SplashScreenActivity.java

    public class SplashScreenActivity extends BaseActivity {

        private Handler handler = new Handler();

        private static final int sDelayTime = 2000;

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

            startService();
            handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    startMainActivity();
                }
            }, sDelayTime);
        }

        private void startMainActivity() {
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(intent);
            finish();
        }

        private void startService() {

        }
}

DMCApplication.java

public class DMCApplication extends Application {

    private static DMCApplication mDmcApplication;

    private List<Activity> activities;

    public ControlPoint mControlPoint;

    @Override
    public void onCreate() {
        super.onCreate();
        if (activities != null) {
            activities = null;
        }
        activities = new ArrayList<Activity>();
        mDmcApplication = this;
    }


    public void addActivity(Activity activity) {
        if (activities != null) {
            if (activities.contains(activity)) {
                activities.remove(activity);
            }
            activities.add(activity);
        }
    }


    public void removeActivity(Activity activity) {
        if (activities != null && activities.contains(activity)) {
            activities.remove(activity);
        }
    }


    public static DMCApplication getInstance() {
        return mDmcApplication;
    }


    public void quit() {
        if (activities != null) {
            for (Activity activity : activities) {
                activity.finish();
            }
            activities = null;
        }
    }


    public void setControlPoint(ControlPoint controlPoint) {
        mControlPoint = controlPoint;
    }


    public ControlPoint getControlPoint() {
        return mControlPoint;
    }
}

logcat的

java.lang.RuntimeException:无法启动活动ComponentInfo { com.abc.xyx / com.abc.xyx.actvities.SplashScreenActivity}:

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void com.abc.xyx.DMCApplication.addActivity(android.app.Activity)'                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)                                                        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3046)                                                        在android.app.ActivityThread.-wrap11(未知来源:0)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1688)                                                        在android.os.Handler.dispatchMessage(Handler.java:105)                                                        在android.os.Looper.loop(Looper.java:164)                                                        在android.app.ActivityThread.main(ActivityThread.java:6809)                                                        at java.lang.reflect.Method.invoke(Native Method)                                                        在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)                                                     引发者:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void com.agileapps.dlnaupnp.DMCApplication.addActivity(android.app.Activity)'                                                        在com.abc.xyx.actvities.BaseActivity.onCreate(BaseActivity.java:14)                                                        在com.abc.xyx.actvities.SplashScreenActivity.onCreate(SplashScreenActivity.java:22)                                                        在android.app.Activity.performCreate(Activity.java:6998)                                                        在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1230)                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2899)

1 个答案:

答案 0 :(得分:0)

您尚未在DMCApplication中注册Manifest。 你必须做这样的事情

 <application
    android:name=".DMCApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"

P.S。名称应完全合格。