I'm particularly new to this field. I'm trying to create a login page using fragments and it crashed when I tried to run it. I checked the logcat, turns out its a NullPointerException. It highlighted the error here:
register = (Button) findViewById(R.id.RegisterUser);
register.setOnClickListener(this);
Here is my code
this is mainactivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
register = (Button) findViewById(R.id.RegisterUser);
register.setOnClickListener(this);
ViewPager viewPager = findViewById(R.id.viewPager);
AuthenticationPagerAdapter pagerAdapter = new
AuthenticationPagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragmet(new LoginFragment());
pagerAdapter.addFragmet(new RegisterFragment());
viewPager.setAdapter(pagerAdapter);
}
public void onClick(View v){
switch (v.getId()){
case R.id.RegisterUser:
startActivity(new Intent(this, RegisterUser.class));
break;
}
}
class AuthenticationPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragmentList = new ArrayList<>();
public AuthenticationPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
@Override
public int getCount() {
return fragmentList.size();
}
void addFragmet(Fragment fragment) {
fragmentList.add(fragment);
}
}
}
CodePudding user response:
It seems you define the global field private TextView register;
but you want to cast it to Button register = (Button) findViewById(R.id.RegisterUser);
Please check your XML file and make sure what's your element TextView or Button.
CodePudding user response:
can you please check your activity_main.xml
file
check into that is a Button
or Textview