I'm making a simple point of sales application for my employer. A simple login screen followed by a simple tab layout displaying all the items we sell. The MainActivity is the login page and upon successful login you are brought to the point of sales activity which had a tab layout with its corresponding fragments for each tab. I have no errors and the login page work fine, until I login successfully and the second activity (which contains the tablayout) is called. I then get a crash with this error,
Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Here is the line its pointing to,
tabMeals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(0);
}
});
Here is the entirety of my posScreen activity
public class posScreen extends AppCompatActivity {
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
private TextView tabMeals, tabSides, tabCheckout;
public posScreen() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pos_screen);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//Tab Resources
tabMeals = findViewById(R.id.meals_tab);
tabSides = findViewById(R.id.sides_tab);
tabCheckout = findViewById(R.id.checkout_tab);
viewPager = findViewById(R.id.FragContainer);
tabMeals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(0);
}
});
tabSides.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(1);
}
});
tabCheckout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(2);
}
});
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
onChangeTab(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void onChangeTab(int position) {
if(position == 0) {
tabMeals.setTextSize(30);
tabSides.setTextSize(20);
tabCheckout.setTextSize(20);
} else if(position == 1) {
tabMeals.setTextSize(20);
tabSides.setTextSize(30);
tabCheckout.setTextSize(20);
} else if(position == 2) {
tabMeals.setTextSize(20);
tabSides.setTextSize(20);
tabCheckout.setTextSize(30);
}
}
}
And here is my posScreen xml file
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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="wrap_content"
tools:context=".posScreen">
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/FragContainer"
android:layout_marginTop="50dp"/>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabItem
android:id="@+id/meals_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Meals" />
<com.google.android.material.tabs.TabItem
android:id="@+id/sides_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sides and Drinks" />
<com.google.android.material.tabs.TabItem
android:id="@+id/checkout_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkout" />
</com.google.android.material.tabs.TabLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
Any help would be appreciated, I'm not super familiar with Java or Android Studio.