为什么我可以将数据从我的活动到片段得到数据?
我有一个主动脉,它发送登录信息,例如名称,日期,电子邮件至第二次。从该第二个动作中,我试图将这些信息添加到第二个标签中的称为ProfileFragment的片段中。我一直在遇到一个错误,说:
java.lang.nullpointerexception:尝试在null对象引用中调用虚拟方法'java.lang.string android.os.os.getstring(java.lang.string)'。
以下是我的主动行动中处理“ datePicker对话框”按钮的方法,以及将数据放入捆绑并将其发送到二极序的提交按钮。这里没有问题。
@Override
public void onClick(View v) {
//When the date button is clicked, display a calendar dialog
if (v.getId() == R.id.dateButton) {
final Calendar cal = Calendar.getInstance();
mYear = cal.get(Calendar.YEAR);
mMonth = cal.get(Calendar.MONTH);
mDay = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
dateText.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
//When submit button is clicked. Validate form data and go to next activity
if (v.getId() == R.id.submitButton) {
//Create intent for ProfileActivity
Intent SecondActivity = new Intent(MainActivity.this, SecondActivity.class);
//Create a new bundle
Bundle bundle = new Bundle();
//Convert all input data to strings
String stringName = name.getText().toString();
String stringEmail = email.getText().toString();
String stringUserName = userName.getText().toString();
String stringDate = dateText.getText().toString();
String stringDescription = description.getText().toString();
String stringOccupation = occupation.getText().toString();
try { //If age/name/email/username is invalid or under 18 then return
if ( !validateEmail() || !checkAge(convertDate(stringDate)) || !validateName() || !validateUserName() || !validateDescription() || !validateOccupation()) {
validateEmail();
validateName();
validateUserName();
validateDescription();
validateOccupation();
checkAge(convertDate(stringDate));
return;
//If age/name/username/email is valid then bundle data to send to ProfileActivity
} else {
//get users age
int age = getAge(convertDate(stringDate));
//add data to bundle
bundle.putString("name", stringName);
bundle.putString("email", stringEmail);
bundle.putString("date", stringDate);
bundle.putString("userName", stringUserName);
bundle.putString("description",stringDescription);
bundle.putString("occupation",stringOccupation);
bundle.putInt("age", age);
//Start activity
SecondActivity.putExtras(bundle);
startActivity(SecondActivity);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
这是我的第二次
public class SecondActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager2 viewPager2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tabLayout = findViewById(R.id.tablayout);
viewPager2 = findViewById(R.id.viewpager2);
viewPager2.setAdapter(createAdapter());
//create profilefragment
ProfileFragment profileFragment = new ProfileFragment();
//get bundle
Bundle bundle = getIntent().getExtras();
if(bundle != null) {
String stringDate = bundle.getString("date");
String stringName = bundle.getString("name");
String stringEmail = bundle.getString("email");
String stringUserName = bundle.getString("userName");
String stringDescription = bundle.getString("description");
String stringOccupation = bundle.getString("occupation");
int userAge = bundle.getInt("age");
//set arguments
profileFragment.setArguments(bundle);
}
}
private VpAdapter createAdapter(){
VpAdapter adapter = new VpAdapter(this);
return adapter;
}
}
class VpAdapter extends FragmentStateAdapter {
public VpAdapter(FragmentActivity fa){
super(fa);
}
private static int NUM_ITEMS = 1;
@Override
public Fragment createFragment(int position){
Fragment fragment = null;
switch(position){
case 0:
fragment = new ProfileFragment();
break;
}
return fragment;
}
@Override
public int getItemCount() {
return NUM_ITEMS;
}
}
,这是我的个人资料fragment
public class ProfileFragment extends Fragment {
TextView welcome;
TextView name;
TextView age;
TextView description;
TextView occupation;
public ProfileFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_profile, container, false);
//Receive data from secondActivity through getArguments
String mStringDate = getArguments().getString("date");
String mStringName = getArguments().getString("name");
String mStringEmail = getArguments().getString("email");
String mStringUserName = getArguments().getString("userName");
String mStringDescription = getArguments().getString("description");
String mStringOccupation = getArguments().getString("occupation");
int mUserAge = getArguments().getInt("age");
welcome = v.findViewById(R.id.welcome);
name = v.findViewById(R.id.user_name);
age = v.findViewById(R.id.age);
description = v.findViewById(R.id.description);
occupation = v.findViewById(R.id.occupation);
//set username in welcome message.
welcome.setText("Welcome " + mStringUserName);
//set username, age, description, and job into the profile fields
name.append(mStringUserName);
age.append(String.valueOf(mUserAge));
description.append(mStringDescription);
occupation.append(mStringOccupation);
// Inflate the layout for this fragment
return v;
}
我的错误指向以下“字符串mstringdate = getArguments()。getString(“ date”);”;在哪里获得空对象引用。有人可以帮我吗?
I have a MainActivity that sends login information such as name, date ,email to a SecondActivity. From that SecondActivity I am trying to add that information to a fragment called ProfileFragment that is in a tab of SecondActivity. I keep getting an error saying:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference.
Below is the method in my MainActivity that handles the a datepicker dialog button, and also the submit button that puts the data into a bundle and sends it to SecondActivity. There is no problem here.
@Override
public void onClick(View v) {
//When the date button is clicked, display a calendar dialog
if (v.getId() == R.id.dateButton) {
final Calendar cal = Calendar.getInstance();
mYear = cal.get(Calendar.YEAR);
mMonth = cal.get(Calendar.MONTH);
mDay = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
dateText.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
//When submit button is clicked. Validate form data and go to next activity
if (v.getId() == R.id.submitButton) {
//Create intent for ProfileActivity
Intent SecondActivity = new Intent(MainActivity.this, SecondActivity.class);
//Create a new bundle
Bundle bundle = new Bundle();
//Convert all input data to strings
String stringName = name.getText().toString();
String stringEmail = email.getText().toString();
String stringUserName = userName.getText().toString();
String stringDate = dateText.getText().toString();
String stringDescription = description.getText().toString();
String stringOccupation = occupation.getText().toString();
try { //If age/name/email/username is invalid or under 18 then return
if ( !validateEmail() || !checkAge(convertDate(stringDate)) || !validateName() || !validateUserName() || !validateDescription() || !validateOccupation()) {
validateEmail();
validateName();
validateUserName();
validateDescription();
validateOccupation();
checkAge(convertDate(stringDate));
return;
//If age/name/username/email is valid then bundle data to send to ProfileActivity
} else {
//get users age
int age = getAge(convertDate(stringDate));
//add data to bundle
bundle.putString("name", stringName);
bundle.putString("email", stringEmail);
bundle.putString("date", stringDate);
bundle.putString("userName", stringUserName);
bundle.putString("description",stringDescription);
bundle.putString("occupation",stringOccupation);
bundle.putInt("age", age);
//Start activity
SecondActivity.putExtras(bundle);
startActivity(SecondActivity);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
here is my SecondActivity
public class SecondActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager2 viewPager2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tabLayout = findViewById(R.id.tablayout);
viewPager2 = findViewById(R.id.viewpager2);
viewPager2.setAdapter(createAdapter());
//create profilefragment
ProfileFragment profileFragment = new ProfileFragment();
//get bundle
Bundle bundle = getIntent().getExtras();
if(bundle != null) {
String stringDate = bundle.getString("date");
String stringName = bundle.getString("name");
String stringEmail = bundle.getString("email");
String stringUserName = bundle.getString("userName");
String stringDescription = bundle.getString("description");
String stringOccupation = bundle.getString("occupation");
int userAge = bundle.getInt("age");
//set arguments
profileFragment.setArguments(bundle);
}
}
private VpAdapter createAdapter(){
VpAdapter adapter = new VpAdapter(this);
return adapter;
}
}
class VpAdapter extends FragmentStateAdapter {
public VpAdapter(FragmentActivity fa){
super(fa);
}
private static int NUM_ITEMS = 1;
@Override
public Fragment createFragment(int position){
Fragment fragment = null;
switch(position){
case 0:
fragment = new ProfileFragment();
break;
}
return fragment;
}
@Override
public int getItemCount() {
return NUM_ITEMS;
}
}
And then here is my ProfileFragment
public class ProfileFragment extends Fragment {
TextView welcome;
TextView name;
TextView age;
TextView description;
TextView occupation;
public ProfileFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_profile, container, false);
//Receive data from secondActivity through getArguments
String mStringDate = getArguments().getString("date");
String mStringName = getArguments().getString("name");
String mStringEmail = getArguments().getString("email");
String mStringUserName = getArguments().getString("userName");
String mStringDescription = getArguments().getString("description");
String mStringOccupation = getArguments().getString("occupation");
int mUserAge = getArguments().getInt("age");
welcome = v.findViewById(R.id.welcome);
name = v.findViewById(R.id.user_name);
age = v.findViewById(R.id.age);
description = v.findViewById(R.id.description);
occupation = v.findViewById(R.id.occupation);
//set username in welcome message.
welcome.setText("Welcome " + mStringUserName);
//set username, age, description, and job into the profile fields
name.append(mStringUserName);
age.append(String.valueOf(mUserAge));
description.append(mStringDescription);
occupation.append(mStringOccupation);
// Inflate the layout for this fragment
return v;
}
My error points to this line "String mStringDate = getArguments().getString("date");" where I get a null object reference. Can anyone please help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅与ViewModel一起使用一个活动
Use only one Activity with ViewModel