【发布时间】:2013-06-14 08:16:17
【问题描述】:
我在将字符串从片段传递到另一个活动时遇到问题。 我尝试了许多使用意图传递它们的方法(例如普通的附加组件、捆绑包),但附加组件在活动中始终为空。
我看过
http://www.vogella.com/articles/AndroidIntent/ 但是没有变化 http://developer.android.com/training/basics/firstapp/starting-activity.html这种传数据的方法也不行
关于 stackoverflow 的其他类似问题 - 但这些并不完全相同
我要做的是获取在片段中的两个 EditTexts 中输入的文本,然后将该文本传递给两个 EditTexts 填充相同文本的活动。问题是活动中的两个 EditText 中没有出现任何内容。我知道片段中的 EditText 正在工作,因为可以使用它们创建通知。
我的代码:我删除了我认为不必要的内容,例如将片段添加到导航抽屉布局。请原谅缺少括号 - 我已经删除了很多代码,有些可能是不小心被删除的! :-)
这是我创建意图的片段:
// Package declaring and importing stuff
public class QuickNoteFragment extends Fragment implements OnClickListener {
// Removed some stuff
EditText body;
EditText title;
Button create;
int counter = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.quicknote, container, false);
body = (EditText) rootView.findViewById(R.id.qn_et_body);
title = (EditText) rootView.findViewById(R.id.qn_et_title);
create.setOnClickListener(this);
// Removed stuff
getActivity().setTitle(noter_activity); // Part of navigation drawer?
return rootView;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.qn_b_create:
String content_text = body.getText().toString();
String content_title = title.getText().toString();
if (content_title.length() >=1){
Context context = v.getContext();
// This intent does not seem to work
Intent eIntent = new Intent(context, QuickNoteEdit.class);
eIntent.putExtra("eTitle", content_title);
eIntent.putExtra("eText", content_text);
PendingIntent EditPendingIntent = PendingIntent.getActivity(context, 0, eIntent, 0);
// This intent works comletely. This is called when a notification action button is pressed
Intent qnCancel = new Intent();
qnCancel.setAction("com.RiThBo.noter.qnCancelBroadcast");
Bundle extras = new Bundle();
extras.putInt("valueOfCounter", counter);
qnCancel.putExtras(extras);
startBroadcast(qnCancel);
PendingIntent pQnCancel = PendingIntent.getBroadcast(this.getActivity(), 0, qnCancel, 0);
// Creates Notification
} else {
// Do something
}
case R.id.*** // Does something else
}
}
private void startBroadcast(Intent qnCancel) { // This is part of the correctly working intent
// TODO Auto-generated method stub
}
}
这是我想要获得额外奖励的活动
// Removed package and imports
public class QuickNoteEdit extends Activity implements OnClickListener {
EditText body;
EditText title;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.quicknote_edit);
variableConnector(); // This gets the id for all the items in the xml
Intent intent = getIntent();
String gotTitle = intent.getStringExtra("content_title"); // This is where I think it equals null. Because the
String gotBody = intent.getStringExtra("content_text");
title.setText(gotTitle);
body.setText(gotBody);
}
private void variableConnector() {
// TODO Auto-generated method stub
body = (EditText) findViewById(R.id.qne_et_body);
title = (EditText) findViewById(R.id.qne_et_title);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
谢谢
【问题讨论】:
标签: android android-intent bundle extras