这向我显示了空指针异常:
if (!bundle.getString("username").equals("start")) {
因为我无法获得所需的字符串。从字面上的最后几个小时,我真的受够了。请帮我解决一下这个。谢谢。
public class HomeActivity extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
private boolean isUserClickedBackButton = false;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = findViewById(R.id.action_bar);
toolbar.setTitleTextColor(getResources().getColor(R.color.black));
setSupportActionBar(toolbar);
tabLayout = findViewById(R.id.tabLayout);
viewPager = findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new EmployerFragment(),"Employer Login");
viewPagerAdapter.addFragments(new AdminFragment(),"Admin Login");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
Bundle bundle = new Bundle();
bundle.putString("username", getIntent().getExtras().getString("username"));
bundle.putString("password", getIntent().getExtras().getString("password"));
EmployerFragment employerFragment = new EmployerFragment();
employerFragment.setArguments(bundle);
}
@Override
public void onBackPressed() {
if(!isUserClickedBackButton)
{
Toast.makeText(this, "Press back key again to exit !!!", Toast.LENGTH_SHORT).show();
isUserClickedBackButton=true;
handler.postDelayed(new Runnable() {
@Override
public void run() {
isUserClickedBackButton=false;
}
},3000);
}
else
{
finish();
System.exit(0);
}
}
}
public class EmployerFragment extends Fragment {
EditText user, pass;
Button login, register;
View view;
public EmployerFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_employer, container, false);
login = view.findViewById(R.id.loginEmp);
register = view.findViewById(R.id.registerEmp);
user = view.findViewById(R.id.userEmp);
pass = view.findViewById(R.id.passEmp);
Bundle bundle = getArguments();
if (!bundle.getString("username").equals("start")) {
user.setText(bundle.getString("username"));
pass.setText(bundle.getString("password"));
}
login.requestFocus();
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent administrator = new Intent(getActivity(), EmployerPanel.class);
if (user.getText().toString().equals("")) {
Toast.makeText(getActivity(), "Please enter username", Toast.LENGTH_SHORT).show();
user.requestFocus();
} else if (pass.getText().toString().equals("")) {
Toast.makeText(getActivity(), "Please enter password", Toast.LENGTH_SHORT).show();
pass.requestFocus();
} else if (user.getText().toString().equals("Employer") && pass.getText().toString().equals("employer")) {
startActivity(administrator);
getActivity().finish();
} else {
user.setText("");
pass.setText("");
Toast.makeText(getActivity(), "Invalid Username or Password", Toast.LENGTH_LONG).show();
user.requestFocus();
}
}
});
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent register = new Intent(getActivity(), RegisterEmployer.class);
startActivity(register);
getActivity().finish();
}
});
return view;
}
}