开发者

Need an explanation about Application class

I'm trying to use build-in android's Application.class, but every time I want to use it, I get a NullPointerException. I'm going to share some pieces of code to show how I'm accessing my custom class, which exte开发者_JAVA百科nds Application:

This is the class I'm using:

public class SharedProperties extends Application {

    private String currentCategory;
    private String dataNews;

    public SharedProperties() {
        super();
    }

    public String getCurrentCategory() {
        return currentCategory;
    }

    public void setCurrentCategory(String currentCategory) {
        this.currentCategory = currentCategory;
    }

    public String getDataNews() {
        return dataNews;
    }

    public void setDataNews(String dataNews) {
        this.dataNews = dataNews;
    }

}

...and this how I set and get values from it:

    SharedProperties shared = ((SharedProperties)getApplication());
    shared.setCurrentCategory(categories[currentCategory]);
    shared.setDataNews(rssList.get(position).getData());

    ......

    SharedProperties shared = ((SharedProperties)getApplication());
    String category = shared.getCurrentCategory();
    String newstext = shared.getDataNews();

Is the way I'm accessing it wrong or I miss something into the SharedProperties.class?

Here is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.news.reader"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />

    <application android:name="SharedProperties" android:icon="@drawable/logo" android:label="@string/app_name">
        <activity android:name="Splash"
            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="Home"
            android:label="@string/app_name"
            android:screenOrientation="sensor"
            android:configChanges="keyboardHidden|orientation" />
        <activity android:name="News"
            android:label="@string/app_name"
            android:screenOrientation="sensor"
            android:configChanges="keyboardHidden|orientation" />
    </application>

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

</manifest>


Put the android:name attribute in the <application> entry which contains all your activities, not as a separate entity. Change it to be the fully-qualified name of the class (e.g., com.this.is.your.package.SharedPreferences).

Also, don't do this. Use a singleton. Please.


For starters, the name of your Application class is SharedProperties, not MyApp, so it should be cast to that.

Secondly, in order for Android to know to use your custom Application, you need to say so in your AndroidManifest.xml like so:

<application
    android:name="SharedProperties"
    android:icon="@drawable/icon"
    android:label="@string/app_name">


I had similar problem, don exactly remember what i did , but do try adding

public SharedProperties()
{

        super();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