mazimazi
دوشنبه 11 آبان 1394, 20:57 عصر
من یه اکتیویتی دارم و چند تا فرگمنت از داخل اکتیویتی اکشن بار و اسلاید منو رو فراخونی میکنم برای بار اول برنامه اجرا میشه دکمه ها کار میکنه ولی وقتی بین فرگمنت ها پیمایش میکنم از کار میفته وقتی هم داخل فرگمنت ها لیسنر مینویسم اررور nullpointerexeption میگیره .کد ها رو قرار میدم.
ActivityMain :
public class ActivityMain extends ActionBarActivity {
private static long back_pressed = 0L;
private Typeface typeface;
private Typeface typeface2;
private Typeface typeface3;
private Typeface typeface4;
private TextView txt_ms_void;
private TextView txt_english_msvoid;
private TextView txt_home;
private TextView txt_fav;
private TextView txt_bazar;
private TextView txt_details;
private TextView txt_exit;
private ImageView img_cafebazar_slide_menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.root);
/****************************************/
final View actionBarView = G.inflater.inflate(R.layout.action_bar, null);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_M ODE_STANDARD);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW _CUSTOM);
actionBar.setCustomView(actionBarView, new ActionBar.LayoutParams
(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
ImageView btnSearch = (ImageView) findViewById(R.id.btnSearch);
ImageView btnFav = (ImageView) findViewById(R.id.btnFav);
btnSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(G.currentActivity, ActivitySearch.class);
G.currentActivity.startActivity(intent);
}
});
btnFav.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(G.currentActivity, ActivityFavSerials.class);
G.currentActivity.startActivity(intent);
}
});
/************************************************** *********/
final DrawerLayout drawerlayout = (DrawerLayout) findViewById(R.id.drawerlayout);
txt_ms_void = (TextView) drawerlayout.findViewById(R.id.txt_ms_void);
txt_english_msvoid = (TextView) drawerlayout.findViewById(R.id.txt_english_msvoid) ;
txt_home = (TextView) drawerlayout.findViewById(R.id.txt_home);
txt_fav = (TextView) drawerlayout.findViewById(R.id.txt_fav);
txt_bazar = (TextView) drawerlayout.findViewById(R.id.txt_bazar);
txt_details = (TextView) drawerlayout.findViewById(R.id.txt_details);
txt_exit = (TextView) drawerlayout.findViewById(R.id.txt_exit);
typeface = typeface.createFromAsset(getAssets(), "arshia.ttf");
typeface2 = typeface.createFromAsset(getAssets(), "A.Arghavan.ttf");
typeface3 = typeface.createFromAsset(getAssets(), "SultanNormal_ufont.ir.ttf");
typeface4 = typeface.createFromAsset(getAssets(), "HandPrintingPress_Meshed_demo.ttf");
txt_ms_void.setTypeface(typeface3);
txt_english_msvoid.setTypeface(typeface4);
txt_home.setTypeface(typeface);
txt_fav.setTypeface(typeface);
txt_bazar.setTypeface(typeface2);
txt_details.setTypeface(typeface);
txt_exit.setTypeface(typeface);
txt_home.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
final Button btnMenu1 = (Button) actionBarView.findViewById(R.id.btnMenu1);
if (drawerlayout.isDrawerOpen(Gravity.RIGHT)) {
drawerlayout.closeDrawer(Gravity.RIGHT);
btnMenu1.setBackgroundResource(R.drawable.slide_me nue);
Animation animation = AnimationUtils.loadAnimation(ActivityMain.this, R.anim.rotate);
btnMenu1.setAnimation(animation);
} else {
drawerlayout.openDrawer(Gravity.RIGHT);
btnMenu1.setBackgroundResource(R.drawable.slide_me nue_horozintal);
Animation animation = AnimationUtils.loadAnimation(ActivityMain.this, R.anim.rotate_back);
btnMenu1.setAnimation(animation);
}
}
});
txt_fav.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(ActivityMain.this, ActivityFavSerials.class);
ActivityMain.this.startActivity(intent);
//ActivitySerials.this.finish();
}
});
img_cafebazar_slide_menu = (ImageView) drawerlayout.findViewById(R.id.img_cafebazar_slide _menu);
Animation animation = AnimationUtils.loadAnimation(ActivityMain.this, R.anim.cafebazar_in_slidemenu);
img_cafebazar_slide_menu.setAnimation(animation);
txt_details.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(ActivityMain.this, Activity_details.class);
ActivityMain.this.startActivity(intent);
//ActivitySerials.this.finish();
ActivityMain.this.overridePendingTransition(R.anim .slide_down, android.R.anim.fade_out);
}
});
final Button btnMenu1 = (Button) actionBarView.findViewById(R.id.btnMenu1);
btnMenu1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (drawerlayout.isDrawerOpen(Gravity.RIGHT)) {
drawerlayout.closeDrawer(Gravity.RIGHT);
btnMenu1.setBackgroundResource(R.drawable.slide_me nue);
Animation animation = AnimationUtils.loadAnimation(ActivityMain.this, R.anim.rotate);
btnMenu1.setAnimation(animation);
} else {
drawerlayout.openDrawer(Gravity.RIGHT);
btnMenu1.setBackgroundResource(R.drawable.slide_me nue_horozintal);
Animation animation = AnimationUtils.loadAnimation(ActivityMain.this, R.anim.rotate_back);
btnMenu1.setAnimation(animation);
}
}
});
txt_exit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
android.os.Process.killProcess(android.os.Process. myPid());
System.exit(1);
}
});
/************************************************** ***********/
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
PagerAdapter adapter = new PagerAdapter(this, getSupportFragmentManager());
viewPager.setAdapter(adapter);
} این هم یکی از فرگمنت ها :
public class FragmentHome extends Fragment {
private ViewGroup layout;
private static ListView list;
public static AdapterSerials adapter;
private static TextView txtTest;
public static ArrayList<StructApps> apps= new ArrayList<StructApps>();
public ProgressDialog mProgressDialog;
public String lastFilter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
layout = (ViewGroup) inflater.inflate(R.layout.fragment_horror, null);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().buil d();
StrictMode.setThreadPolicy(policy);
list = (ListView) layout.findViewById(R.id.list);
lastFilter = "Horror";
boolean online = isOnline();
if (online) {
Log.i("LOG", "B");
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setMessage("لطفا صبر کنید...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
mProgressDialog.setCancelable(false);
requestData("http://" + G.IP + "/app-server2/service.php?action=readfilter");
} else {
Toast.makeText(G.context, "No Internet", Toast.LENGTH_SHORT).show();
}
adapter = new AdapterSerials(apps);
list.setAdapter(adapter);
return layout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVIT Y_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
public void requestData(String uri) {
StringRequest request = new StringRequest(Method.POST, uri,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
apps.clear();
JSONArray series = new JSONArray(response);
for (int i = 0; i < app.length(); i++) {
JSONObject object = app.getJSONObject(i);
StructApps appss= new StructApps ();
appss.id = object.getLong("app_id");
appss.name = object.getString("app_name");
appss.ratingValue = (float) object.getDouble("app_rating");
appss.logoUrl = object.getString("app_logourl");
appss.year = object.getInt("app_year");
appss.votes = object.getString("app_vote");
apps.add(app);
}
if (adapter != null) {
adapter.notifyDataSetChanged();
}
mProgressDialog.dismiss();
}
catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError ex) {
Toast.makeText(getActivity(), "خطا سرور", Toast.LENGTH_LONG).show();
mProgressDialog.dismiss();
}
}) {
Map<String, String> params = new HashMap<String, String>();
@Override
protected Map<String, String> getParams() {
params.put("filter", "" + lastFilter);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
params.put("filter", "" + lastFilter);
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicatio nContext());
queue.add(request);
}
}
ActivityMain :
public class ActivityMain extends ActionBarActivity {
private static long back_pressed = 0L;
private Typeface typeface;
private Typeface typeface2;
private Typeface typeface3;
private Typeface typeface4;
private TextView txt_ms_void;
private TextView txt_english_msvoid;
private TextView txt_home;
private TextView txt_fav;
private TextView txt_bazar;
private TextView txt_details;
private TextView txt_exit;
private ImageView img_cafebazar_slide_menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.root);
/****************************************/
final View actionBarView = G.inflater.inflate(R.layout.action_bar, null);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_M ODE_STANDARD);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW _CUSTOM);
actionBar.setCustomView(actionBarView, new ActionBar.LayoutParams
(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
ImageView btnSearch = (ImageView) findViewById(R.id.btnSearch);
ImageView btnFav = (ImageView) findViewById(R.id.btnFav);
btnSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(G.currentActivity, ActivitySearch.class);
G.currentActivity.startActivity(intent);
}
});
btnFav.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(G.currentActivity, ActivityFavSerials.class);
G.currentActivity.startActivity(intent);
}
});
/************************************************** *********/
final DrawerLayout drawerlayout = (DrawerLayout) findViewById(R.id.drawerlayout);
txt_ms_void = (TextView) drawerlayout.findViewById(R.id.txt_ms_void);
txt_english_msvoid = (TextView) drawerlayout.findViewById(R.id.txt_english_msvoid) ;
txt_home = (TextView) drawerlayout.findViewById(R.id.txt_home);
txt_fav = (TextView) drawerlayout.findViewById(R.id.txt_fav);
txt_bazar = (TextView) drawerlayout.findViewById(R.id.txt_bazar);
txt_details = (TextView) drawerlayout.findViewById(R.id.txt_details);
txt_exit = (TextView) drawerlayout.findViewById(R.id.txt_exit);
typeface = typeface.createFromAsset(getAssets(), "arshia.ttf");
typeface2 = typeface.createFromAsset(getAssets(), "A.Arghavan.ttf");
typeface3 = typeface.createFromAsset(getAssets(), "SultanNormal_ufont.ir.ttf");
typeface4 = typeface.createFromAsset(getAssets(), "HandPrintingPress_Meshed_demo.ttf");
txt_ms_void.setTypeface(typeface3);
txt_english_msvoid.setTypeface(typeface4);
txt_home.setTypeface(typeface);
txt_fav.setTypeface(typeface);
txt_bazar.setTypeface(typeface2);
txt_details.setTypeface(typeface);
txt_exit.setTypeface(typeface);
txt_home.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
final Button btnMenu1 = (Button) actionBarView.findViewById(R.id.btnMenu1);
if (drawerlayout.isDrawerOpen(Gravity.RIGHT)) {
drawerlayout.closeDrawer(Gravity.RIGHT);
btnMenu1.setBackgroundResource(R.drawable.slide_me nue);
Animation animation = AnimationUtils.loadAnimation(ActivityMain.this, R.anim.rotate);
btnMenu1.setAnimation(animation);
} else {
drawerlayout.openDrawer(Gravity.RIGHT);
btnMenu1.setBackgroundResource(R.drawable.slide_me nue_horozintal);
Animation animation = AnimationUtils.loadAnimation(ActivityMain.this, R.anim.rotate_back);
btnMenu1.setAnimation(animation);
}
}
});
txt_fav.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(ActivityMain.this, ActivityFavSerials.class);
ActivityMain.this.startActivity(intent);
//ActivitySerials.this.finish();
}
});
img_cafebazar_slide_menu = (ImageView) drawerlayout.findViewById(R.id.img_cafebazar_slide _menu);
Animation animation = AnimationUtils.loadAnimation(ActivityMain.this, R.anim.cafebazar_in_slidemenu);
img_cafebazar_slide_menu.setAnimation(animation);
txt_details.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(ActivityMain.this, Activity_details.class);
ActivityMain.this.startActivity(intent);
//ActivitySerials.this.finish();
ActivityMain.this.overridePendingTransition(R.anim .slide_down, android.R.anim.fade_out);
}
});
final Button btnMenu1 = (Button) actionBarView.findViewById(R.id.btnMenu1);
btnMenu1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (drawerlayout.isDrawerOpen(Gravity.RIGHT)) {
drawerlayout.closeDrawer(Gravity.RIGHT);
btnMenu1.setBackgroundResource(R.drawable.slide_me nue);
Animation animation = AnimationUtils.loadAnimation(ActivityMain.this, R.anim.rotate);
btnMenu1.setAnimation(animation);
} else {
drawerlayout.openDrawer(Gravity.RIGHT);
btnMenu1.setBackgroundResource(R.drawable.slide_me nue_horozintal);
Animation animation = AnimationUtils.loadAnimation(ActivityMain.this, R.anim.rotate_back);
btnMenu1.setAnimation(animation);
}
}
});
txt_exit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
android.os.Process.killProcess(android.os.Process. myPid());
System.exit(1);
}
});
/************************************************** ***********/
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
PagerAdapter adapter = new PagerAdapter(this, getSupportFragmentManager());
viewPager.setAdapter(adapter);
} این هم یکی از فرگمنت ها :
public class FragmentHome extends Fragment {
private ViewGroup layout;
private static ListView list;
public static AdapterSerials adapter;
private static TextView txtTest;
public static ArrayList<StructApps> apps= new ArrayList<StructApps>();
public ProgressDialog mProgressDialog;
public String lastFilter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
layout = (ViewGroup) inflater.inflate(R.layout.fragment_horror, null);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().buil d();
StrictMode.setThreadPolicy(policy);
list = (ListView) layout.findViewById(R.id.list);
lastFilter = "Horror";
boolean online = isOnline();
if (online) {
Log.i("LOG", "B");
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setMessage("لطفا صبر کنید...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
mProgressDialog.setCancelable(false);
requestData("http://" + G.IP + "/app-server2/service.php?action=readfilter");
} else {
Toast.makeText(G.context, "No Internet", Toast.LENGTH_SHORT).show();
}
adapter = new AdapterSerials(apps);
list.setAdapter(adapter);
return layout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVIT Y_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
public void requestData(String uri) {
StringRequest request = new StringRequest(Method.POST, uri,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
apps.clear();
JSONArray series = new JSONArray(response);
for (int i = 0; i < app.length(); i++) {
JSONObject object = app.getJSONObject(i);
StructApps appss= new StructApps ();
appss.id = object.getLong("app_id");
appss.name = object.getString("app_name");
appss.ratingValue = (float) object.getDouble("app_rating");
appss.logoUrl = object.getString("app_logourl");
appss.year = object.getInt("app_year");
appss.votes = object.getString("app_vote");
apps.add(app);
}
if (adapter != null) {
adapter.notifyDataSetChanged();
}
mProgressDialog.dismiss();
}
catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError ex) {
Toast.makeText(getActivity(), "خطا سرور", Toast.LENGTH_LONG).show();
mProgressDialog.dismiss();
}
}) {
Map<String, String> params = new HashMap<String, String>();
@Override
protected Map<String, String> getParams() {
params.put("filter", "" + lastFilter);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
params.put("filter", "" + lastFilter);
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicatio nContext());
queue.add(request);
}
}