gpt4 book ai didi

java - Intent Android : click the button text is not transferred and show nothing

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

我遇到了 Intent 问题。我从 YouTube (thenewboston) 了解到,当我启动应用程序并单击按钮时,文本不会传输,也不显示任何内容。

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apples);
}


public void onClick(View view){
Intent i = new Intent(this,Bacon.class);

final EditText applesInput = (EditText)findViewById(R.id.applesInput);
String userMessage = applesInput.getText().toString();
i.putExtra("appleMessage",userMessage);

startActivity(i);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_apples, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

XML 布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Apples"
android:background="#009900">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to bacon"
android:id="@+id/applesButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="onClick"/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/applesInput"
android:layout_above="@+id/applesButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="41dp"
android:width="250dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/applesText"
android:layout_above="@+id/applesInput"
android:layout_centerHorizontal="true"
android:layout_marginBottom="41dp" />
</RelativeLayout>






<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="198dp" />
</RelativeLayout>

培根 Activity :

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bacon);

Bundle applesData =getIntent().getExtras();
if(applesData==null){
return;
}
String appleMessage = applesData.getString("applesMessage");
final TextView textView=(TextView)findViewById(R.id.textView);
textView.setText(appleMessage);
}

最佳答案

您的额外键名不匹配。您将数据作为 appleMessage 放入,但将其作为 applesMessage 检索,这将返回 null

为了确保将来不会发生这种情况,您应该在接收 Intent 的类中定义一个静态最终常量。

public static final String EXTRA_APPLE_MSG = "appleMessage";

您的源 Activity 将使用它作为

    String userMessage = applesInput.getText().toString();
i.putExtra(Bacon.EXTRA_APPLE_MSG,userMessage);

关于java - Intent Android : click the button text is not transferred and show nothing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28792263/

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