android


can't receive data from one fragment to another fragment


I want to implement fragment communication. Actually I am trying to send edit text data from one fragment to the 2nd Fragment. I have tried some of the codes, but my requirements are not met.
My First Fragment is here
I have tried it in different ways but I couldn't resolve it, so please guide me.
Verification.java First fragment
public class Verification extends Fragment {
EditText card_NO,pin_code;
Button button;
Toolbar toolbar;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.verificaton,null);
card_NO=(EditText)view.findViewById(R.id.cardNo);
pin_code=(EditText)view.findViewById(R.id.pin);
button=(Button)view.findViewById(R.id.varify);
toolbar=(Toolbar)view.findViewById(R.id.toolbar);
toolbar.setTitle("Verification");
listeners();
return view;
}
private void listeners() {
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
acc_logIn();
}
});
}
private void acc_logIn(){
String cardid = card_NO.getText().toString().trim();
String pin = pin_code.getText().toString().trim();
verify(cardid,pin);
//
}
private void verify(final String cardid, final String pin){
class UserVerification extends AsyncTask<String,Void,String> {
ProgressDialog loading;
#Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(getActivity(),"Please Wait",null,true,true);
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
if(s.equalsIgnoreCase("Welcome....")){
Toast.makeText(getActivity(),"Successfully Verified",Toast.LENGTH_LONG).show();
String cardid = card_NO.getText().toString();
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
Bundle bundle=new Bundle();
bundle.putString(Config.KEY_ACCOUNT, cardid);
BankServices bankServices=new BankServices();
bankServices.setArguments(bundle);
transaction.add(R.id.myFrame,bankServices);
transaction.commit();
}else{
Toast.makeText(getActivity(),s,Toast.LENGTH_LONG).show();
}
}
#Override
protected String doInBackground(String... params) {
HashMap<String,String> data = new HashMap<>();
data.put("cardid",params[0]);
data.put("pin",params[1]);
RequestHandler rh=new RequestHandler();
String result=rh.sendPostRequest(Config.URL_VERIFY,data);
return result;
}
}
UserVerification uv = new UserVerification();
uv.execute(cardid,pin);
}
}
BankServices.java Second fragment
public class BankServices extends Fragment {
private Button balance,transfer,bill;
private TextView textView;
private Toolbar toolbar;
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Bundle bundle = this.getArguments();
if (bundle != null) {
String cardid = bundle.getString(Config.KEY_ACCOUNT);
textView.setText(cardid);
}
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.services,null);
balance=(Button)v.findViewById(R.id.bt_balanceInq);
transfer=(Button)v.findViewById(R.id.bt_transfer);
bill=(Button)v.findViewById(R.id.bt_payBill);
toolbar= (Toolbar) v.findViewById(R.id.toolbar);
toolbar.setTitle("Services");
// Bundle bundle = this.getArguments();
// if (bundle != null) {
// String cardid = bundle.getString(Config.KEY_ACCOUNT);
// textView.setText(cardid);
// }
listeners();
return v;
}
private void listeners() {
balance.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
CheckBal checkBal=new CheckBal();
// transaction.add(R.id.myFrame,checkBal);
transaction.replace(R.id.myFrame,checkBal);
transaction.addToBackStack(null);
transaction.commitAllowingStateLoss();
// transaction.commit();
}
});
transfer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Transfer tr=new Transfer();
// transaction.add(R.id.myFrame,tr);
transaction.replace(R.id.myFrame,tr);
transaction.addToBackStack(null);
transaction.commitAllowingStateLoss();
}
});
bill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
PayBill payBill=new PayBill();
// transaction.add(R.id.myFrame,payBill);
transaction.replace(R.id.myFrame,payBill);
transaction.addToBackStack(null);
transaction.commitAllowingStateLoss();
}
});
}
}
I try this now and it gives this error occurred..
Process: com.example.hp.amibank, PID: 3532
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.hp.amibank.BankServices.onViewCreated(BankServices.java:31)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1010)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1171)
Hi Abdulmateen following your question Null object Refference Error on sending data from one fragment to another. Solution to your problem is to add textView = (TextView) findViewById(id here) in OnCreateView, similar to bill=(Button)v.findViewById(R.id.bt_payBill). Your error stacktrace clearly states you are attempting to invoke setText on a null reference where textView is not yet initialised, hopefully it helps.
Problem is in First fragment where your starting BankService fragment and just creating Object of PayBill,
Code updated
private void verify(final String cardid, final String pin){
class UserVerification extends AsyncTask<String,Void,String> {
ProgressDialog loading;
#Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(getActivity(),"Please Wait",null,true,true);
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
if(s.equalsIgnoreCase("Welcome....")){
Toast.makeText(getActivity(),"Successfully Verified",Toast.LENGTH_LONG).show();
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
Bundle bundle=new Bundle();
bundle.putString(Config.KEY_ACCOUNT, cardid);
PayBill payBill=new PayBill();
payBill.setArguments(bundle);
transaction.add(R.id.myFrame,payBill);
transaction.commit();
}else{
Toast.makeText(getActivity(),s,Toast.LENGTH_LONG).show();
}
}
#Override
protected String doInBackground(String... params) {
HashMap<String,String> data = new HashMap<>();
data.put("cardid",params[0]);
data.put("pin",params[1]);
RequestHandler rh=new RequestHandler();
String result=rh.sendPostRequest(Config.URL_VERIFY,data);
return result;
}
}
UserVerification uv = new UserVerification();
uv.execute(cardid,pin);
}
Try
Verification.java First fragment
public class Verification extends Fragment {
EditText card_NO,pin_code;
Button button;
Toolbar toolbar;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.verificaton,null);
card_NO=(EditText)view.findViewById(R.id.cardNo);
pin_code=(EditText)view.findViewById(R.id.pin);
button=(Button)view.findViewById(R.id.varify);
toolbar=(Toolbar)view.findViewById(R.id.toolbar);
toolbar.setTitle("Verification");
listeners();
return view;
}
private void listeners() {
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
acc_logIn();
String cardid = card_NO.getText().toString();
PayBill payBill = new PayBill();
Bundle bundle = new Bundle();
bundle.putString(Config.KEY_ACCOUNT, cardid);
payBill.setArguments(bundle);
}
});
}
private void acc_logIn(){
String cardid = card_NO.getText().toString().trim();
String pin = pin_code.getText().toString().trim();
verify(cardid,pin);
//
}
private void verify(final String cardid, final String pin){
class UserVerification extends AsyncTask<String,Void,String> {
ProgressDialog loading;
#Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(getActivity(),"Please Wait",null,true,true);
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
if(s.equalsIgnoreCase("Welcome....")){
Toast.makeText(getActivity(),"Successfully Verified",Toast.LENGTH_LONG).show();
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
BankServices bankServices=new BankServices();
transaction.add(R.id.myFrame,bankServices);
transaction.commit();
Bundle bundle=new Bundle();
bundle.putString(Config.KEY_ACCOUNT, cardid);
PayBill payBill=new PayBill();
payBill.setArguments(bundle);
FragmentManager manager= getActivity().getFragmentManager(); //getSupportFragmentManager
FragmentTransaction transaction=manager.beginTransaction();
Bundle bundle=new Bundle();
bundle.putString(Config.KEY_ACCOUNT, cardid);
PayBill payBill=new PayBill();
payBill.setArguments(bundle);
transaction.add(R.id.yourFragmeID,payBill);
transaction.commit();
// Intent intent = new Intent(getActivity(),PayBill.class);
// intent.putExtra(Config.KEY_ACCOUNT,cardid);
// startActivity(intent);
}else{
Toast.makeText(getActivity(),s,Toast.LENGTH_LONG).show();
}
}
#Override
protected String doInBackground(String... params) {
HashMap<String,String> data = new HashMap<>();
data.put("cardid",params[0]);
data.put("pin",params[1]);
RequestHandler rh=new RequestHandler();
String result=rh.sendPostRequest(Config.URL_VERIFY,data);
return result;
}
}
UserVerification uv = new UserVerification();
uv.execute(cardid,pin);
}
}
PayBill.java Updated
public class PayBill extends Fragment {
private EditText ref;
private EditText bill;
private TextView textView;
private Button fetch,bt_pay;
private ProgressDialog loading;
Toolbar toolbar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.paybill,null);
ref=(EditText)v.findViewById(R.id.refId);
textView=(TextView)v.findViewById(R.id.tv_c);
bill=(EditText) v.findViewById(R.id.tv_Bill);
fetch=(Button)v.findViewById(R.id.bt_fetch);
bt_pay=(Button)v.findViewById(R.id.bt_payBill);
fetch.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
getBill();
}
});
bt_pay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
paybill();
}
});
toolbar=(Toolbar)v.findViewById(R.id.toolbar);
toolbar.setTitle("Pay Bill");
Bundle bundle = this.getArguments();
if (bundle != null) {
String cardid = bundle.getString(Config.KEY_ACCOUNT);
textView.setText(cardid);
}
return v;
}
private void getBill(){
final String rid = ref.getText().toString().trim();
if (rid.equals("")) {
Toast.makeText(getActivity(), "Please enter an id", Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(getActivity(),"Please wait ......","fetching....",false,false);
String url = Config.URL_Get_BILL+ref.getText().toString().trim();
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(),error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String sbill="";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
sbill = collegeData.getString(Config.TAG_SHOW_BILL);
} catch (JSONException e) {
e.printStackTrace();
}
bill.setText(sbill);
}
private void paybill() {
final String rid = ref.getText().toString().trim();
final String paydue = bill.getText().toString().trim();
final String cardid=textView.getText().toString().trim();
loading = ProgressDialog.show(getActivity(), "Please wait ......", "Working....", false, false);
StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.URL_PAY_BILL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
loading.dismiss();
Toast.makeText(getActivity(), response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
loading.dismiss();
Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_LONG).show();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put(Config.KEY_ACCOUNT, cardid);
params.put(Config.KEY_BILL_ID,rid);
params.put(Config.KEY_BILL_DUE, paydue);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
}
}
I hope this will help you
Change in Fragment 1 in Post method
Fragment PayBill = new PayBill();
Bundle bundle = new Bundle();
bundle.putString(Config.KEY_ACCOUNT, cardid);
PayBill.setArguments(bundle);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.myFrame, PayBill);
fragmentTransaction.commit();
//-----------fragment 2 ---------------
public class PayBill extends Fragment {
private EditText ref;
private EditText bill;
private TextView textView;
String cardid;
private Button fetch,bt_pay;
private ProgressDialog loading;
Toolbar toolbar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments();
if (bundle != null) {
cardid = getArguments().getString(Config.KEY_ACCOUNT);
}
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.paybill,null);
ref=(EditText)v.findViewById(R.id.refId);
textView=(TextView)v.findViewById(R.id.tv_c);
bill=(EditText) v.findViewById(R.id.tv_Bill);
fetch=(Button)v.findViewById(R.id.bt_fetch);
bt_pay=(Button)v.findViewById(R.id.bt_payBill);
textView.setText(cardid);
fetch.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
getBill();
}
});
bt_pay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
paybill();
}
});
toolbar=(Toolbar)v.findViewById(R.id.toolbar);
toolbar.setTitle(“Pay Bill”);
return v;
}
private void getBill(){
final String rid = ref.getText().toString().trim();
if (rid.equals(“”)) {
Toast.makeText(getActivity(), “Please enter an id”, Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(getActivity(),“Please wait ......“,”fetching....“,false,false);
String url = Config.URL_Get_BILL+ref.getText().toString().trim();
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(),error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String sbill=“”;
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
sbill = collegeData.getString(Config.TAG_SHOW_BILL);
} catch (JSONException e) {
e.printStackTrace();
}
bill.setText(sbill);
}
private void paybill() {
final String rid = ref.getText().toString().trim();
final String paydue = bill.getText().toString().trim();
final String cardid=textView.getText().toString().trim();
loading = ProgressDialog.show(getActivity(), “Please wait ......“, “Working....“, false, false);
StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.URL_PAY_BILL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
loading.dismiss();
Toast.makeText(getActivity(), response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
loading.dismiss();
Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_LONG).show();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put(Config.KEY_ACCOUNT, cardid);
params.put(Config.KEY_BILL_ID,rid);
params.put(Config.KEY_BILL_DUE, paydue);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
}
}

