我在点击时在新活动中显示前一个片段的字符串时遇到了一些麻烦。该字符串显示为" isnull"。
非常感谢任何帮助。相关代码如下。
技能活动:
public class Skill extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_skill);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent in = getIntent();
String skill = in.getStringExtra("skill");
TextView dispSkill = (TextView) findViewById(R.id.skill);
dispSkill.setText("this skill is" + skill);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
RVAdapter(上一个片段):
public static class AssessViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
CardView cv;
TextView assessName;
TextView pracName;
TextView required;
TextView reqnum;
AssessViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
assessName = (TextView)itemView.findViewById(R.id.assessment);
pracName = (TextView) itemView.findViewById(R.id.details);
required = (TextView) itemView.findViewById(R.id.required);
reqnum = (TextView) itemView.findViewById(R.id.reqnum);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, Skill.class);
context.startActivity(intent);
String assessName = ((TextView) v.findViewById(R.id.assessment))
.getText().toString();
intent.putExtra(KEY_SKILL, assessName);
System.out.println(assessName);
}
}
答案 0 :(得分:1)
正如我在您的问题下面的评论中指出的那样,问题是您在将数据附加到intent之前调用startActivity,因此在context.startActivity(intent);
方法结束时移动onClick
应该可以正常工作
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, Skill.class);
String assessName = ((TextView) v.findViewById(R.id.assessment))
.getText().toString();
intent.putExtra(KEY_SKILL, assessName);
System.out.println(assessName);
context.startActivity(intent);
}
答案 1 :(得分:0)
你的片段应该使用附加到发送的意图的相关信息来调用startActivity,这看起来就像你正在做的那样。如果您的新活动没有收到该信息(特别是因为它仍然收到" isnull,"这是用户定义的值),您应该进行调试(并测试)传入的值和键。