简体   繁体   English

Android findViewById返回null

[英]Android findViewById returning null

I'm using a button to call a method that just places input text into a textview. 我正在使用按钮来调用将输入文本放入textview的方法。 Whenever I use findViewById, it returns null. 每当我使用findViewById时,它都会返回null。

public void encode(View view){
  TextView out = (TextView) view.findViewById(R.id.encoderout);
  EditText in = (EditText) view.findViewById(R.id.encoderin);
  out.setText(in.getText().toString());
  out.setTypeface(font);
}

My xml is here 我的xml在这里

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
    >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/input"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    >
    <EditText android:id="@+id/encoderin"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="wrap_content"
        android:hint="Text to encode"/>
    <Button android:id="@+id/encoderbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="encode"
        android:text="Encode!"/>
</LinearLayout>
<TextView android:id="@+id/encoderout"
    android:text="test"
    android:layout_width="fill_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:layout_height="wrap_content"/>
</LinearLayout>

Almost every post I could find said that cleaning the project helped solve the problem but not in this case. 我几乎可以找到的每个帖子都说清理项目有助于解决问题,但在这种情况下并不能解决问题。

EDIT : ADDITIONAL INFO 编辑 :其他信息

I'm using Fragments, and this is my fragment code 我正在使用片段,这是我的片段代码

public static class Encoder extends Fragment{
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.encoder, container, false);       
    return view;
  }
}

It's calling up into the Activity because of the way buttons work. 由于按钮的工作方式,它正在调用Activity。 I can call the find methods fine in the fragment, but not when I have to go back to the activity. 我可以在片段中调用find方法,但是不能在必须返回活动时调用。

I can verify the view is not null, it prints out fine when the toString is called in the method. 我可以确认视图不为null,当在方法中调用toString时,它可以正常打印。

Solution : Never realized that I had to use the main view, thought I was SUPPOSE to use the button view. 解决方案 :从来没有意识到我必须使用主视图,以为我可以使用按钮视图。 I'm very new to this (started today) thankyou everyone for the input! 我对此很陌生(从今天开始),谢谢大家的投入!

<Button android:id="@+id/encoderbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="encode"
    android:text="Encode!"/>

The parameter view in callback method encode(View view) in the Java code is supplied with the Button view itself but not the Activity . Java代码中的回调方法encode(View view)中的参数view是随Button视图本身提供的,而不是由Activity

See Reference below: 请参阅下面的参考:

http://developer.android.com/reference/android/widget/Button.html http://developer.android.com/reference/android/widget/Button.html

Fragment s don't receive onClick s methods that are declared in the XML. Fragment不会接收XML中声明的onClick的方法。 You need to define encode in your Activity instead, then direct the desired behavior to the appropriate Fragment . 您需要改为在Activity定义encode ,然后将所需的行为定向到适当的Fragment Annoying, but that's the way it's done. 烦人,但这就是它的完成方式。 I suggest you just implement the OnClickListener programatically in your Fragment code to avoid intertwining the behavior of your fragment and activity. 建议您仅在Fragment代码中以编程方式实现OnClickListener ,以避免将片段和活动的行为交织在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM