Java Android,接受用户的值并在下一个活动中显示它们
我应该向用户询问姓名、年龄、电子邮件和电话号码,并在其他活动中显示这些值。Android Development training(来自谷歌)中的示例也使用了一个变量(键值对)。但是,这里我必须传递4个值。一抬头看如何做,我就发现了这个包裹。使用相同的方法,第二个活动不显示任何内容,即该活动显示一个空白屏幕,尽管有4个textView对象。 我的代码如下: MainActivity.javaJava Android,接受用户的值并在下一个活动中显示它们,java,android,xml,Java,Android,Xml,我应该向用户询问姓名、年龄、电子邮件和电话号码,并在其他活动中显示这些值。Android Development training(来自谷歌)中的示例也使用了一个变量(键值对)。但是,这里我必须传递4个值。一抬头看如何做,我就发现了这个包裹。使用相同的方法,第二个活动不显示任何内容,即该活动显示一个空白屏幕,尽管有4个textView对象。 我的代码如下: MainActivity.java import android.content.Intent; import android.suppor
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE_NAME="com.example.myfirstapp.MESSAGE";
public static final String EXTRA_MESSAGE_AGE="com.example.myfirstapp.MESSAGE";
public static final String EXTRA_MESSAGE_EMAIL="com.example.myfirstapp.MESSAGE";
public static final String EXTRA_MESSAGE_PH="com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view) {
Bundle extras=new Bundle();
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
extras.putString(EXTRA_MESSAGE_NAME, message);
EditText editText2 = (EditText) findViewById(R.id.editText7);
message=editText2.getText().toString();
extras.putString(EXTRA_MESSAGE_AGE, message);
EditText editText3 = (EditText) findViewById(R.id.editText5);
message=editText3.getText().toString();
extras.putString(EXTRA_MESSAGE_EMAIL, message);
EditText editText4=(EditText)findViewById(R.id.editText6);
message=editText4.getText().toString();
extras.putString(EXTRA_MESSAGE_PH, message);
intent.putExtras(extras);
startActivity(intent);
}
}
第二个活动,DisplayMessageActivity:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.w3c.dom.Text;
public class DisplayMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent=getIntent();
Bundle extras=intent.getExtras();
String message=extras.getString("EXTRA_MESSAGE_NAME");
String message1=extras.getString("EXTRA_MESSAGE_AGE");
String message2=extras.getString("EXTRA_MESSAGE_EMAIL");
String message3=extras.getString("EXTRA_MESSAGE_PH");
TextView textView= (TextView)findViewById(R.id.textView);
textView.setText(message);
TextView textView2=(TextView)findViewById(R.id.textView2);
textView2.setText(message1);
TextView textView3=(TextView)findViewById(R.id.textView3);
textView3.setText(message2);
TextView textView4=(TextView)findViewById(R.id.textView4);
textView4.setText(message3);
}
}
<EditText
android:id="@+id/editText"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:hint="@string/edit_name"
android:inputType="textPersonName"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="@string/button_send"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/editText6"
app:layout_constraintHorizontal_bias="0.501" />
<EditText
android:id="@+id/editText5"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/edit_email"
android:inputType="textEmailAddress"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText7" />
<EditText
android:id="@+id/editText6"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/edit_ph"
android:inputType="phone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText5" />
<EditText
android:id="@+id/editText7"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/edit_age"
android:inputType="number"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</android.support.constraint.ConstraintLayout>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textColor="@android:color/background_dark"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
</android.support.constraint.ConstraintLayout>
活动\u main.xml:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.w3c.dom.Text;
public class DisplayMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent=getIntent();
Bundle extras=intent.getExtras();
String message=extras.getString("EXTRA_MESSAGE_NAME");
String message1=extras.getString("EXTRA_MESSAGE_AGE");
String message2=extras.getString("EXTRA_MESSAGE_EMAIL");
String message3=extras.getString("EXTRA_MESSAGE_PH");
TextView textView= (TextView)findViewById(R.id.textView);
textView.setText(message);
TextView textView2=(TextView)findViewById(R.id.textView2);
textView2.setText(message1);
TextView textView3=(TextView)findViewById(R.id.textView3);
textView3.setText(message2);
TextView textView4=(TextView)findViewById(R.id.textView4);
textView4.setText(message3);
}
}
<EditText
android:id="@+id/editText"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:hint="@string/edit_name"
android:inputType="textPersonName"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="@string/button_send"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/editText6"
app:layout_constraintHorizontal_bias="0.501" />
<EditText
android:id="@+id/editText5"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/edit_email"
android:inputType="textEmailAddress"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText7" />
<EditText
android:id="@+id/editText6"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/edit_ph"
android:inputType="phone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText5" />
<EditText
android:id="@+id/editText7"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/edit_age"
android:inputType="number"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</android.support.constraint.ConstraintLayout>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textColor="@android:color/background_dark"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
</android.support.constraint.ConstraintLayout>
activity\u display\u message.xml:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.w3c.dom.Text;
public class DisplayMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent=getIntent();
Bundle extras=intent.getExtras();
String message=extras.getString("EXTRA_MESSAGE_NAME");
String message1=extras.getString("EXTRA_MESSAGE_AGE");
String message2=extras.getString("EXTRA_MESSAGE_EMAIL");
String message3=extras.getString("EXTRA_MESSAGE_PH");
TextView textView= (TextView)findViewById(R.id.textView);
textView.setText(message);
TextView textView2=(TextView)findViewById(R.id.textView2);
textView2.setText(message1);
TextView textView3=(TextView)findViewById(R.id.textView3);
textView3.setText(message2);
TextView textView4=(TextView)findViewById(R.id.textView4);
textView4.setText(message3);
}
}
<EditText
android:id="@+id/editText"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:hint="@string/edit_name"
android:inputType="textPersonName"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="@string/button_send"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/editText6"
app:layout_constraintHorizontal_bias="0.501" />
<EditText
android:id="@+id/editText5"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/edit_email"
android:inputType="textEmailAddress"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText7" />
<EditText
android:id="@+id/editText6"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/edit_ph"
android:inputType="phone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText5" />
<EditText
android:id="@+id/editText7"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/edit_age"
android:inputType="number"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</android.support.constraint.ConstraintLayout>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textColor="@android:color/background_dark"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
</android.support.constraint.ConstraintLayout>
任何有关这方面的帮助都将不胜感激。我对Android编程非常陌生,所以如果我犯了愚蠢的错误,请原谅,请随意指出。谢谢:)您在第二个活动中传递了错误的密钥,这就是为什么您的活动可能会在
Oncreate()中崩溃的原因
上面您已经为bundle声明了键,但在第二个活动中您犯了错误
Intent intent=getIntent();
Bundle extras=intent.getExtras();
String message=extras.getString("EXTRA_MESSAGE_NAME");
String message1=extras.getString("EXTRA_MESSAGE_AGE");
String message2=extras.getString("EXTRA_MESSAGE_EMAIL");
String message3=extras.getString("EXTRA_MESSAGE_PH");
您需要像下面这行一样传递密钥:
String message=extras.getString(MainActivity.EXTRA_MESSAGE_NAME);
您的问题是,在main活动中
声明了四个变量,所有变量的值都相同。当您将用户输入放入捆绑包时,它将始终覆盖您之前插入的值,因为只有一个单独的键。因此,您应该编辑您的变量:
public static final String EXTRA_MESSAGE_NAME="com.example.myfirstapp.MESSAGE_NAME";
public static final String EXTRA_MESSAGE_AGE="com.example.myfirstapp.MESSAGE_AGE";
public static final String EXTRA_MESSAGE_EMAIL="com.example.myfirstapp.MESSAGE_EMAIL";
public static final String EXTRA_MESSAGE_PH="com.example.myfirstapp.MESSAGE_PH";
下一个问题在于DisplayMessageActivity
类。您正试图使用在MainActivity
中声明的变量名检索插入到MainActivity
中的数据。因此,getString()
方法返回null
,因为它们没有包含变量名称的键。
您需要使用变量的值来检索该数据。根据此,将代码更改为:
String message=extras.getString(MainActivity.EXTRA_MESSAGE_NAME);
String message1=extras.getString(MainActivity.EXTRA_MESSAGE_AGE);
String message2=extras.getString(MainActivity.EXTRA_MESSAGE_EMAIL);
String message3=extras.getString(MainActivity.EXTRA_MESSAGE_PH);
由于MainActivity
中的变量是public static,因此无需MainActivity
类的实例即可访问它们
因此请记住,您可以使用变量的值来访问保存的数据,而不是它的名称
我的提示:阅读一些Java和Android教程,特别是Java教程;) 非常感谢你!工作!:D