我解析了json,我可以在画廊中用paseadapter显示我的json'components。 我也有setOnItemSelectedListener方法,int这个方法我按位置改变了背景图像,我也有这个方法的线程(1000毫秒)和progressdialog。当我点击我的listview时,我有零点异常.progressdialog是我的问题 这是我的来源
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();
}
}
}
我在做错了什么?如果有人知道解决方案,请帮助我 感谢答案 0 :(得分:0)
快速谷歌搜索`android nullpointerexception解决对话框主题``给我这个:
NullPointerException when creating a new Dialog
如果有帮助,请记住也要回答他的答案。
另外你为什么要在你的线程中睡觉?你知道睡觉时没有做任何计算,对吗?
我会为它创建一个AsyncTask,你显然知道如何使用它。然后,您还可以在onPreExecute
中显示进度对话框,并在onPostExecute
中将其关闭。我认为更清洁。