我在编辑文本中将数据转换为额外的共享意图时遇到了一些麻烦。出于某种原因,即使EditText字段已填满,它也会返回一个空字符串。 我知道有几个类似的线程关于人们无法从编辑文本字段中获取数据,但我似乎无法使这些答案适应我的代码。
我附上了相关的代码。请告诉我是否需要查看更多的代码。(我遇到问题的代码位于私有的Intent createShareIntent()中。这是第三个附件中的方法见下文)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
setContentView(R.layout.note_edit);//Set the layout file
setTitle(R.string.edit_note);//Set the title to be displayed in the action bar
mTitleText = (EditText) findViewById(R.id.title);
mBodyText = (EditText) findViewById(R.id.body);
//get the row id of the note in the database form the intent .
mRowId = (savedInstanceState == null) ? null :
(Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
if (mRowId == null) {
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
: null;
}
populateFields();
}
//THis method is used to create the menu in the Action bar
@Override//This works perfectly fine
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);//inflating the menu layout
MenuItem actionItem = menu.findItem(R.id.menu_share);//The share button in handled here
//The share action provider
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
actionProvider.setShareHistoryFileName(null);
actionProvider.setShareIntent(createShareIntent());//creating a share intent
return true;//return true if the menu creation has been successful
}
//Handles the share intent
private Intent createShareIntent() {//this is where the trouble is
saveState();
String body = mBodyText.getText().toString();//this returnes an empty string
String title = mTitleText.getText().toString();// this returnes an empty string
Intent shareIntent = new Intent(Intent.ACTION_SEND);//Setting the action to ACTION_SEND
shareIntent.setType("text/plain");//setting the text type to plain text
shareIntent.putExtra(Intent.EXTRA_TEXT,body);//this sends an empty string
shareIntent.putExtra(Intent.EXTRA_SUBJECT,title);//this sends an empty string
return shareIntent;
}
我的问题是: 1.为什么我从edittext中获取一个空字符串,即使它已被填充? 2.我该如何解决?
感谢您花时间阅读本文以及您可以提供的任何帮助。
答案 0 :(得分:0)
它返回空文本,因为在putExtra
内调用了onCreateOptionsMenu
,并且在开始时(创建选项菜单时),EditText
为空。您可以在编辑文本中添加TextChangedListener
,并在EditText
更改时更新您的共享意图。
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Update your intent here.
}
});