Related Links

android layout dot points
Android : Client server programming
Contents of APK (Android Package)
Android: How to override the Compass action on Google Maps
Android mobile app always get restarted once it is connected or disconnected to bluetooth device
Can not play .3gp video in android videoview
Mocking/Stubbing backend server when running Android emulator or Genymotion
Android : Where is the database sqlite file in hardware device? [duplicate]
My Android app is not able to post an image on another my Facebook profile. setting or code?
Implementing Google In-app billing - Which Google tutorial is the right one?
Onclick event for overflowmenu icon on action bar
Pressing Back Button should close app,should not Stop
Strange screen flashin on the first Animation (ObjectAnimator)
Get image in gridview from server
“paddingLeft” disapears when I use setText() + setBackgroundDrawable() on a Button
Error in http connection org.apache.http.conn.HttpHostConnectException: Connection to <ip address> refused

Categories

HOME
url-redirection
time-complexity
linkedin
nunit
case
graphics
javacc
spring-data-mongodb
cublas
mips
cakephp-3.4
iis-6
nstableheaderview
fhir
plist
resultset
gkturnbasedmatch
shared-libraries
3nf
android-intent
glyphicons
nodemcu
sencha-touch
boost-icl
topology
backpack-for-laravel
parcelable
highstock
comparator
has-and-belongs-to-many
tpm
winpe
sqsh
python-2.6
mustache
cognos-bi
playstation-portable
linkedhashmap
gojs
gnat-gps
graphhopper
reactivekit
ideascript
xdocreport
release
metaprogramming
linq-to-objects
stringtokenizer
lpsolve
apollostack
photos
puredata
opennms
clarifai
spring-integration-sftp
remedy
scalding
stdmap
libjpeg
android-instrumentation
donations
usart
iotivity
uicolor
x-tag
jquery-jscrollpane
clipboard.js
gcloud-ruby
libav
mobiledoc-kit
file-move
pypiserver
toolkit
viola-jones
bufferedinputstream
rrule
webmatrix-3
sip-server
crowd
post-commit-hook
machine-instruction
angular-carousel
named-parameters
matrix-inverse
buffering
rfc5322
bin
gtm-oauth2
oocss
flex++
actiondispatch
robotlegs
idn
vfs
xcode3.2
update-statement
google-ajax-api
massive
dataform
cdonts
cac
pos-for-.net
spread
xpsdocument
perl5.12
localizable.strings

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App