android – 如何使用片段“findViewById”?

android – 如何使用片段“findViewById”?,第1张

概述我最近将项目从 Eclipse移到了 Android Studio.我试图让电子邮件功能正常工作,但我收到“findViewById”错误.我也收到“Toast.makeText”的错误.你能帮忙解决这两个错误.我班上的代码如下: package com.example.ishonours.witsbusapp;import android.app.Activity;import andro 我最近将项目从 Eclipse移到了 Android Studio.我试图让电子邮件功能正常工作,但我收到“findVIEwByID”错误.我也收到“Toast.makeText”的错误.你能帮忙解决这两个错误.我班上的代码如下:
package com.example.ishonours.witsbusapp;import androID.app.Activity;import androID.net.Uri;import androID.os.Bundle;import androID.app.Fragment;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.content.Intent;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Toast;public class ComplaintsFragment extends Fragment {//I addedprivate EditText recipIEnt;private EditText subject;private EditText body;private OnFragmentInteractionListener mListener;private static final String ARG_SECTION_NUMBER = "5";public static ComplaintsFragment newInstance(int menuNumber) {    ComplaintsFragment fragment = new ComplaintsFragment();    Bundle args = new Bundle();    args.putInt(ARG_SECTION_NUMBER,menuNumber);    fragment.setArguments(args);    return fragment;}public ComplaintsFragment() {    // required empty public constructor}@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);}@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) {    // Inflate the layout for this fragment    return  inflater.inflate(R.layout.fragment_complaints,container,false);    //i added    recipIEnt = (EditText) findVIEwByID(R.ID.recipIEnt);    subject = (EditText) findVIEwByID(R.ID.subject);    body = (EditText) findVIEwByID(R.ID.body);    button sendBtn = (button) findVIEwByID(R.ID.sendEmail);    sendBtn.setonClickListener(new VIEw.OnClickListener() {        public voID onClick(VIEw vIEw) {            sendEmail();            // after sending the email,clear the fIElds            recipIEnt.setText("");            subject.setText("");            body.setText("");        }    });}//i addedprotected voID sendEmail() {    String[] recipIEnts = {recipIEnt.getText().toString()};    Intent email = new Intent(Intent.ACTION_SEND,Uri.parse("mailto:"));    // prompts email clIEnts only    email.setType("message/rfc822");    email.putExtra(Intent.EXTRA_EMAIL,recipIEnts);    email.putExtra(Intent.EXTRA_SUBJECT,subject.getText().toString());    email.putExtra(Intent.EXTRA_TEXT,body.getText().toString());    try {        // the user can choose the email clIEnt        startActivity(Intent.createChooser(email,"Choose an email clIEnt from..."));    } catch (androID.content.ActivityNotFoundException ex) {        Toast.makeText(ComplaintsFragment.this,"No email clIEnt installed.",Toast.LENGTH_LONG).show();    }}public voID onbuttonpressed(Uri uri) {    if (mListener != null) {        mListener.onFragmentInteraction(uri);    }}@OverrIDepublic voID onAttach(Activity activity) {    super.onAttach(activity);    ((MainActivity) activity).onSectionAttached(            getArguments().getInt(ARG_SECTION_NUMBER));}@OverrIDepublic voID onDetach() {    super.onDetach();    mListener = null;}public interface OnFragmentInteractionListener {    // Todo: Update argument type and name    public voID onFragmentInteraction(Uri uri);}

}

我的xml文件中的代码是:

<linearLayout 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"    androID:orIEntation="vertical" >    tools:context="com.example.ishonours.witsbusapp.ComplaintsFragment"><TextVIEw    androID:ID="@+ID/text1"    androID:layout_wIDth="fill_parent"    androID:layout_height="wrap_content"    androID:layout_marginBottom="20dp"    androID:gravity="center"    androID:text="Complete the fIElds to log a complaint"    androID:textAppearance="?androID:attr/textAppearanceMedium" /><EditText    androID:ID="@+ID/recipIEnt"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:ems="10"    androID:inputType="textEmailAddress"    androID:hint="RecipIEnt" /><EditText    androID:ID="@+ID/subject"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:ems="10"    androID:hint="Subject" /><EditText    androID:ID="@+ID/body"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:ems="10"    androID:hint="Message body" /><button    androID:ID="@+ID/sendEmail"    androID:layout_wIDth="fill_parent"    androID:layout_height="wrap_content"    androID:layout_margintop="30dp"    androID:text="Compose an email" />
解决方法 问题出在你的onCreateVIEw方法中.
@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater,Bundle savedInstanceState) {     // Inflate the layout for this fragment     VIEw v = inflater.inflate(R.layout.fragment_complaints,false);     //i added     recipIEnt = (EditText) v.findVIEwByID(R.ID.recipIEnt);     [...]     return v;}

看到不同?在碎片的情况下,您必须在实际的VIEw对象上调用findVIEwByID.

你有Toast的问题是因为你传递了worng对象作为第一个参数.你需要Context而你正在传递Fragment.片段不是一个上下文但幸运的是你的活动就是这样,你必须像这样构建你的Toast:

Toast.makeText(ComplaintsFragment.this.getActivity(),Toast.LENGTH_LONG).show();

注意getActivity()调用.

总结

以上是内存溢出为你收集整理的android – 如何使用片段“findViewById”?全部内容,希望文章能够帮你解决android – 如何使用片段“findViewById”?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1134115.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存