Search code examples
androidandroid-fragmentsfragmentandroid-fragmentactivity

Hi, i have a troble in my project... pls help me to sort it out


In my project contain a navigation drawer ,in the navigation drawer contain few items, i used fragments for representing the items inside the drawer.

This is my main activity, i included all fragments in here for navigating from the drawer to the corresponding fragment view.

public class MainActivity extends NavigationActionBarLiveo 
implements             OnItemClickListener {
    //naviagtion Drawer libarary

    private HelpLiveo mHelpLiveo;

    private Toolbar mToolbar;
    private SessionManager session;


    public void onInt(Bundle savedInstanceState) {



//        Bundle bundle = getIntent().getExtras(); //        String
username=bundle.getString("name"); //        String
email=bundle.getString("email"); // //       
this.userName.setText(username); //       
this.userEmail.setText(email);
        this.userPhoto.setImageResource(R.drawable.ic_rudsonlive);
       this.userBackground.setImageResource(R.drawable.ic_user_background_first);


        // Creating items navigation
        mHelpLiveo = new HelpLiveo();
        mHelpLiveo.add(getString(R.string.Home), R.mipmap.home);
        mHelpLiveo.add(getString(R.string.MyGorups), R.mipmap.mygroup);
        mHelpLiveo.add(getString(R.string.MyCart), R.mipmap.mycart); //            mHelpLiveo.addSeparator(); // Item separator
      mHelpLiveo.addSubHeader(getString(R.string.categories)); //Item subHeader

        mHelpLiveo.add(getString(R.string.Mobile), R.mipmap.mobile);

        mHelpLiveo.add(getString(R.string.Electronics), R.mipmap.electronics);
        mHelpLiveo.add(getString(R.string.Laptops), R.mipmap.laptop);

        //{optional} - Header Customization - method customHeader //        View mCustomHeader =
getLayoutInflater().inflate(R.layout.custom_header_user,
this.getListView(), false); //        ImageView imageView =
(ImageView) mCustomHeader.findViewById(R.id.imageView);

        with(this).startingPosition(0) //Starting position in the list
                .addAllHelpItem(mHelpLiveo.getHelp())

                        //{optional} - List Customization "If you remove these methods and the list will take his white standard
color"
                        //.selectorCheck(R.drawable.selector_check) //Inform the background of the selected item color
                        //.colorItemDefault(R.color.nliveo_blue_colorPrimary) //Inform the
standard color name, icon and counter
                        //.colorItemSelected(R.color.nliveo_purple_colorPrimary) //State the
name of the color, icon and meter when it is selected
                        //.backgroundList(R.color.nliveo_black_light) //Inform the list of
background color
                        //.colorLineSeparator(R.color.nliveo_transparent) //Inform the color
of the subheader line

                        //{optional} - SubHeader Customization
                .colorItemSelected(R.color.nliveo_blue_colorPrimary)
                .colorNameSubHeader(R.color.nliveo_blue_colorPrimary)
                        //.colorLineSeparator(R.color.nliveo_blue_colorPrimary)


                        //.footerSecondItem(R.string.settings, R.mipmap.ic_settings_black_24dp)

                        //{optional} - Header Customization
                        //.customHeader(mCustomHeader)

                        //{optional} - Footer Customization
                        //.footerNameColor(R.color.nliveo_blue_colorPrimary)
                        //.footerIconColor(R.color.nliveo_blue_colorPrimary)
                        //.footerBackground(R.color.nliveo_white)

                .setOnClickUser(onClickPhoto)
                .setOnPrepareOptionsMenu(onPrepare)

                        .setOnClickFooterSecond(onClickFooter)
                .build();

        int position = this.getCurrentPosition();
        //this.setElevationToolBar(position != 0 ? 6 : 0);
        session = new SessionManager(getApplicationContext());

        if (!session.isLoggedIn()) {
            logoutUser();
        }
        mToolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(mToolbar);
    }

    @Override
    public void onItemClick(int position) {
        Fragment mFragment=null;
        FragmentManager mFragmentManager = getSupportFragmentManager();
        String title = getString(R.string.app_name);
        switch (position){
            **case 0:
                mFragment = new Fragment_Home();
                title = getString(R.string.title_home);
                break;**
            case 1:
                mFragment=new Fragment_Mygroup();
                title=getString(R.string.title_mygroup);
                break;
            case 2:
                  mFragment=new Fragment_Mycart();

                title=getString(R.string.title_mycart);
                break;
            case 4:
                mFragment=new Fragment_Mobile();
                title=getString(R.string.title_mobile);
                break;
            case 5:
                mFragment=new Fragment_Electronics();
                title=getString(R.string.title_electronics);
                break;
            case 6:
                mFragment=new Fragment_Laptops();
                title=getString(R.string.title_Laptop);
                break;

            default:

                break;
        }
        ;
        if (mFragment != null){
            mFragmentManager.beginTransaction().replace(R.id.container,
mFragment).commit();
            getSupportActionBar().setTitle(title);
        }

        //setElevationToolBar(position != 0 ? 6 : 0);
    }

From the drawer i can go each fragment view ,from each fragment contain a recyclerview for listing the details from the webservice and once clicked on the item it will be redirect in to a actvity called Viewactvity. The code for my View activity is

public class Viewactivity extends AppCompatActivity implements 
View.OnClickListener {
    TextView phone,price,email;
    ImageView product;
    Button wishList,orderList;

    private ProgressDialog progressDialog;
    private static final String TAG = Viewactivity.class.getSimpleName();


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_viewactivity);


        phone=(TextView)findViewById(R.id.product_name);
        price=(TextView)findViewById(R.id.product_price);
        email=(TextView)findViewById(R.id.textView_email);
        product=(ImageView)findViewById(R.id.product_image);
        wishList=(Button)findViewById(R.id.button_wishlist);
        orderList=(Button)findViewById(R.id.button_order);


      // order & wishlist
        wishList.setOnClickListener(this);
        orderList.setOnClickListener(this);

        //setting progressDialog
        progressDialog = new ProgressDialog(this);
        progressDialog.setCancelable(false);

        // Fragments home


     // Getting the product details


        Bundle bundle = getIntent().getExtras();
        String url = bundle.getString("Image");

         phone.setText(bundle.getString("Product"));
        price.setText( "Price:   "+bundle.getString("Price"));
       // email.setText(bundle.getString("email"));


        Picasso.with(getApplicationContext()).load(url).into(product);



    }
    /**
     * function to verify login details
     * */
    private void Orderplacing( final String product_name) {
        // Tag used to cancel the request
        String tag_string_req = "req_order";

        progressDialog.setMessage("Your order is placing ...");
        showDialog();

        StringRequest strReq = new StringRequest(Request.Method.POST,
                AppConfig.URL_ORDERPLACING, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "login Response: " + response.toString());
                hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);


           Boolean error = jObj.getBoolean("error");

                      //String order=jObj.getString("order");
                    System.out.println("Your now at if"+error);

                    if (!error) {



                        // Launching  home activity
                        FragmentManager mFragmentManager =    getSupportFragmentManager();
                        FragmentTransaction fragmentTransaction=mFragmentManager.beginTransaction();
                        Fragment_Home fragment_home=new Fragment_Home();
                        fragmentTransaction.replace(android.R.id.content,fragment_home);
                        fragmentTransaction.commit();
                      Intent intent = new Intent(Viewactivity.this,
                               Fragment_Home.class);

                        String responseMsg = jObj.getString("response");
                        Toast.makeText(getApplicationContext(),
                                responseMsg, Toast.LENGTH_LONG).show();
                        System.out.println("Your" + responseMsg);
                 startActivity(intent);
                      //  startActivity(intent);

                    }

                    else {
                        // order error
                        String responseMsg = jObj.getString("response");
                        Toast.makeText(Viewactivity.this,
                          "ooola kirane"+ responseMsg, Toast.LENGTH_LONG).show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(Viewactivity.this,
                        error.getMessage(), Toast.LENGTH_LONG).show();
                hideDialog();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                // Post product to orderplacing url
                Map<String, String> params = new HashMap<String, String>();

                params.put("item", product_name);

                return params;
            }

        };

        // Adding request to  queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }


    /*
    function to show dialog
     */
    private void showDialog() {
        if (!progressDialog.isShowing())
            progressDialog.show();
    }

    /*
    function to hide dialog
     */
    private void hideDialog() {
        if (progressDialog.isShowing())
            progressDialog.dismiss();
    }

From this activity i used a web service api ,once response of the api is not error then i want to redirect from the this activity to My Fragment_Home (Fragment).But when am trying to run the app its crashed ..


Solution

  • can you please try to modify like this

     Fragment mFragment;
     FragmentManager mFragmentManager = getSupportFragmentManager();
     FragmentTransaction fragmentTransaction=mFragmentManager.beginTransaction();
     Fragment_Home fragment_home=new Fragment_Home();
     fragmentTransaction.replace(android.R.id.content,fragment_home);
     fragmentTransaction.commit();
    
     mFragment = new Fragment_Home();