【问题标题】:Call of findViewById in Fragment to find a button returns null在 Fragment 中调用 findViewById 查找按钮返回 null
【发布时间】:2013-08-02 12:15:37
【问题描述】:

我是 android 开发的新手,我正在尝试编写一个小而简单的应用程序。 这个应用程序应该只是从 2 个 EditTexts 中读取文本,当用户按下“发送”按钮时,它应该将文本写入 2 个 TextViews。这些 TextView 中的每一个都在一个 Fragment 中。

但我不能将 onClickEvent 添加到 Button,因为 findViewById(R.id.sendTextButton) 总是返回 null。我试图让这个应用程序工作,但我现在找不到任何解决方案。

以下是相关的代码块:

片段的onCreateView函数,应该处理第一个输入(HeadlineFragment.java):

Button sendButton = null;
View inflatedView = null;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    this.inflatedView = inflater.inflate(R.layout.headline_view, container, false);

    sendButton = (Button) inflatedView.findViewById(R.id.sendTextButton);

    if(sendButton == null)
    {
        Log.d("debugCheck", "HeadFrag: sendButton is null");
        return inflatedView;
    }
    sendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String headline = ((EditText) v.findViewById(R.id.enterHeadlineText)).getText().toString();
            ((TextView) v.findViewById(R.layout.headline_view)).setText(headline);
        }
    });
    return inflatedView;
}

带有布局的xml文件(activity_main.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">

        <EditText
            android:id="@+id/enterHeadlineText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/enterHeadlineTextHint"
            android:maxLines="1"
            android:inputType="textCapSentences"
        />

        <EditText
            android:id="@+id/enterMessageText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:hint="@string/enterMessageTextHint"
            android:maxLines="1"
            android:inputType="textCapSentences"
            />

        <Button
            android:id="@+id/sendTextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="@string/sendTextButton"
            />



        <fragment android:name="com.example.mysecondapplication.HeadlineFragment"
            android:id="@+id/headlineFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            />

        <fragment android:name="com.example.mysecondapplication.MessageFragment"
            android:id="@+id/messageFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            />



</LinearLayout>

以及带有 TextView 的 xml 文件,它应该包含文本 (headline_view.xml):

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/headline_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"
    android:text="@string/hello_world"
/>

【问题讨论】:

    标签: android android-fragments android-button


    【解决方案1】:
    this.inflatedView = inflater.inflate(R.layout.headline_view, container, false);
    
    sendButton = (Button) inflatedView.findViewById(R.id.sendTextButton);
    

    您正在寻找一个 ID 为 sendTextButtonheadline_view.xml 内的按钮。但是从您发布的布局中,没有 ID 为 sendTextButton 的按钮。这样你就得到了 null

    【讨论】:

    • 有没有办法从我的 Fragment 中查看 activity_main.xml 还是我必须从另一个文件访问按钮?
    • 你想达到什么目的。为什么需要在片段内的活动布局内声明一个按钮?如果它属于片段返回的视图,则应在 header_view.xm 中声明它
    • 我想设置按钮事件,改变片段中片段的内容,因为我认为这会是一种很好的风格。但我现在会尝试用其他方式来做,谢谢!
    • 这是一个很棒的解决方案
    【解决方案2】:

    您的按钮在activity_main.xml 内,但您正在膨胀R.layout.headline_view

    this.inflatedView = inflater.inflate(R.layout.headline_view, container, false);
    

    【讨论】:

      【解决方案3】:

      这就是问题

      sendButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              String headline = ((EditText) v.findViewById(R.id.enterHeadlineText)).getText().toString();
              ((TextView) v.findViewById(R.layout.headline_view)).setText(headline);
          }
      });
      

      视图 v 是单击的视图,所以当然 v.findViewById() 不起作用..
      为了实现你的目标,你可以声明你的 EditText 和 TextView 是全局的,在 onCreate() 中使用充气器来实例化它们,你可以使用它们的 onClick 方法!

      【讨论】:

        【解决方案4】:

        如果 Button 不在片段正在膨胀的布局中,则必须在管理片段的 Activity 内部而不是片段内部设置 onClickListener

        【讨论】:

          【解决方案5】:

          首先:放大视图

          然后:行动! -> 访问按钮或其他任何东西

          override fun onCreateView(
              inflater: LayoutInflater, container: ViewGroup?,
              savedInstanceState: Bundle?
          ): View? {
              val view = inflater.inflate(R.layout.notifications_fragment, container, false)
              view?.findViewById<View>(R.id.Button)?.setOnClickListener {
                  println("findViewById")
          
              }
              return view
          }
          

          【讨论】:

            猜你喜欢
            • 2012-05-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-04-02
            • 2011-03-16
            • 1970-01-01
            相关资源
            最近更新 更多