我有一个viewpager,它由三个xml文件组成,但最后一个xml文件有一个EditText和一个Button.Now我希望用户在EditText中键入的内容在按钮点击时保存到共享首选项中:这些是我的类
public class CustomPagerAdapter extends PagerAdapter {
private Context context;
public CustomPagerAdapter(Context context) {
this.context = context;
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
ModelObject mo = ModelObject.values()[position];
LayoutInflater inflater = LayoutInflater.from(context);
ViewGroup layout = (ViewGroup) inflater.inflate(mo.getLayoutResId(), collection, false);
Button butIntro = (Button) layout.findViewById(R.id.introStart);
EditText= (EditText) layout.findViewById(R.id.introUsername);
butIntro.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharepreff = getSharedPreferences("StartUpInfo", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharepreff.edit();
editor.putString("username",introUser.getText().toString());
editor.apply();
}
});
collection.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
@Override
public int getCount() {
return ModelObject.values().length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public CharSequence getPageTitle(int position) {
ModelObject customPagerEnum = ModelObject.values()[position];
return context.getString(customPagerEnum.getTitleResId());
}
}
public class intro extends AppCompatActivity {
EditText introUser;
TextView username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
if(pref.getBoolean("activity_executed", false)){
Intent intent = new Intent(this, homeActivity.class);
startActivity(intent);
finish();
} else {
SharedPreferences.Editor ed = pref.edit();
ed.putBoolean("activity_executed", true);
ed.commit();
}
ViewPager intro =(ViewPager) findViewById(R.id.introviewpager);
intro.setAdapter(new CustomPagerAdapter(this));
introUser= (EditText) findViewById(R.id.introUsername);
}
public void newStart(View view){
Intent intent1 = new Intent(this, homeActivity.class);
this.startActivity(intent1);
}
}
每次运行时应用程序都会崩溃。请允许任何人帮助我弄清楚我在这里做错了什么。谢谢