[英]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
. 我正在尝试使用内置的android的Application.class ,但是每次我要使用它时,都会收到
NullPointerException
。 I'm going to share some pieces of code to show how I'm accessing my custom class, which extends Application
: 我将分享一些代码,以展示如何访问自定义类,该类
extends 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
? 我访问它的方式是否错误,或者我错过了
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. 将
android:name
属性放在<application>
条目中,该条目包含您的所有活动,而不是作为单独的实体。 Change it to be the fully-qualified name of the class (eg, com.this.is.your.package.SharedPreferences
). 将其更改为该类的标准名称(例如
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. 首先,您的Application类的名称是
SharedProperties
,而不是MyApp
,因此应将其强制转换为该名称。
Secondly, in order for Android to know to use your custom Application, you need to say so in your AndroidManifest.xml
like so: 其次,为了使Android知道使用您的自定义应用程序,您需要在
AndroidManifest.xml
这样说:
<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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.