在片段findViewById的打电话找一个按钮返回空 [英] Call of findViewById in Fragment to find a button returns null
问题描述
I'm新的Android开发和我正尝试写一个小的,简单的应用。
此应用程序应该做的无非就是读2 EditTexts文本,当用户presses发送按钮,就应该文写2 TextViews。每个这些TextViews的是在一个片段。
但我不能在onClickEvent添加到按钮,因为findViewById(R.id.sendTextButton)始终返回null。我曾试图让这个应用程序的工作,但我wasn't能够通过现在找到任何解决方案。
下面是相关$ C $个cblocks:
片段的onCreateView功能,应该处理的第一个输入(HeadlineFragment.java):
按钮sendButton = NULL;
查看inflatedView = NULL;@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){ this.inflatedView = inflater.inflate(R.layout.headline_view,集装箱,FALSE); sendButton =(按钮)inflatedView.findViewById(R.id.sendTextButton); 如果(sendButton == NULL)
{
Log.d(debugCheck,HeadFrag:sendButton为空);
返回inflatedView;
}
sendButton.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
字符串标题=((EditText上)v.findViewById(R.id.enterHeadlineText))的getText()的toString()。
((的TextView)v.findViewById(R.layout.headline_view))的setText(标题)。
}
});
返回inflatedView;
}
与布局(activity_main.xml中)的XML文件:
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直> <的EditText
机器人:ID =@ + ID / enterHeadlineText
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:提示=@字符串/ enterHeadlineTextHint
机器人:MAXLINES =1
安卓的inputType =textCapSentences
/> <的EditText
机器人:ID =@ + ID / enterMessageText
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =10dp
机器人:提示=@字符串/ enterMessageTextHint
机器人:MAXLINES =1
安卓的inputType =textCapSentences
/> <按钮
机器人:ID =@ + ID / sendTextButton
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =10dp
机器人:文字=@字符串/ sendTextButton
/> <片段机器人:名字=com.example.mysecondapplication.HeadlineFragment
机器人:ID =@ + ID / headlineFragment
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =25dp
/> <片段机器人:名字=com.example.mysecondapplication.MessageFragment
机器人:ID =@ + ID / messageFragment
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =10dp
/>< / LinearLayout中>
和与TextView的XML文件中,应包含文本(headline_view.xml):
<?XML版本=1.0编码=UTF-8&GT?;< TextView中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / headline_view
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字颜色=#FFFFFF
机器人:文字=@字符串/参考hello world
/>
this.inflatedView = inflater.inflate(R.layout.headline_view,集装箱,FALSE);sendButton =(按钮)inflatedView.findViewById(R.id.sendTextButton);
您正在寻找的id sendTextButton
A按钮放在 headline_view.xml
。但是,从布局你贴有ID为sendTextButton没有按钮。 That`s,你得到空
I´m new to android development and I´m trying to write a small, simple Application. This application should do nothing more than reading the text from 2 EditTexts and when the user presses the "send" button it should write the text to 2 TextViews. Each of these TextViews is in one Fragment.
But I can´t add the onClickEvent to the Button, because findViewById(R.id.sendTextButton) always returns null. I have tried to get this App working but I wasn´t able to find any solution by now.
Here are the relevant codeBlocks:
The onCreateView function of the fragment, that should handle the first input(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;
}
The xml file with the layout (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>
And the xml file with the TextView, that should contain the text (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"
/>
this.inflatedView = inflater.inflate(R.layout.headline_view, container, false);
sendButton = (Button) inflatedView.findViewById(R.id.sendTextButton);
you are looking for a Button with id sendTextButton
inside headline_view.xml
. But from the layout you posted there is no Button with id sendTextButton. That`s way you get null
这篇关于在片段findViewById的打电话找一个按钮返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!