dav*_*_it 5 android android-fragments android-button
我是Android开发的新手,我正在尝试编写一个小而简单的应用程序.这个应用程序除了从2个EditTexts中读取文本之外什么都不做,当用户按下"发送"按钮时,它应该将文本写入2个TextViews.这些TextView中的每一个都在一个片段中.
但我无法将onClickEvent添加到Button,因为findViewById(R.id.sendTextButton)始终返回null.我试图让这个应用程序工作,但我现在无法找到任何解决方案.
以下是相关的codeBlocks:
片段的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;
}
Run Code Online (Sandbox Code Playgroud)
带有布局的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>
Run Code Online (Sandbox Code Playgroud)
和带有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"
/>
Run Code Online (Sandbox Code Playgroud)
this.inflatedView = inflater.inflate(R.layout.headline_view, container, false);
sendButton = (Button) inflatedView.findViewById(R.id.sendTextButton);
Run Code Online (Sandbox Code Playgroud)
你正在寻找一个sendTextButton内有id的按钮headline_view.xml.但是从你发布的布局中,没有带有id sendTextButton的Button.这样你就变得空了
| 归档时间: |
|
| 查看次数: |
24891 次 |
| 最近记录: |