gpt4 book ai didi

java - MyFirstApp 教程 Android Studio 2.3 Build Errors re EXTRA_MESSAGE 和 textview

转载 作者:行者123 更新时间:2023-12-02 02:43:48 24 4
gpt4 key购买 nike

MyFirstApp 教程 Android Studio 在应用程序内接收消息时出现问题。

构建错误如下:

Error:(17, 60) error: cannot find symbol variable EXTRA_MESSAGE
Error:(20, 57) error: cannot find symbol variable textView

我的消息接收器代码如下所示:

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);

// Get the Intent that started this activity and extract the
string
Intent intent = getIntent();
String message =
intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

// Capture the layout's TextView and set the string as its text
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(message);
}

注意:EXTRA_MESSAGE 和 textview 为红色。

发送代码如下所示:

    public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this,
DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}

注意:在我上面的代码中的这一行:

public void sendMessage(View view) 

第一个“View”有一条浅水平线穿过它,我收到一条错误消息,例如 sendMessage(View view) is depreciated。

我认为问题可能与

public void sendMessage(View view) 

及其折旧代码消息...(?)

出现问题的教程的链接:

https://developer.android.com/training/basics/firstapp/starting-activity.html

最佳答案

检查您的 MainActivity.java 文件中的这一行,您的代码中是否缺少。

public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

关于java - MyFirstApp 教程 Android Studio 2.3 Build Errors re EXTRA_MESSAGE 和 textview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44981541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com