Java 如何显示接收到的具有意图的字符串?
我即将进行上的培训,学习如何将带有额外(本例中为字符串)的意图传递给另一个活动,并在第二个活动中显示它。通过培训,我了解了如何创建和传递意图,但不了解如何显示额外的字符串,因为有两种方法(用于配置字符串的显示)在android studio中出现了错误(这是第二个活动): setTextSize和setText出现以下错误:Java 如何显示接收到的具有意图的字符串?,java,android,android-intent,Java,Android,Android Intent,我即将进行上的培训,学习如何将带有额外(本例中为字符串)的意图传递给另一个活动,并在第二个活动中显示它。通过培训,我了解了如何创建和传递意图,但不了解如何显示额外的字符串,因为有两种方法(用于配置字符串的显示)在android studio中出现了错误(这是第二个活动): setTextSize和setText出现以下错误: non-static method setText(CharSequence) cannot be referenced from a static context 尽管
non-static method setText(CharSequence) cannot be referenced from a static context
尽管我不确定这意味着什么,但我尝试用另一种方式显示字符串:
private TextView ausgabe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent= getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
ausgabe.setText(message);
TextView.setText(message);
setContentView(ausgabe);
}
这可能吗?如果可能的话,我如何才能最终显示字符串
为了提供一些额外的上下文,这里是第一个活动中的实现,我用来创建意图:
public static final String EXTRA_MESSAGE = "com.my_very_own.myapplication.MESSAGE";
public void sendMessage(View view){
Intent intent= new Intent(this, DisplayMessageActivity.class);
EditText editText= (EditText) findViewById(R.id.edit_message);
String message= editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
我还认为,这可能是一个java错误——为此将jdk表单7更改为8,但显然没有帮助
我非常感谢有人提议完成这项基本任务,因为我在网上搜索了几个小时,却找不到关键部分。删除这一行-
TextView.setText(message);
:)
TextView.setText(消息)
您不能直接setText(…)
到TextView小部件
。您需要创建TextView
的对象,然后应用TextView\u Object.setText(消息)代码>到对象
像这样
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
试试这个
FirstActivity.java类
{
// pass string value from first activity to second activity
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("Message", searchKey);
startActivity(intent);
}
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get intent value
Intent intent = getIntent();
strMessage = intent.getStringExtra("Message");
textView= (TextView) findViewById(R.id.message);
textView.setText(strMessage);
}
SecondActivity.class
{
// pass string value from first activity to second activity
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("Message", searchKey);
startActivity(intent);
}
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get intent value
Intent intent = getIntent();
strMessage = intent.getStringExtra("Message");
textView= (TextView) findViewById(R.id.message);
textView.setText(strMessage);
}
textView
为NULL
。