通过意图传递字符串的麻烦

时间:2021-08-24 22:23:49

So I have two app pages. The app is supposed to pass the pickedTask through to the second page and then it is supposed to show up on the next page. What I am getting is on the second page's creation nothing the textView assigned to change goes to nothing instead of fire. http://imgur.com/a/iymeW

所以我有两个应用页面。该应用程序应该将pickedTask传递到第二页,然后它应该显示在下一页上。我得到的是在第二页的创建,没有任何textView分配给改变什么都没有,而不是火。 http://imgur.com/a/iymeW

First page:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent goodIntentions = new Intent(getApplicationContext(), TimerList.class);
    goodIntentions.putExtra("pickedTask", "Fire");
}

public void goToTimerList(View view){
    Intent goodIntentions = new Intent(getApplicationContext(), TimerList.class);
    startActivity(goodIntentions);
}

Second Page:

        TextView mahTextView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(com.example.cluel.oc.R.layout.activity_timer_list);
    mahTextView = (TextView) findViewById(R.id.taskText);

}

public void Test(View view){
    Intent goodIntentions = getIntent();
    String mahString = goodIntentions.getStringExtra("pickedTask");
    mahTextView.setText(mahString);
}

1 个解决方案

#1


1  

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent goodIntentions = new Intent(getApplicationContext(), TimerList.class);
    goodIntentions.putExtra("pickedTask", "Fire");
}

public void goToTimerList(View view){
    Intent goodIntentions = new Intent(getApplicationContext(), TimerList.class);
    startActivity(goodIntentions);
}

You have created two different Intent objects. onCreate() creates an Intent sets the text but otherwise does nothing with it. goToTimerList() creates an Intent and immediately starts an activity with it but does not set any extras. Because you have declared each intent variable locally to each function, they are completely unrelated even though they have the same name. Since you do not need the Intent in onCreate() for anything, you should just put all of logic to start the second activity in goToTimerList() including setting the text in the Intent:

您已经创建了两个不同的Intent对象。 onCreate()创建一个Intent设置文本,但不对其进行任何操作。 goToTimerList()创建一个Intent并立即用它启动一个活动,但不设置任何额外的东西。因为您已在每个函数的本地声明每个intent变量,所以它们完全不相关,即使它们具有相同的名称。既然你不需要onCreate()中的Intent来做任何事情,你应该把所有逻辑放在goToTimerList()中启动第二个活动,包括在Intent中设置文本:

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

public void goToTimerList(View view){
    Intent goodIntentions = new Intent(getApplicationContext(), TimerList.class);
    goodIntentions.putExtra("pickedTask", "Fire");
    startActivity(goodIntentions);
}

I suggest that you learn about local variables and fields. These two topics will help you understand more about how we use variables in Java.

我建议你了解局部变量和字段。这两个主题将帮助您更多地了解我们如何在Java中使用变量。

#1


1  

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent goodIntentions = new Intent(getApplicationContext(), TimerList.class);
    goodIntentions.putExtra("pickedTask", "Fire");
}

public void goToTimerList(View view){
    Intent goodIntentions = new Intent(getApplicationContext(), TimerList.class);
    startActivity(goodIntentions);
}

You have created two different Intent objects. onCreate() creates an Intent sets the text but otherwise does nothing with it. goToTimerList() creates an Intent and immediately starts an activity with it but does not set any extras. Because you have declared each intent variable locally to each function, they are completely unrelated even though they have the same name. Since you do not need the Intent in onCreate() for anything, you should just put all of logic to start the second activity in goToTimerList() including setting the text in the Intent:

您已经创建了两个不同的Intent对象。 onCreate()创建一个Intent设置文本,但不对其进行任何操作。 goToTimerList()创建一个Intent并立即用它启动一个活动,但不设置任何额外的东西。因为您已在每个函数的本地声明每个intent变量,所以它们完全不相关,即使它们具有相同的名称。既然你不需要onCreate()中的Intent来做任何事情,你应该把所有逻辑放在goToTimerList()中启动第二个活动,包括在Intent中设置文本:

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

public void goToTimerList(View view){
    Intent goodIntentions = new Intent(getApplicationContext(), TimerList.class);
    goodIntentions.putExtra("pickedTask", "Fire");
    startActivity(goodIntentions);
}

I suggest that you learn about local variables and fields. These two topics will help you understand more about how we use variables in Java.

我建议你了解局部变量和字段。这两个主题将帮助您更多地了解我们如何在Java中使用变量。