Я пытаюсь использовать встроенный в 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>