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

Make existing Android app freemium, unfair to existing customers
How to read data from a device at the Kernel Level?
Insert new data into sqlite if not present
Adverse effects of uppercase characters in package name
Can't achieve notifyDataSetChanged()
How to decode the image string to bitmap image from php server?
Showing multiple markers in google map v2
How to get the time for which screen is ON
Drive android Api How to delete or Trash and Move a file or folder? [duplicate]
Clear All ShelockFragmentActvity from Stack
android listview items background
Having layout problems with Samsung galaxy Note 3
Modify HTTP headers in Google App Engine Endpoints (Android)
Android: How to pipe stream between rfcomm socket and Pppd with notty option
Weird behaviour of drawableRight for Switch
How to get data from a database created in salesforce in android?

Categories

HOME
c++11
nvd3.js
r-googlesheets
vxml
jquery-ui
jtextpane
modal-dialog
spin
little-proxy
siddhi
google-form
pybossa
gource
coin3d
chargify
dreamweaver
git-extensions
imessage
enterprise-miner
googlebot
cgbitmapcontext
inno-download-plugin
string-concatenation
pimcore
imageresizer
qliksense
nstimer
winexe
sqlpackage
autodesk-designautomation
cognos-bi
turfjs
spring-security4
wacom
hunspell
alphabetical
apns-php
demo
apache-stanbol
wmp
parser-generator
sharp-snmp
memory-mapping
jide
openal
proget
wcftestclient
mschart
mongotemplate
singleinstance
css-transforms
oozie-coordinator
libjpeg
vibrate
custom-lists
flex-monkey
actor-platform
pacemaker
jodd
mapinfo
ng-grid
mediametadataretriever
tracker-enabled-dbcontext
cortex-m
invoke-sqlcmd
directdraw
pypiserver
morton-number
ons-api
zen-cart
coda
.bash-profile
cakephp-2.7
public-html
document-repository
android-cookiemanager
angular-gettext
universal-image-loader
lambda-architecture
parallel-testing
dot42
square-bracket
windows-embedded-standard
route-provider
time.h
actiondispatch
stacky
http-status-code-410
pdfviewer
patricia-trie
lambdaj
procedures
socketserver
opml
wysiwym
change-management

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