这是我所看到的一个完全的例外,对于标题来说太大了
java.lang.illegalstateexception:在android的父或祖先上下文中找不到方法storeemail(view):在id为“confirmfrag”的视图类com.google.android.material.button.materialbutton上定义的onclick属性
我正试图将一些数据从片段发送回它的父活动。我对安卓开发非常陌生,在意图、片段和数据传递的一般方式上都有很多困难。我原以为下面的代码最终是罚单,但似乎不是。
这是片段代码
public class EmailFragment extends Fragment {
EmailCreated activity;
public interface EmailCreated {
String onEmailCreated(Bundle emailBundle);
}
public EditText subjectLineFrag, emailFrag;
public String emails, subject;
public Button confirmFrag;
public Bundle emailBundle;
@Nullable
@org.jetbrains.annotations.Nullable
@Override
public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.email_fragment, container, false);
emailFrag = view.findViewById(R.id.emailFrag);
subjectLineFrag = view.findViewById(R.id.subjectLineFrag);
confirmFrag = view.findViewById(R.id.confirmFrag);
return view;
}
public Bundle storeEmail(View v, EditText subjectLineFrag, EditText emailFrag) {
String emails = emailFrag.getText().toString();
String subject = subjectLineFrag.getText().toString();
emailBundle.putString("message", emails);
emailBundle.putString("subject", subject);
return emailBundle;
}
@Override
public void onAttach(@NonNull @NotNull Context context) {
//context will refer to the attached activity
super.onAttach(context);
activity = (EmailCreated) context;
}
@Override
public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
//Implement Fragment Lifecycle
@Override
public void onStart() {
super.onStart();
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onAttach(@NonNull @NotNull Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
}
最初我将变量声明为private,但我在这里读到,将它们公开可能会起作用,但不会起作用
下面是它所附加到的活动的代码,非常简单,首先尝试执行片段
public class GuestActivity2 extends AppCompatActivity implements EmailFragment.EmailCreated {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guest2);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, new EmailFragment());
transaction.commit();
}
@Override
public String onEmailCreated(Bundle emailBundle) {
return null;
}
}
我不知道为什么这不起作用。我试图在活动中创建方法,因为我认为这是消息告诉我要做的,但没有被识别。更改变量不起作用。我还没有试着用这个包裹做任何事情,所以也许就这样?我有点迷路了
暂无答案!
目前还没有任何答案,快来回答吧!