I am using Fragment and due to some reasons, EditText is not displaying the data retrieved from a POST request which gives JSON response.
There is no issue in JSON response. I also used OnResume and OnStart methods but still nothing working.
Am I missing anthing?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragment_profile = inflater.inflate(R.layout.fragment_profile, container, false);
InitializeControls(fragment_profile);
NM_Profile.getInstance(this.getContext()).ViewProfile(new IResultListener<JOM_User>() {
#Override
public void getResult(JOM_User object) {
user = object.getUser();
if(user != null) {
txtEmailAddress_Profile.setText(user.getEmailAddress());
}
}
});
return inflater.inflate(R.layout.fragment_profile, container, false);
}
#Override
public void OnStart() {
if(user != null) {
txtEmailAddress_Profile.setText(user.getEmailAddress());
}
return super.OnStart();
}
#Override
public void OnResume() {
if(user != null) {
txtEmailAddress_Profile.setText(user.getEmailAddress());
}
return super.OnStart();
}
Try initialising txtEmailAddress_Profile EditText inside the onCreateView() rather than InitializeControls().
Try something like this:
private EditText txtEmailAddress_Profile;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragment_profile = inflater.inflate(R.layout.fragment_profile, container, false);
InitializeControls(fragment_profile);
txtEmailAddress_Profile =(EditText) view.findViewById(R.id.YourEditText);
NM_Profile.getInstance(this.getContext()).ViewProfile(new IResultListener<JOM_User>() {
#Override
public void getResult(JOM_User object) {
user = object.getUser();
if(user != null) {
txtEmailAddress_Profile.setText(user.getEmailAddress());
}
}
});
return inflater.inflate(R.layout.fragment_profile, container, false);
}
Related
I am trying to change TextView content on Sensor change but after calling TextView.setText("foo"); app crashes as if the TextView variable was null. I set up the TextView variable in onCreateView. Here is the code:
public class fragment1 extends Fragment implements SensorEventListener
{
private SensorManager mSrMgr = null;
private Sensor mLight;
private TextView page1;
protected View mView;
public fragment1()
{
// Required empty public constructor
}
public void onCreate(Bundle savedInstance)
{
super.onCreate(savedInstance);
mSrMgr = (SensorManager) getActivity().getSystemService(SENSOR_SERVICE);
mLight = mSrMgr.getDefaultSensor(Sensor.TYPE_LIGHT);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
page1 = container.findViewById(R.id.page1);
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
this.mView = view;
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
public void onPause()
{
super.onPause();
this.unregister();
}
public void onResume()
{
super.onResume();
this.registerSensorListiner();
}
private void registerSensorListiner()
{
mSrMgr.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
}
private void unregister()
{
mSrMgr.unregisterListener(this);
}
#Override
public void onSensorChanged(SensorEvent event)
{
Toast.makeText(getContext(),String.valueOf(event.values[0]),Toast.LENGTH_SHORT).show();
page1.setText("elo"); //here is the crashing part
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
}
First, inflate the layout
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
this.mView = view;
page1 = view.findViewById(R.id.page1);
return view, false);
use view object to call findViewById method
Use this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
this.mView = view;
page1 = view.findViewById(R.id.page1);
return view;
}
Instead of this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
page1 = container.findViewById(R.id.page1);
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
this.mView = view;
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
Why Multiple times R.layout. ?
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
this.mView = view;
return inflater.inflate(R.layout.fragment_fragment1, container, false);
It should be One Time. At first modify here.
Note
You will get NullPointerException.
Thrown when an application attempts to use null in a case where an
object is required.
You should pass View's object .
page1 = view.findViewById(R.id.page1);
Finally
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
page1 = view.findViewById(R.id.page1);
this.mView = view;
return view;
public class fragment1 extends Fragment implements SensorEventListener {
private SensorManager mSrMgr = null;
private Sensor mLight;
private TextView page1;
protected View mView;
public fragment1() {
// Required empty public constructor
}
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
mSrMgr = (SensorManager) getActivity().getSystemService(SENSOR_SERVICE);
mLight = mSrMgr.getDefaultSensor(Sensor.TYPE_LIGHT);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_fragment1, container, false);
page1 = mView.findViewById(R.id.page1);
return mView;
}
public void onPause() {
super.onPause();
this.unregister();
}
public void onResume() {
super.onResume();
this.registerSensorListiner();
}
private void registerSensorListiner() {
mSrMgr.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
}
private void unregister() {
mSrMgr.unregisterListener(this);
}
#Override
public void onSensorChanged(SensorEvent event) {
Toast.makeText(getContext(), String.valueOf(event.values[0]), Toast.LENGTH_SHORT).show();
page1.setText("elo"); //here is the crashing part
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
put your findViewById code in onActivityCreated like below:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
page1 = getActivity().findViewById(R.id.page1);
}
Sometimes when creating layouts in fragments you copy and paste xml code that has duplicate IDs. When you then call the id of the pasted resources it will try to invoke the first defined resources which is probably null at runtime. Double check if the page1 id is found only in one fragment's Xml. If not work around "findviewbyid".I hope that helps, there's little detail in your code(Error msg)
i am calling a list inside a dialog fragment which is loaded from a custom adapter.
loading my fragment
ProfileListViewHolder.imageButtonMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ProfileDialog ProfileDialogFragment = new ProfileDialog();
ProfileDialogFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog);
android.support.v4.app.FragmentTransaction fragmentManager =((AppCompatActivity)context).getSupportFragmentManager().beginTransaction();
fragmentManager.replace(R.id.drawer_layout,ProfileDialogFragment) .addToBackStack(null).commit();
}
});
and this is my Dialog that i am using
public class ProfileDialog extends android.support.v4.app.DialogFragment {
private Context context;
public View profileView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void setTitle(String title) {
Dialog dialog = getDialog();
dialog.setTitle(title);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (profileView == null) {
profileView = inflater.inflate(R.layout.fragment_profile_dialog, container,false);
} else {
((ViewGroup) profileView .getParent()).removeView(profileView );
}
ListView profileListView = (ListView) profileView.findViewById(R.id.lstvSelectedProfile);
return profileListView;
}
#Override
public void onStart() {
super.onStart();
//addInnerFragment();
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
return dialog;
}
}
You are returning the profileListView instead of profileView. change the code like this ,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
profileView = inflater.inflate(R.layout.fragment_profile_dialog, container, false);
ListView profileListView = (ListView) profileView.findViewById(R.id.lstvSelectedProfile);
return profileView;
}
Try
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
profileView = inflater.inflate(R.layout.fragment_profile_dialog, container,false);
ListView profileListView = (ListView) profileView.findViewById(R.id.lstvSelectedProfile);
return profileView;
}
I'm passing a string between two fragments. It's working but for a reason i can't get the string outside onStart...
public class EventSelectFriendFragment extends Fragment {
final static String DATA_RECEIVE = "data_receive";
String passedPushId;
Button create;
#Override
public void onStart() {
super.onStart();
Bundle args = getArguments();
if (args != null) {
passedPushId = args.getString(DATA_RECEIVE);
Log.d("passed id",args.getString(DATA_RECEIVE) );
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_event_select_friend, container, false);
create= (Button) rootView.findViewById(R.id.btnCreate);
btnMaakEvent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for(int i = selectedFriends.size()-1 ; i >= 0; i--){
Log.d("array: ", i + selectedFriends.get(i).getFirstName());
Log.d("passedPushId: ", passedPushId);
}
}
});
return rootView;
}
}
In onStart passedPushId gives me the string from the other fragment.
But when i want to use it in a for loop the passedPushId is null...
onCreateView() is called earlier than onStart(). Generally speaking, you should initialize instance variables from the arguments Bundle inside of onCreate(), not onStart(), as that is much earlier in the fragment lifecycle.
I have two fragments in my mainactivity, from the first one, the user clicks on a button and send through my listener a char to the second fragment, depends of that char a textview in the second fragment must print a text. But in my metho onActivityCreated my textview is always null.
This is my code:
FragmentOne
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btnChefcito = (Button)getView().findViewById(R.id.btnChefcito);
btnChefcita = (Button)getView().findViewById(R.id.btnChefcita);
btnChefcito.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeListener.elegirSexo(sexo);
replaceFragment();
}
});
btnChefcita.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sexo="f";
welcomeListener.elegirSexo(sexo);
replaceFragment();
}
});
}
This is my second fragment
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
tviBienvenida = (TextView)getView().findViewById(R.id.tviBienvenida);
}
public void getSex(String sex){
if(sex.equals("m")){
tviBienvenida.setText(bienvenido);
}else if(sex.equals("f")){
tviBienvenida.setText(bienvenida);
}
}
Instead of doing in onActivityCreated Do This in onCreateView:
private static View viewHolder;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
viewHolder = inflater.inflate(R.layout.YourFragmentLayout, container, false);
btnChefcito = (Button) viewHolder.findViewById(R.id.YourButtonId);
btnChefcito.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeListener.elegirSexo(sexo);
replaceFragment();
}
});
return viewHolder;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.your_fragment_id, null, false);
btnChefcito = (Button)view.findViewById(R.id.btnChefcito);
btnChefcita = (Button)view.findViewById(R.id.btnChefcita);
btnChefcito.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
welcomeListener.elegirSexo(sexo);
replaceFragment();
}
});
btnChefcita.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sexo="f";
welcomeListener.elegirSexo(sexo);
replaceFragment();
}
});
return view;
}
the problem is public void onActivityCreated(Bundle savedInstanceState) not create any view in fragment so replace all code in this method
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
sorry for my English.
On Android you have to declare a global variable.
tviBienvenida in you class, and on create view
tviBienvenida = (TextView)getView().findViewById(R.id.tviBienvenida);
The variable tviBienvenida will always have the value, and any change in tviBienvenida will also be refected to your textview.
Implement view in onCreateView method and repleace your stuff there, for example:
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_layout, container, false);
TextView textView = view.findViewById(R.id.yourTextView);
//your code
return view;
}
The problem is, when I make a search into the listfragment, it works. When I close it and open again and do another search, the exception appears. In the developers options I choose to not maintain the activities.
I guess the references of the layout are lost when the app wakes up, but have no idea why.
What can I do?
This is my code
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listAdapter = new ListAdapter(getActivity(), adArrayList);
setListAdapter(listAdapter);
getListView().setOnScrollListener(this);
}
#Override public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view,savedInstanceState);
ViewGroup viewGroup = (ViewGroup) view;
mPullToRefreshLayout = new PullToRefreshLayout(viewGroup.getContext());
ActionBarPullToRefresh.from(getActivity())
.insertLayoutInto(viewGroup)
.theseChildrenArePullable(android.R.id.list)
.listener(this)
.setup(mPullToRefreshLayout);
}
#Override
public void onResume() {
pagination = 0;
getAds("");
super.onResume() ;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
listView = (ListView) view.findViewById(android.R.id.list);
ViewGroup parent = (ViewGroup) listView.getParent();
// Remove ListView and add CustomView in its place
int listViewIndex = parent.indexOfChild(listView);
parent.removeViewAt(listViewIndex);
LinearLayout linearLayout = (LinearLayout) inflater.inflate(
R.layout.list_frag, container, false);
parent.addView(linearLayout , listViewIndex,
listView.getLayoutParams());
listView.setDrawSelectorOnTop(false);
listView.setDivider(null);
listView.setDividerHeight(0);
return view;
}
And this is the method I use to get the data from the server
//the next method use retrofit
public void getAds(String productSearch){
isLoadingMoreAds = true;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
mPullToRefreshLayout.setRefreshing(true);
} else{
setListShown(false);
}
Services services= Utils.getRESTAdapter();
services.getSearch(new Callback<AdsList>() {
#Override
public void success(AdsList adsList, Response response) {
try{
setListShown(true);
mPullToRefreshLayout.setRefreshComplete();
updateView(adsList.getAd());
isLoadingMoreAds = false;
}catch(IllegalStateException e){
Log.e("Exception", "ListFragment ya no existe");
}
}
#Override
public void failure(RetrofitError error) {
isLoadingMoreAds = false;
mPullToRefreshLayout.setRefreshComplete();
updateView(new ArrayList<Ad>());
setListShown(true);
Log.e("Error", "Failure at get ads");
}
});
}