提问人:FranyxD 提问时间:11/15/2021 更新时间:11/15/2021 访问量:540
我在按钮点击监听器中收到错误 (Android Studio)
I get an error in the button click listener (Android Studio)
问:
我制作了一个应用程序来单独发送邮件,我正在尝试在另一个应用程序中实现它,好吧,它给了我这个错误尝试调用虚拟方法
空对象引用上的“void android.widget.Button.setOnClickListener (android.view.View $ OnClickListener)”
我有我知道的id,放置按钮和点击监听器的方法很好。错误会带来什么?
这是我的代码
public class ContactFragment extends Fragment {
//public EditText mEmail;
public EditText mSubject;
public EditText mMessage;
private Button buttonSend_contact;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
public ContactFragment() {
}
public static ContactFragment newInstance(String param1, String param2) {
ContactFragment fragment = new ContactFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_videos, container, false);
mMessage = view.findViewById(R.id.messageID);
mSubject = view.findViewById(R.id.subjectID);
buttonSend_contact = view.findViewById(R.id.buttonSendMail);
buttonSend_contact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendMail();
}
});
return view;
}
private void sendMail() {
String mail = "[email protected]";
String message = mMessage.getText().toString();
String subject = mSubject.getText().toString().trim();
//Send Mail
JavaMailAPI javaMailAPI = new JavaMailAPI(getActivity(),mail,subject,message);
javaMailAPI.execute();
}
这是我的XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ContactFragment">
<!-- <EditText
android:id="@+id/mailID"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"/> -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/subjectID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Subject" />
<EditText
android:id="@+id/messageID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Message" />
<Button
android:id="@+id/buttonSendMail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</FrameLayout>
答: 暂无答案
评论
inflater.inflate(R.layout.fragment_videos, container, false)
R.layout.fragment_contact