我是android新手。我正在开发一个应用程序,其中我有两个选项卡。首先,我想从服务器加载数据,并将它们保存在共享首选项中,并在列表视图中显示。如果共享偏好中存在数据,则首先从共享首选项显示数据,然后再次从服务器加载数据并替换相同的数据。两个选项卡包含来自同一网址的不同数据。数据在列表视图中正确显示,但问题是当数据从服务器加载时,当数据仍然从服务器丢失时,当我返回主活动时,应用程序在共享偏好时崩溃并出现空指针异常。
以下是我的代码
public class Tab1 extends Fragment {
ListView ll;
Pojo pojo;
ListView dosList;
ProgressDialog nDialog;
String strServerResponse = null;
ArrayList<Pojo> dos;
DosAdapter dosAdapter;
SharedPreferences MyPrefs1;
ProgressBar nPregress;
ArrayList<String> tii1;
LayoutInflater inf;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab_1, container, false);
return v;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
dosList = (ListView) view.findViewById(R.id.doList);
nPregress = (ProgressBar) getActivity().findViewById(R.id.toolbar_progress_bar);
nPregress.setVisibility(View.GONE);
dos = new ArrayList<Pojo>();
SharedPreferences prefs = getActivity().getSharedPreferences("MyPref1", 0);
Set<String> set = prefs.getStringSet("does", null);
if (set != null) {
for (String p : set) {
pojo = new Pojo();
pojo.setType(p);
dos.add(pojo);
}
dosAdapter = new DosAdapter(getContext(), dos);
dosList.setAdapter(dosAdapter);
} else {
new NetCheck(getContext()).execute();
}
new NetCheck(getContext()).execute();
}
private class NetCheck extends AsyncTask<Void, Void, Void> {
private Context context;
public NetCheck(Context context)
{
this.context=context;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
nPregress.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dos.clear();
nPregress.setVisibility(View.GONE);
SharedPreferences prefs = getActivity().getSharedPreferences("MyPref1", 0);
Set<String> set = prefs.getStringSet("does", null);
for (String p : set) {
pojo = new Pojo();
pojo.setType(p);
dos.add(pojo);
}
dosAdapter = new DosAdapter(getContext(), dos);
dosList.setAdapter(dosAdapter);
return;
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpRequest = new HttpPost(
"http://tipseducation.com/system/eadmin/getdoes_n_doesnot/");
//"http://techie-web.com/android/testm/gcm_server_php/dosndonts.php");
//"http://192.168.56.1:8080/Project/categories.php");
httpRequest.setHeader("Content-Type", "application/json");
JSONObject json = new JSONObject();
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding("UTF-8");
se.setContentType("application/json");
httpRequest.setEntity(se);
HttpResponse httpRes = httpClient.execute(httpRequest);
java.io.InputStream inputStream = httpRes.getEntity()
.getContent();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
inputStream.close();
strServerResponse = sb.toString();
Log.e("Server Response", "" + strServerResponse.toString());
if (strServerResponse != null) {
try {
JSONArray arr = new JSONArray(strServerResponse);
JSONObject jsonObj = arr.getJSONObject(0);
tii1 = new ArrayList<String>();
for (int i = 0; i < arr.length(); i++) {
pojo = new Pojo();
JSONObject jobj2 = arr.getJSONObject(i);
String does = jobj2.optString("does");
tii1.add(does);
}
List<String> listTemp1 = tii1;
Set<String> temp1 = new HashSet<String>(listTemp1);
////////////////////////Crashhhhhhhhhhhh///////////////////////////////
SharedPreferences.Editor editor5 = context.getSharedPreferences("MyPref1", 0).edit();
temp1.addAll(listTemp1);
editor5.putStringSet("does", temp1);
editor5.commit();
} catch (JSONException e) {
e.printStackTrace();
}
代码中是否有问题。另一个选项卡的代码也是如此。任何人都可以帮助我
logcat的
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at com.example.priyanka.newdentalapp.Tab2$NetCheck.doInBackground(Tab2.java:192)
at com.example.priyanka.newdentalapp.Tab2$NetCheck.doInBackground(Tab2.java:83)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
答案 0 :(得分:0)
在asynctask类中,您必须创建如下构造函数:
private Context context;
public NetCheck(Context context)
{
this.context=context;
}
在此之后,您必须使用上下文更改所有getActivity()行。就是这样。尝试这个,它会工作。