Search code examples
androidandroid-application-class

Extended Application can not run correctly


everyone. I am new to android. Recently, I tried to learn how to extend Application, but there is a strange problem. I have tried a lot of methods, but still can not solve it. So, thanks in advance if anyone can help me.

The code section is like this:

package com.example.xw.myfirstapplication;

import android.app.Application;

/**
 * Created by Administrator on 2016/8/26.
 */
public class MyApplication extends Application {
    public static MyApplication mApplication;

    @Override
    public void onCreate(){
    super.onCreate();
    mApplication = this;

    }

    public static MyApplication getMyApplication(){
        return mApplication;
    }
}

And, Manifest.xml is like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.xw.myfirstapplication"
    >
    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="24" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application android:name="com.example.xw.myfirstapplication.MyApplication"

        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        >
        <!--<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>-->
        <!--<uses-permission android:name="android.permission.INTERNET"></uses-permission>-->
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--<activity android:name="testNewTech.DisplayMessageActivity" />
        <activity android:name="testNewTech.NotificationActivity"></activity>-->
    </application>

</manifest>

But, when I run the code, something happens very quickly, like this:

enter image description here

In addition: 1. I tried not to use extended Application but default Application, everything is good.

  1. I add Log.v("MainActivity", "start onCreate"); in method onCreate() of class MainActivity. But, there is nothing in logcat, so I think the method onCreate() of class MainActivity is not processed.

So, anyone has any advice? Thank you so much!


Solution

  • If you can see in your manifest.xml file then you can pass another class

    <application android:name="com.example.xw.myfirstapplication.MyApplication"
    ...
    ...
    >
    

    just change

    <application android:name="com.example.xw.myfirstapplication.Application"
    ...
    ...
    >