-2

I can run this code , but when i click button on my checkbalance layout , my app closed ,can someone help me pls, my checkbalance actually is a fragment from my navigation drawer activity,my idea is when i click the button on check balance,it will go to makepayment page

Checkbalance.java

package com.helloworld.basikal;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


/**
* Created by LENOVO on 8/21/2017.
*/

public class CheckBalance extends Fragment{
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getActivity().setTitle("Check Balance");

    Button button = (Button) getView().findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), MadePayment.class);
            getActivity().startActivity(intent);
        }
    });
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.checkbalance,container,false);
}


}

Madepayment.java

package com.helloworld.basikal;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

 /**
 * Created by LENOVO on 8/24/2017.
 */

public class MadePayment extends Fragment{
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.madepayment, container, false);
    Intent intent = getActivity().getIntent();


    return view;


}

}
Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44

2 Answers2

1

It seems that class MadePayment is a fragment. But you treated it as a activity.

// error code start here
Intent intent = new Intent(getActivity(), MadePayment.class);
getActivity().startActivity(intent);
// end

Correct it as follows

/* Add this method in your host Activity */
public void attachFragment(Fragment fragment) {

    if (null == fragment) {
        return;
    }

    try {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.fragContainer, fragment);
        /* add to back stack */
        //ft.addToBackStack(null);
        ft.commitAllowingStateLoss();
    } catch (Exception e) {
    }
}

And replace the fragmet

MadePayment fragment = new MadePayment;
MainActivity hostActivity= (MainActivity)getActivity();
hostActivity.attachFragment(homeFragment);
Mable John
  • 4,518
  • 3
  • 22
  • 35
  • for the replace the fragment,should i put it in my checkbalance.java?then what should i change in madepayment.java?for the method that i should post in my host activity,is it my checkbalance.java?because checkbalance is fragment for my mainActivity(which is my navigation drawer).sorry im stil learning :) – hafez busra Aug 24 '17 at 02:55
  • after 7 hours, i finally get to solve the problem using your answer,but i dont understrand what your code mean,can u give me link that i can learn about this things pls – hafez busra Aug 24 '17 at 10:59
  • Check android developers site https://developer.android.com/training/basics/fragments/index.html. Pls accept my answer if it is helped. – Mable John Aug 24 '17 at 14:31
0

The MadePayment is a fragment. Use FragmentTransactions to replace the fragment.

Button button = (Button) getView().findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment newfragment= new newFragment(); 
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentframe_container, newfragment); 
transaction.addToBackStack(null);
transaction.commit();
}
});
Abhi
  • 3,431
  • 1
  • 17
  • 35