【发布时间】:2015-05-23 22:11:14
【问题描述】:
像几个用户一样,我也陷入了这个问题。我尝试使用从这里获得的捆绑代码将数据从活动传递给片段,我被卡住了,因为我的应用程序在我从导航抽屉中选择该片段时立即崩溃。谁能指出我犯的愚蠢错误?
这是我创建捆绑包的活动代码:
if(code==1)
{
Bundle bundle = new Bundle();
bundle.putString("mailsession", mail);
EditMyProfile fragobj = new EditMyProfile();
fragobj.setArguments(bundle);
Intent l = new Intent(this, MainActivity.class);
startActivity(l);
Toast.makeText(getBaseContext(), "Login Successfully",
Toast.LENGTH_SHORT).show();
}
以下是片段代码:
public class EditMyProfile extends Fragment {
Button savebutton;
String emailofuser;
EditText name,age,bloodgroup,city,state,phone,email;
public EditMyProfile(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_edit_profile, container, false);
emailofuser = this.getArguments().getString("mailsession");
email=(EditText)rootView.findViewById(R.id.mailU);
email.setText(emailofuser);
name=(EditText)rootView.findViewById(R.id.nameU);
age=(EditText)rootView.findViewById(R.id.ageU);
bloodgroup=(EditText)rootView.findViewById(R.id.bgroupU);
city=(EditText)rootView.findViewById(R.id.cityU);
state=(EditText)rootView.findViewById(R.id.stateU);
phone=(EditText)rootView.findViewById(R.id.mobU);
savebutton=(Button)rootView.findViewById(R.id.Savbutton);
savebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),Login.class);
startActivity(intent);
Toast.makeText(getActivity().getApplicationContext(),"AVC",Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
【问题讨论】:
-
活动代码调用的是mainactivity类而不是fragment类
-
那是因为我在主要活动中使用导航抽屉,因此片段是主要活动的一部分。
-
你用 fragobj 做什么?看起来您从未将其添加到活动中。