[英]Can't get text from editText Android
I'm having some issues with my school project. 我的学校项目遇到一些问题。 I can't get the text from the textbox. 我无法从文本框中获取文本。 I searched for a solution but nothing found.. I'll be grateful if somebody help me :) 我正在寻找解决方案,但未找到任何解决方案。如果有人帮助我,我将不胜感激:)
So here is my Java code: 所以这是我的Java代码:
package com.src.vicnote;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class NewNoteActivity extends ActionBarActivity {
Button saveButton;
EditText textData;
Context context;
String text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_note);
saveButton = (Button) this.findViewById(R.id.buttonSave);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textData = (EditText) findViewById(R.id.editText);
text = textData.getText().toString();
//text = "this is sparta!";
Log.d("ADebugTag", "string: \"" + text + "\" end of note text");
new SaveClass(text/*, context.getApplicationContext()*/);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_note, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_new_note,
container, false);
return rootView;
}
}
}
And my XML 还有我的XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.src.vicnote.NewNoteActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="@+id/buttonSave"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Save" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/buttonSave"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
</RelativeLayout>
Also I want to ask you what will happen if the text is cyrillic? 我也想问你,如果文字是西里尔字母会怎样? Will be there any problem? 会有什么问题吗?
Can't really tell what your error is without a logCat. 没有logCat,无法真正分辨出您的错误。
but one thing that might fix your problem is replacing 但是可能会解决您问题的一件事是更换
textData = (EditText) findViewById(R.id.editText);
text = textData.getText().toString();
with 同
textData = (EditText)getActivity().findViewById(R.id.editText);
text = textData.getText().toString();
// Toast for debugging purposes
Toast.makeText(getActivity(),text,0).show();
When using Fragments you should really read about getView() & getActivity() . 使用Fragments时,您应该真正了解getView()和getActivity() 。
where in the xml have you set the text? 您在xml中的哪里设置了文本?
you need to set your text to something before you actually call the getText() command. 您需要在实际调用getText()命令之前将文本设置为某种形式。
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/buttonSave"
android:ems="10"
android:gravity="top"
----- add this line ------
android:text="whatever you want"
--------------------------
android:inputType="textMultiLine" >
Good Luck 祝好运
Adding a getActivity()
did the trick before accessing the editText. 在访问editText之前,添加getActivity()
解决问题。
For example: 例如:
EditText addressText = (EditText) getActivity().findViewById(R.id.location);
getEditableText return an editable object, and you can't edit the object like that, so it is null. getEditableText返回一个可编辑的对象,并且您不能像这样编辑该对象,因此它为null。
simply use "getText" instead. 只需使用“ getText”代替。
I don't know about Cyrillic, but it shouldn't cause any problems. 我不了解西里尔字母,但是它不会造成任何问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.