Custom MvxAdapter - Override Methods not getting called - android

I have created an MvxListView with a custom adapter, but none of the adapter methods are getting called. It is worth mentioning the list is in a fragment. What am I doing wrong?
Adapter:
public class WishlistAdapter : MvxAdapter
{
public WishlistAdapter(Context context, IMvxAndroidBindingContext bindingContext)
: base(context, bindingContext)
{
}
protected override View GetBindableView(View convertView, object source, int templateId)
{
if (((Wish)source).IsOwner == false)
{
if (((Wish)source).IsBought)
{
templateId = Resource.Layout.listitem_wishbought;
}
}
return base.GetBindableView(convertView, source, templateId);
}
}
View:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.WishlistView, null);
var list = view.FindViewById<ListView>(Resource.Id.WishlistView_ListWishes);
list.Adapter = new WishlistAdapter(Activity, (MvxAndroidBindingContext)BindingContext);
return view;
}
ViewModel:
public ObservableCollection<Wish> Wishes
{
get
{
return _Wishes;
}
set
{
_Wishes = value;
RaisePropertyChanged("Wishes");
}
}
FIXED:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.WishlistView, null);
var list = view.FindViewById<**MvxListView**>(Resource.Id.WishlistView_ListWishes);
list.Adapter = new WishlistAdapter(Activity, (MvxAndroidBindingContext)BindingContext);
return view;
}

Related

onCreateView pager adapter [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
Attempt to invoke virtual method 'void androidx.viewpager.widget.ViewPager.setAdapter(androidx.viewpager.widget.PagerAdapter)' on a null object reference
I'm trying to implement sliding tab layout using android material design. But it gives me NullPointerException. Here is my code so far:
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
private List<Slide> listSlides;
private ViewPager slidePager;
private TabLayout indicator;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
listSlides = new ArrayList<>();
listSlides.add(new Slide(R.drawable.a, "slide title"));
listSlides.add(new Slide(R.drawable.b, "slide title"));
final SliderPagerAdapter adapter = new SliderPagerAdapter(getContext(), listSlides);
homeViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
slidePager.setAdapter(adapter);
}
});
return root;
}
}
public class SliderPagerAdapter extends PagerAdapter {
private Context mText;
private List<Slide> mList;
public SliderPagerAdapter(Context mText, List<Slide> mList) {
this.mText = mText;
this.mList = mList;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) mText.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View slideLayout = inflater.inflate(R.layout.slider_item,null);
ImageView slideImg = slideLayout.findViewById(R.id.slide_img);
TextView slideText= slideLayout.findViewById(R.id.slide_title);
slideImg.setImageResource(mList.get(position).getImage());
slideText.setText(mList.get(position).getTitle());
container.addView(slideLayout);
return slideLayout;
}
#Override
public int getCount() {
return mList.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == object;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View) object);
}
}
You didn't instantiate the slidePagerso when you try to setAdapter, it is in fact a null object. You should call a findViewById on the ViewPager.

How to get access to list of files in directory from fragment?

Last line gives Unreachable Statement error. How to fix it?
public class ListenFragment extends Fragment {
private String[] files;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_listen, container, false);
files = getActivity().getDir("voices", Context.MODE_PRIVATE).list();
}
}
Unreachable Statement error can be fixed by this way :
public class ListenFragment extends Fragment {
private String[] files;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
files = getActivity().getDir("voices", Context.MODE_PRIVATE).list();
return inflater.inflate(R.layout.fragment_listen, container, false);
}
}
public class ListenFragment extends Fragment {
private String[] files;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_listen, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
files = getActivity().getDir("voices", Context.MODE_PRIVATE).list();
}
}

TextView.setText() in fragment crashes app

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)

SetText EditText not working in Fragment using OnStart, OnResume and onCreateView

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);
}

Display layout in fragment on main activity xamarin

I am not able to display layout on class SecondFragment : Fragment
MAIN ACTIVITY
$ [Activity (Label = "project", Theme = "#style/Tab")]
public class TabActivity : Activity
{
ProductDB dbHelper;
ICursor cursor;
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.MainTab);
dbHelper = new ProductDB(this);
cursor = dbHelper.ReadableDatabase.RawQuery ("select * from movie", null);
StartManagingCursor (cursor);
this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
AddTab ("Products", new FirstFragment(this, cursor));
AddTab ("User Profile", new SecondFragment());
AddTab("User Order", new ThirdFragment());
if (savedInstanceState != null)
this.ActionBar.SelectTab(this.ActionBar.GetTabAt(savedInstanceState.GetInt("tab")));
}
void AddTab (string tabText, Fragment view)
{
var tab = this.ActionBar.NewTab ();
tab.SetText (tabText);
tab.TabSelected += delegate(object sender, ActionBar.TabEventArgs ab)
{
var fragment = this.FragmentManager.FindFragmentById(Resource.Id.frameLayout1);
if (fragment != null)
ab.FragmentTransaction.Remove(fragment);
ab.FragmentTransaction.Add (Resource.Id.frameLayout1, view); };
tab.TabUnselected += delegate(object sender, ActionBar.TabEventArgs ab) {
ab.FragmentTransaction.Remove(view); };
this.ActionBar.AddTab (tab);
}
protected override void OnDestroy ()
{
StopManagingCursor (cursor);
cursor.Close ();
base.OnDestroy ();
}
class FirstFragment: Fragment
{
ICursor cursor;
ListView listView;
Activity context;
public FirstFragment(Activity context, ICursor cursor) {
this.cursor = cursor;
this.context = context;
}
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);
var view = inflater.Inflate (Resource.Layout.Tab1, container, false);
listView = view.FindViewById<ListView> (Resource.Id.mylist);
listView.Adapter = new ProductAdapter (context, cursor);
listView.ItemClick += OnItemListClick;
return view;
}
protected void OnItemListClick (object sender, AdapterView.ItemClickEventArgs ab)
{
var curs = (ICursor)listView.Adapter.GetItem (ab.Position);
var movieName = curs.GetString (1);
Android.Widget.Toast.MakeText (context, movieName, Android.Widget.ToastLength.Short).Show();
}
}
class SecondFragment : Fragment
{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(R.layout.your_fragment, container, false);
return view;
}
// Inflate the layout for this fragment
// return inflater.inflate(R.layout.article_view, container, false);
// base.OnCreateView(inflater, container, savedInstanceState);
// var view = inflater.Inflate(Resource.Layout.User, container, false);
// var layout = view.FindViewById<LinearLayout>(Resource.Id.linearLayoutmargin1);
// return view;
}
class ThirdFragment : Fragment
After observing above code, there might be some issue in onCreateView of SecondFragment, you missed below line,
base.OnCreateView (inflater, container, savedInstanceState);
Confirm and let me know whether working or not?
:)GlbMP

Categories

Resources