Android App stops with a Keeps Stopping message - kotlin-android-extensions

I am following the instructions at CodeLabs to add the countMe function
But when I run it I get a "My First App keeps stopping" message.
import android.view.View
import android.widget.TextView
import android.widget.Toast
import android.widget.Toast.*
class MainActivity() : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun countMe (view: View) {
// Get the text view
val showCountTextView = findViewById<TextView>(R.id.textView)
// Get the value of the text view.
val countString = showCountTextView.text.toString()
// Convert value to a number and increment it
var count: Int = Integer.parseInt(countString)
count++
// Display the new value in the text view.
showCountTextView.text = count.toString();
}
}
the layout is
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/FirstHello"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" android:id="#+id/textView"/>
<Button
android:text="#string/toast"
android:onClick="toastMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/toast_button" android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/textView" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp" android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:layout_marginRight="8dp"/>
<Button
android:text="Count"
android:onClick="countMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/count_button" android:layout_marginTop="152dp"
app:layout_constraintTop_toTopOf="#+id/toast_button" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="48dp" android:layout_marginStart="48dp"/>
<Button
android:text="Random"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/random_button" android:layout_marginTop="160dp"
app:layout_constraintTop_toBottomOf="#+id/textView" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="72dp" android:layout_marginRight="72dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
When I run the app and click the button the application stops.

It worked when I changed the text to contain a number

Related

Onclick doen't work in the fragments of viewpager2

