i parsed json and i can show my json'components in gallery,with paseadapter. also i have setOnItemSelectedListener method,int this method i changed background image by position and also i have thread in this method (1000 milliseconds) and progressdialog. and somethimes when i click my listview i have nullpoint exception .progressdialog is my problem this is a my source
public class MainmoviesList extends Fragment {
public final static String TAG = MainmoviesList.class.getSimpleName();
String imageurl = "******";
public static List<ServerItems> arrayOfList;
public static Gallery main_listview;
private AzercellMainPageAdapter objAdapter;
private RelativeLayout mSwitcher;
private CustomerStatistic cs;
private String apisha_url = "*********";
public static int mPosition;
private ServerItems objItem;
public static MainmoviesList newInstance() {
return new MainmoviesList();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main_movies_list, container,
false);
mSwitcher = (RelativeLayout) rootView.findViewById(R.id.rootNode);
main_listview = (Gallery) rootView
.findViewById(R.id.horizontallistview);
arrayOfList = new ArrayList<ServerItems>();
cs = new CustomerStatistic();
cs.execute(apisha_url);
main_listview.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
mPosition = position;
final ProgressDialog ringProgressDialog = ProgressDialog.show(
getActivity(), "Please wait ...",
"Downloading Image ...", true);
ringProgressDialog.setCancelable(true);
new Thread(new Runnable() {
@Override
public void run() {
try {
loadimagePosition(mPosition);
Thread.sleep(1000);
} catch (Exception e) {
}
ringProgressDialog.dismiss();
}
}).start();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
main_listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mPosition = position;
MoviewListResult newFragment = new MoviewListResult();
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("image", arrayOfList.get(position)
.getBlurimage());
bundle.putString("title", arrayOfList.get(position).getTitle());
bundle.putString("trailer", arrayOfList.get(position)
.getYoutube());
bundle.putString("category", arrayOfList.get(position)
.getCategory());
bundle.putString("writer", arrayOfList.get(position)
.getWritten());
bundle.putString("stars", arrayOfList.get(position).getStars());
bundle.putString("descraption", arrayOfList.get(position)
.getDescraption());
bundle.putString("time", arrayOfList.get(position).getTime());
newFragment.setArguments(bundle);
transaction.replace(R.id.content_frame, newFragment);
transaction.commit();
}
});
return rootView;
}
private class CustomerStatistic extends AsyncTask<String, Void, String> {
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setCancelable(false);
pDialog.show();
pDialog.setContentView(R.layout.custom_progressdialog);
}
@Override
protected String doInBackground(String... params) {
return Utils.getJSONString(params[0]);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONArray mainJson = new JSONArray(result);
for (int i = 0; i < mainJson.length(); i++) {
JSONObject objJson = mainJson.getJSONObject(i);
objItem = new ServerItems();
objItem.setImage(imageurl + objJson.getString("ipone_4"));
objItem.setTitle(objJson.getString("title"));
objItem.setYoutube(objJson.getString("youtube"));
objItem.setWritten(objJson.getString("written"));
objItem.setCategory(objJson.getString("category"));
objItem.setDescraption(objJson.getString("descraption"));
objItem.setStars(objJson.getString("stars"));
objItem.setBlurimage(imageurl
+ objJson.getString("ipone_4_blur"));
JSONObject cinema = objJson.getJSONObject("Cinemas");
JSONArray cinemasarray = cinema.getJSONArray("Cinemaname");
for (int j = 0; j < cinemasarray.length(); j++) {
JSONObject objJson1 = cinemasarray.getJSONObject(j);
JSONArray info = objJson1.getJSONArray("info");
for (int k = 0; k < info.length(); k++) {
JSONObject information = info.getJSONObject(k);
objItem.setTime(information.getString("time"));
Log.e("time is", objItem.getTime());
}
}
arrayOfList.add(objItem);
}
} catch (JSONException e) {
e.printStackTrace();
}
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
setAdapterToListview();
}
}
public void setAdapterToListview() {
objAdapter = new AzercellMainPageAdapter(getActivity(),
R.layout.azercell_main_page_adapter, arrayOfList);
main_listview.setAdapter(objAdapter);
}
public void loadimagePosition(int pos) {
URL url;
try {
url = new URL(arrayOfList.get(pos).getBlurimage());
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
Drawable drawable = new BitmapDrawable(bmp);
mSwitcher.setBackgroundDrawable(drawable);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
what am i doing wrong? if anyone knows solution please help me thanks
A quick google search `android nullpointerexception resolve dialog theme``gave me this:
NullPointerException when creating a new Dialog
Remember to upvote his answer as well if it helps.
Also why do you want to sleep in your thread? You are aware that it is not doing any computation while sleeping, right?
I would create an AsyncTask for that aswell, you obviously know how to use it. Then you can also show the progress dialog in onPreExecute
and dismiss it in onPostExecute
. Much cleaner in my opinion.