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

How can I pass a custom object from one activity to another using putExtra()
Video streaming from xml file(android)
Wifi sleep policy and wifi lock which one overrides when both conflict?
Parse entire soap response in android with ksoap2-android
how to bring text close to image in a button
Android splash screen with a different image each time the app starts
nothing is being displayed after opening the app
Getting a TableLayout from a fragment
sending and receiving notification in android
Show Facebook / Google plus profile image in higher version
Long text in Android TextView pushes out other Objects
Gmail file attachment download is failing with getContentResolver()
Access Not Configured. Please use Google Developers Console to activate the API for your project for youtube search list
How to combine images into single canvas Android
How to highlight android EditText text and display cursor at end?
phonegap Android: How to clear datas displayed in label according to selection in dropdown list?

Categories

HOME
xml
facebook-oauth
jpa
design-patterns
case
apache-spark-mllib
sitecore
solaris
error-handling
ms-access-2016
javacc
spring-data-mongodb
soa
vxml
telegram-bot
visual-studio-emulator
hugo
square
zip
nuget-package
gluon-mobile
bing-maps
g++
ibeacon-android
angular-dart
docx
digital-signature
topology
sar
symbols
visual-glitch
rubymotion
libigl
named-entity-recognition
imageresizer
qliksense
displaytag
flooding
android-maps
tizen-tv
jaws
zend-debugger
login-script
joomla2.5
indy10
video-capture
engine.io
salesforce-communities
sql-server-ce
xcode7.3
gdata
linq-to-objects
datacachefactory
hammer.js
dbcontext
launchctl
datamaps
reindex
stdmap
check-mk
vibrate
gets
chromium-os
docpad
cudd
mapxtreme
envi
lrs
adafruit
greatest-n-per-group
beamer
drawstring
qiime
dbamp
kiosk
pushbots
jaunt-api
rasterize
dock
visual-sourcesafe-2005
atoi
canvg
phpldapadmin
appserver
functional-java
vbe
spring-3
c++builder-xe5
apache-abdera
xcode3.2
lambdaj
cross-database
agility.js
jquery-tools
dataform
unattended-processing
svk
cac
maven-1
easyb
office-2007
wordprocessingml
cuda.net
localizable.strings
fdf
great-circle

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