Android Apps-don't store data in the application class

Source: Internet
Author: User

Foreword: Recently found in the development of a more serious problem, when we will apply the home button into the background to run, after a period of time, when we open the application again, nine times there will be a nullpointexception null pointer exception, according to the Logcat log, is going to go global to a variable, what's the reason? It turns out that we put a lot of data into the application as a global variable, causing the problem, and here's why we can't put the data in application.
I. Introduction to the Application classapplication and Activity,service are a system component of the Android framework, and when the Android program starts, a Application object is created to store some information about the system. In general, the system creates a application class by default, and we do not need to do anything in it, and the program is created automatically. When you need to create your own application, it's easy to create a class that inherits application and registers it in the Application tab of manifest ( Just add a name attribute to the application tag to set the name of your application.
<application        android:name= "com.example.applicationdemo.MyApplication"        android:allowbackup= "true"        android:icon= "@drawable/ic_launcher"        android:label= "@string/app_name"        android:theme= "@style/ Apptheme ">
where myThe application class is a class that we customize, inheriting from the application class
public class MyApplication extends application {public String name;public string GetName () {return name;} public void SetName (String name) {this.name = name;} @Overridepublic void OnCreate () {//TODO auto-generated method Stubsuper.oncreate ();}}

Second, why can't store data in application
Let's look at a simple example:we write a method of a set get variable in the application inheriting class, and then use the first activity to set the value of the variable by using the application set method, which is obtained in another activity. and convert it to uppercase to display it. The code is as follows:mainactivity:
public class Mainactivity extends Activity {private MyApplication application;private Button btnname; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Main); application = (MyApplication) getapplication (); Application.setname ("Yangliang"); btnname = (Button) Findviewbyid (R.id.btn_name); Btnname.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) { Intent Intent = new Intent (mainactivity.this, Shownameactivity.class); startactivity (Intent);}});}}
MyApplication class
public class MyApplication extends application {public String name;public string GetName () {return name;} public void SetName (String name) {this.name = name;} @Overridepublic void OnCreate () {//TODO auto-generated method Stubsuper.oncreate ();}}
Another activity, show content
public class Shownameactivity extends Activity {private myapplication app;private TextView TV; @Overrideprotected void OnC Reate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.showname); app = (MyApplication) getapplication (); String name = App.getname (); TV = (TextView) Findviewbyid (r.id.tv_showname); Tv.settext (Name.tolowercase ());}}
After opening the app, enter the Shownameactivity interface, then press the home key to enter the background, after a period of time, open the demo again, there is a null pointer exception
error log in Logcat
Navigate to this code:
Tv.settext (Name.tolowercase ());
name is empty at this time.
What is the reason for this? because when the application is running in the background, when we cut the application into the background or when the memory is low, the system may kill the application process currently applied,when we switch the app to the foreground again from the background, the system regenerates a application class, which we call in the displayed activity
App = (myapplication) getapplication (); String name = App.getname (); Tv.settext (Name.tolowercase ());
The above name is empty, so there will be null pointer exception, that is, we stored in the application data, it is possible because the program runs in the background, resulting in application destruction and reconstruction resulting in the loss of data, which is fatal to the program, A direct result of a null pointer exception occurs, the program crashes.

third, what is the better way? 1, through the intent to pass data, rather than put the data in the global variable application, of course, this is limited, not all places are suitable for intent to pass data, and not all types of data are suitable for intent to pass, For data transmission of intent, readers can refer to other materials.
2. Persist data, write files, shareprefrence, database and so on to save data safely. Then file read operations where data is needed.
3, in all need to use such data where the non-empty judgment, and then the corresponding operation.
Iv. Summary: Do not easily in the application class data storage operations, application class should do is to do some global configuration initialization operation, and data storage should use the previous recommended methods.
demo:http://download.csdn.net/detail/yanglfree/7709955

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.