需要有关Application类的解释

时间:2011-08-03 18:47:50

标签: java android class android-manifest share

我正在尝试使用内置的android Application.class,但每次我想使用它时,我都会得到NullPointerException。我将分享一些代码来展示我如何访问我的自定义类extends Application

这是我正在使用的课程:

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;
    }

}

...以及我如何设置并从中获取值:

    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();

我访问它的方式是错误的还是我错过了SharedProperties.class

这是我的清单:

<?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>

3 个答案:

答案 0 :(得分:2)

android:name属性放在包含所有活动的<application>条目中,而不是作为单独的实体。将其更改为班级的完全限定名称(例如com.this.is.your.package.SharedPreferences)。

另外,不要这样做。使用singleton

答案 1 :(得分:1)

对于初学者来说,Application类的名称是SharedProperties,而不是MyApp,所以它应该被强制转换为。{/ p>

其次,为了让Android知道使用您的自定义应用,您需要在AndroidManifest.xml中这样说:

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

答案 2 :(得分:0)

我有类似的问题,不记得我做了什么,但尝试添加

public SharedProperties()
{

        super();
}
相关问题