我正在开发一个应用程序,我想在splashscreen期间将一些数据存储在sharedPreference中,然后在主活动加载后读取该数据。如果我从MainActivity保存数据,数据将被保存,我可以将其取回。但是,如果我试图在splashscreen中保存数据,我会得到nulpointer的重复。这是一个例外:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at com.example.amandeepsingh.loginregisterandother.SharedPreferncesExecutor.save(SharedPreferncesExecutor.java:22)
at com.example.amandeepsingh.loginregisterandother.SplashScreenActivityWithAsyncTask$PrefetchData.doInBackground(SplashScreenActivityWithAsyncTask.java:79)
at com.example.amandeepsingh.loginregisterandother.SplashScreenActivityWithAsyncTask$PrefetchData.doInBackground(SplashScreenActivityWithAsyncTask.java:36)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
以下是我的示例代码:
SplashScreenActivityWithAsyncTask:
public class SplashScreenActivityWithAsyncTask extends Activity{
//String now_playing, earned;
private SharedPreferncesExecutor sharedPreference;
Activity context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
/**
* Showing splashscreen while making network calls to download necessary
* data before launching the app Will use AsyncTask to make http call
*/
Activity context = this;
new PrefetchData().execute();
}
/**
* Async Task to make http call
*/
private class PrefetchData extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// before making http calls
}
@Override
protected Void doInBackground(Void... arg0) {
sharedPreference = new SharedPreferncesExecutor();
sharedPreference.save(context,"SchoolData","Updated data from Splash Screen");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// After completing http call
// will close this activity and lauch main activity
Intent i = new Intent(SplashScreenActivityWithAsyncTask.this, MainActivity.class);//Here Main activity is the splash screen.
//i.putExtra("now_playing", now_playing);
//i.putExtra("earned", earned);
startActivity(i);
// close this activity
finish();
}
}
}
请帮忙。
答案 0 :(得分:3)
您的班级成员context
为空,因为在onCreate
中,声明
Activity context = this;
您隐藏了成员类的可见性,有利于本地成员。为避免歧义,您可以使用NomeOfYourActivity.this
(在您的情况下为SplashScreenActivityWithAsyncTask.this
),而无需指定任何引用。在您的AsyncTask中更改
sharedPreference.save(context,
到
sharedPreference.save(SplashScreenActivityWithAsyncTask.this,
答案 1 :(得分:1)
您在onCreate中声明了一个局部变量,并为其指定了活动的值。不修改成员变量上下文。它保持为空,你得到一个NPE。 无需创建任何成员变量。你可以这样做 而不是
sharedPreference.save(context,"SchoolData","Updated data from Splash Screen");
使用
sharedPreference.save(SplashScreenActivityWithAsyncTask.this, "SchoolData",
"Updated data from Splash screen");
异步任务是SplashScreenActivityWithAsyncTask的非静态内部类,因此它已经有对活动的引用。