I have a viewpager2 with a tablayout with two tabs. In the fragment of each tab a have a button, but the onclick listener never is called when I click on it. I don't know where is the problem...
Fragment where I instance the viewpager2:
...
#BindView(R.id.tab_viewpager2_bookings)
lateinit var tabLayout: TabLayout
#BindView(R.id.viewpager2_booking)
lateinit var viewPager: ViewPager2
...
val tabs = arrayOf(getString(R.string.bookings_on_going), getString(R.string.bookings_upcoming))
tabLayout.tabGravity = TabLayout.GRAVITY_FILL
val adapter: FragmentStateAdapter = BookingsPagerAdapter(requireActivity(), tabs, ticketsOnGoing, ticketUpcoming, _user!!, bearerToken)
viewPager.adapter = adapter
if (ticketsOnGoing.isEmpty() && ticketUpcoming.isNotEmpty()) {
viewPager.currentItem = 1
}
TabLayoutMediator(tabLayout, viewPager) { tab, position ->
tab.text = tabs[position]
}.attach()
The XML with the viewpager2:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tab_viewpager2_bookings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:visibility="visible"
app:tabIndicatorColor="#color/tabs_indicator_boardingpass"
app:tabTextColor="#color/tabs_indicator_boardingpass"></com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewpager2_booking"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/tab_viewpager2_bookings" />
...
The adapter:
class BookingsPagerAdapter(fa: FragmentActivity, private val tabs:Array<String>, private val
ticketsOnGoing: List<TicketModel>, private val ticketUpcoming: List<TicketModel>,
private val user: UserModel, private val bearerToken: String) : FragmentStateAdapter(fa) {
override fun getItemCount(): Int {
return tabs.size
}
override fun createFragment(position: Int): Fragment {
when(position){
0 -> return BookingsTabFragment(ticketsOnGoing, position, user, bearerToken)
1 -> return BookingsTabFragment(ticketUpcoming, position, user, bearerToken)
}
return BookingsTabFragment(ticketsOnGoing,0, user, bearerToken)
}
}
The tab fragment:
class BookingsTabFragment(private val bookingsList: List<TicketModel>, val position: Int, val user: UserModel, private val bearerToken: String) : BaseFragment() {
lateinit var buttonTest : Button
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v: View = inflater.inflate(R.layout.fragment_tab_bookings, container, false)
if (bookingList.isEmpty()) {
llMessageBookingsFragment.visibility = View.GONE
rv_bookings_list.visibility = View.VISIBLE
buttonTest = v.findViewById<Button>(R.id.button_test)
buttonTest.setOnClickListener(View.OnClickListener {
Toast.makeText(context, "Prueba", Toast.LENGTH_SHORT).show()
//IT NEVER ENTER HERE!!
})
} else {
llMessageBookingsFragment.visibility = View.VISIBLE
rv_bookings_list.visibility = View.GONE
...
The XML of tab fragment:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/ll_message_bookings_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:orientation="vertical"
tools:visibility="gone"
android:clickable="true">
<Button
android:id="#+id/button_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prueba"/>
...
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_bookings_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:clipToPadding="false"
tools:listitem="#layout/row_bookings"
tools:visibility="visible"/>
Thanks!
I found the problem. In the tab fragment, I use RelativeLayout. The order of the components is very important for this issue, so I put the recycler view before the layout with the button.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_bookings_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:clipToPadding="false"
tools:listitem="#layout/row_bookings"
tools:visibility="visible"/>
<LinearLayout
android:id="#+id/ll_message_bookings_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:orientation="vertical"
tools:visibility="gone"
android:clickable="true">
<Button
android:id="#+id/button_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prueba"/>
...

What property do you use to bind data to axml layout in MvvmLight?

I've been for two whole days trying to figure out how do you bind data and commands to layout elements like a button or a listView and up until now I have had no success heres what I have as an example of one of my layouts.
Can you Help me?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/srlStores"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="80"
local:MvxBind="Refreshing IsBusy">
<MvxListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lvStores"
local:MvxBind="ItemsSource Stores; ItemClick OpenDetailCommand"
local:MvxItemTemplate="#layout/store_list_item" />
</android.support.v4.widget.SwipeRefreshLayout>
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
android:id="#+id/progressBar1"
local:MvxBind="Visible IsBusy" />
<Button
android:text="#string/storeListCreateButtonText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/bCreate"
android:layout_weight="20"
local:MvxBind="Click CreateCommand" />
</LinearLayout>
You are extremely close. You have to make sure the ViewModel is setup properly, and then just go into the ViewModel that is associated with that Fragment which this Layout belongs to and for the button add this:
private IMvxCommand _createCommand;
public IMvxCommand CreateCommand
{
get
{
return createCommand ?? (createCommand = new MvxCommand(() =>
{
// Do Some Work
}));
}
}
Similarly for your List, you need to create an ObservableCollection like this:
private ObservableCollection<StoreListModelWrapper> _stores;
public ObservableCollection<StoreListModelWrapper> Stores
{
get { return _stores; }
set { SetProperty(ref _stores, value); }
}
that will be called using the click command
public IMvxCommand<StoreListModelWrapper> _itemClickCommand;
public IMvxCommand<StoreListModelWrapper> ItemClickCommand
{
get
{
return _itemClickCommand ?? (_itemClickCommand = new MvxCommand<StoreListModelWrapper>((item) => // Do Work with item.
));
}
}

MvvmCross Android binding EditText in release mode

I have a problem with binding EditText on Android platform.
Today I update in my project MvvmCross framework from 6.2.X to 6.3.1 (+ update others NuGets) and changed TargetSdk and CompileSdk from Android 8.1 to 9.0 and now when I have Release mode and linking set to "Sdk Assemblies" Only my app crash on View where I have binding EditText. In debug where I have checked "Use Shared Runtime" and set linking to "None" there is no problem it works.
I have Include TextView in LinkerPleaseInclude:
public void Include(TextView text)
{
text.AfterTextChanged += (sender, args) => text.Text = $"{text.Text}";
text.Hint = $"{text.Hint}";
}
it throws this exception: https://pastebin.com/EmkuL7hM
Layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:paddingTop="50dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroundColor">
<LinearLayout
android:layout_margin="10dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:id="#+id/logo"
android:layout_width="200dp"
android:layout_height="100dp"
android:src="#drawable/ic_logo_red"
android:scaleType="fitCenter" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:text="Email"
local:MvxLang="Text Email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/primaryColor"
android:textStyle="bold"
android:textSize="#dimen/text_medium" />
<EditText
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="40dp"
local:MvxBind="Text LoginName"
android:singleLine="true"
android:inputType="textEmailAddress"
android:background="#color/white"
android:cursorVisible="true"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColorHint="#color/primaryTextColor"
android:textColor="#color/primaryTextColor" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:text="Password"
local:MvxLang="Text Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/primaryColor"
android:textStyle="bold"
android:textSize="#dimen/text_medium" />
<EditText
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="40dp"
local:MvxBind="Text Password"
android:singleLine="true"
android:inputType="textPassword"
android:background="#color/white"
android:cursorVisible="true"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColorHint="#color/primaryTextColor"
android:textColor="#color/primaryTextColor" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:gravity="center"
android:id="#+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/text_large"
local:MvxLang="Text Login"
local:MvxBind="Click LoginCommand"
android:padding="10dp"
android:background="#drawable/button_round_primary"
android:textColor="#color/white"
android:text="Login"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</ScrollView>
ViewModel:
public class LoginViewModel : MvxViewModel
{
private readonly IMvxNavigationService _navigationService;
private readonly IInfoMessageReporter _infoMessageReporter;
private readonly ISessionInfo _session;
private readonly ILoginService _loginService;
private readonly IDataService _dataService;
public LoginViewModel()
{
_navigationService = Mvx.IoCProvider.Resolve<IMvxNavigationService>();
_infoMessageReporter = Mvx.IoCProvider.Resolve<IInfoMessageReporter>();
_session = Mvx.IoCProvider.Resolve<ISessionInfo>();
_loginService = Mvx.IoCProvider.Resolve<ILoginService>();
_dataService = Mvx.IoCProvider.Resolve<IDataService>();
RememberLogin = true;
}
public IMvxLanguageBinder TextSource => new MvxLanguageBinder(Constants.LocalizationNamespace, GetType().Name);
public override async Task Initialize()
{
await InitializePermissionsAsync();
}
private async Task InitializePermissionsAsync()
{
// some stuff...
}
private string _password;
public string Password
{
get => _password;
set
{
_password = value;
RaisePropertyChanged(() => Password);
}
}
private string _loginName;
public string LoginName
{
get => _loginName;
set
{
_loginName = value;
RaisePropertyChanged(() => LoginName);
}
}
private MvxAsyncCommand _loginCommand;
public IMvxAsyncCommand LoginCommand
{
get
{
_loginCommand = _loginCommand ?? new MvxAsyncCommand(async () => await ExecuteLoginAsync());
return _loginCommand;
}
}
private async Task ExecuteLoginAsync()
{
// some stuff....
}
}
I believe the issue you are experiencing is a current bug in Xamarin's latest build (around Xamarin Android 9.4). This issue can be tracked here on GitHub.
The suggested workaround
In case any other users come across this issue when using
Xamarin.Android 9.4, a possible workaround is to use a custom linker
configuration to preserve the missing types. To do that, add a new
linker.xml file to the project, set the Build Action to
LinkDescription, and add the XML lines to preserve the missing types.
For example, for the ITextWatcherInvoker error, add the following
lines to the file:
<linker>
<assembly fullname="Mono.Android">
<type fullname="Android.Text.ITextWatcherInvoker" preserve="all" />
</assembly>
</linker>

(MapFragment)FragmentManager.FindFragmentById(Resource.Id.map) retuns null always

I am trying to import google map in to my xamarin.android application. I set the key and written code as following `
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Maps);
SetUpMap();
FindViewElements();
}
private void SetUpMap()
{
if (mMap == null)
{
var _mapFragment = FragmentManager.FindFragmentByTag("maptag") as MapFragment;//null
var mapFragment = (MapFragment)FragmentManager.FindFragmentById(Resource.Id.map);//null
// var mapFragment = FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map);//Here are also null
// var _mapFragment = SupportFragmentManager.FindFragmentById(Resource.Id.map) as SupportMapFragment; /null
}
}
protected override async void FindViewElements()
{
_mapsModel = new MapsModel
{
MapsFragment = FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map),
MapTypeSpinner = FindViewById<Spinner>(Resource.Id.spinner),
Toolbar = FindViewById<Toolbar>(Resource.Id.mapToolbar),
};
SetSupportActionBar(_mapsModel.Toolbar);
SupportActionBar.Title = "Maps";
SupportActionBar.SetHomeButtonEnabled(true);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
_mapsModel.MapsFragment.GetMapAsync(this);
_mapsModel.MapTypeSpinner.ItemSelected += MapTypeSpinner_ItemSelected;
}
I have axml as following
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="#+id/header">
<include
android:id="#+id/mapToolbar"
layout="#layout/toolbar" />
</RelativeLayout>
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/maptype_arrays" />
<fragment
android.id="#+id/map"
android:tag="maptag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
I tried every possible way which I found like replacing Fragment to suppportFragment but nothing got helped. Can any one please suggest me to achieve this.
Thanks in advance
I try your axml, and I get the result as your, so I suggest you can add fragment in FrameLayout, so you can get this fragment by Id. Like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/zoomInButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Zoom In" />
<Button
android:id="#+id/zoomOutButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Zoom Out" />
<Button
android:id="#+id/animateButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Animate to Location" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</FrameLayout>
var mapFragment = (MapFragment)FragmentManager.FindFragmentById(Resource.Id.map);
var mapFragment = (MapFragment)FragmentManager.FindFragmentById(Resource.Id.map);
You can also take a look the MSDN document about importing Google map in your project:
https://learn.microsoft.com/en-us/xamarin/android/platform/maps-and-location/maps/maps-api

Xamarin - RecyclerView with PagerSlidingTabStrip - System.NullReferenceException in SetLayoutManager()

I try to use a RecyclerView in a SlideMenu.
I use this example for my SlideMenu : https://github.com/jamesmontemagno/PagerSlidingTabStrip-for-Xamarin.Android
And now, I add RecyclerView with SlideMenu. I need two menu : "menu" and "product", so I use two xml file for my fragment : menu.xml for "menu" and menu_recyclerView.xml for "product".
This is my code :
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true" >
<include
android:id="#+id/top_menu"
layout="#layout/top_menu"/>
<com.refractored.PagerSlidingTabStrip
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:pstsShouldExpand="true"
android:background="#color/Blue"
app:pstsDividerWidth="1dp"
app:pstsDividerPadding="12dp"
app:pstsDividerColor="#50FFFFFF"
android:textColor="#color/Green"
app:pstsTextColorSelected="#color/White"
app:pstsIndicatorColor="#color/Red"
app:pstsUnderlineColor="#color/White"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
menu_recyclerView.xml
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="#+id/menu_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v7.widget.CardView>
</LinearLayout>
row_recyclerView is a pattern for my items
row_recyclerView.xml
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:paddingRight="15dp"
android:paddingLeft="15dp"
android:orientation="vertical">
<refractored.controls.CircleImageView
android:id="#+id/imageView"
app:civ_border_width="2dp"
app:civ_border_color="#000000"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="88"
android:orientation="vertical"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingTop="7dp"
android:paddingBottom="7dp">
<TextView
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtName"
android:textColor="#000"
android:gravity="center_vertical"
android:padding="2dp"
android:textSize="18sp" />
<TextView
android:text="Brand"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtBrand"
android:textColor="#000"
android:gravity="center_vertical"
android:padding="2dp"
android:textSize="14sp"
android:singleLine="true" />
<TextView
android:text="Description"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtDescription"
android:textColor="#000"
android:gravity="center_vertical"
android:padding="2dp"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
-
public class MainActivity : BaseActivity
{
protected override int LayoutResource
{
get
{
return Resource.Layout.Main;
}
}
private Android.Widget.Toolbar _topMenu;
private Adapter _adapter;
private ViewPager _pager;
private PagerSlidingTabStrip _tabs;
private RecyclerView _recyclerView;
private LayoutManager _layoutManager;
public string _tag = "MainActivity";
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
_recyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
_layoutManager = new LayoutManager(this, _recyclerView);
_layoutManager.addItem("one", "two", "three");
_layoutManager.addItem("one", "two", "three");
_layoutManager.createLayoutManager();
_adapter = new Adapter(SupportFragmentManager);
_pager = FindViewById<ViewPager>(Resource.Id.pager);
_tabs = FindViewById<PagerSlidingTabStrip>(Resource.Id.tabs);
_pager.Adapter = _adapter;
_tabs.SetViewPager(_pager);
_topMenu = FindViewById<Android.Widget.Toolbar>(Resource.Id.top_menu);
SetActionBar(_topMenu);
}
-
class LayoutManager
{
public RecyclerView _mRecyclerView;
public RecyclerView.LayoutManager _mLayoutManager;
public RecyclerView.Adapter _mAdapter;
public ItemList<Item> _mItems;
public Activity _main;
public LayoutManager(Activity main, RecyclerView recyclerView)
{
_main = main;
_mRecyclerView = recyclerView;
_mItems = new ItemList<Item>();
}
public void createLayoutManager()
{
_mLayoutManager = new LinearLayoutManager(_main);
_mRecyclerView.SetLayoutManager(_mLayoutManager);
_mAdapter = new RecyclerAdapter(_mItems, _mRecyclerView);
_mItems.Adapter = _mAdapter;
_mRecyclerView.SetAdapter(_mAdapter);
}
public void addItem(string name, string brand, string description)
{
_mItems.Add(new Item()
{
Img = Resource.Drawable.Icon,
Name = name,
Brand = brand,
Desciption = description
});
}
}
And in this line (in my LayoutManager.cs):
_mRecyclerView.SetLayoutManager(_mLayoutManager);
I have This error :
System.NullReferenceException: Object reference not set to an instance of an object.
I do not understand what is the value of the probleme ?
I forget any thing ?
I am completely lost ?
Please.. help !
Thanks you,
Romain
According to your code: public class MainActivity : BaseActivity and
protected override int LayoutResource
{
get
{
return Resource.Layout.Main;
}
}
I guess that your BaseActivity is the same as BaseActivity.cs of PagerSlidingTabStrip sample.
Then you actually here set your Main.xml as the content view of your MainActivity, and in this Main.xml, there is no RecyclerView with the id recyclerView, it is in your menu_recyclerView.xml. Although you can find the id recyclerView when you coding, but it is not present on the content view.
So in your MainActivity, when you try to find it using _recyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);, this _recyclerView here should be null when you run your code. Then of course when you call _layoutManager = new LayoutManager(this, _recyclerView);, you pass a null to this LayoutManager. You can insert a break point on this line to check if it is a null:
_recyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
To solve your issue, I think you may need to redesign your layout or your frame.
Thanks for your answer !
Yes my BaseActivity il the same as BaseActivity.cs of PagerSlidingTabStrip sample.
I follow your advice, and yes I have a probleme with my architecture.
I learn how it works the RecyclerView, and I add this :
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true" >
<include
android:id="#+id/top_menu"
layout="#layout/top_menu"/>
<com.refractored.PagerSlidingTabStrip
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:pstsShouldExpand="true"
android:background="#color/Blue"
app:pstsDividerWidth="1dp"
app:pstsDividerPadding="12dp"
app:pstsDividerColor="#50FFFFFF"
android:textColor="#color/Green"
app:pstsTextColorSelected="#color/White"
app:pstsIndicatorColor="#color/Red"
app:pstsUnderlineColor="#color/White"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<include
layout="#layout/recyclerView" />
</LinearLayout>
menu_recyclerView.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:paddingRight="15dp"
android:paddingLeft="15dp"
android:orientation="vertical">
<refractored.controls.CircleImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:civ_border_width="2dp"
app:civ_border_color="#000000"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="88"
android:orientation="vertical"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingTop="7dp"
android:paddingBottom="7dp">
<TextView
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtName"
android:textColor="#000"
android:gravity="center_vertical"
android:padding="2dp"
android:textSize="18sp" />
<TextView
android:text="Brand"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtBrand"
android:textColor="#000"
android:gravity="center_vertical"
android:padding="2dp"
android:textSize="14sp"
android:singleLine="true" />
<TextView
android:text="Description"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtDescription"
android:textColor="#000"
android:gravity="center_vertical"
android:padding="2dp"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
I add the new xml file : recyclerView.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v7.widget.CardView>
Now I call recyclerView.xml in my Main.xml. recyclerView.xml contain CardView, and he wrap RecyclerView.
I should put my RecyclerView in a only CardView or I should to wrap my menu_recyclerView.xml with cardView ?
I want to create a menu like this :
I have a result, but now I think I have a probleme with my menu_recyclerView.xml because I don't have a list, juste "name, brand, description". screenshot :
I search for this, and is a another subject...
I think the good keyword for this probleme is : RecyclerView, CardView
for finish this is my other sources :
https://www.binpress.com/tutorial/android-l-recyclerview-and-cardview-tutorial/156
How to add listView inside cardView android?
How to implement RecyclerView with CardView rows in a Fragment with TabLayout
Thanks a lot,

Resources