我尝试将我的主要活动中的用户输入显示到我的第二个活动,但是当我输入并单击主活动上的发送按钮时,整个应用程序就崩溃了。请帮忙!
崩溃解决了!非常感谢! 但是,我无法在第二个活动中显示消息。
MainActivity.java
package com.example.dell_inspiron.sendmessage;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import static android.R.id.message;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String message = getResources().getString(R.string.UserInput);
final Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent sendMessage = new Intent(MainActivity.this, DisplayMessageActivity.class);
sendMessage.putExtra("UserInput", message);
startActivity(sendMessage);
}
});
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.dell_inspiron.sendmessage.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="@string/edit"
android:ems="10"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="15dp"
android:id="@+id/UserInput" />
<Button
android:text="@string/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/UserInput"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="14dp"
android:id="@+id/send"
android:onClick="sendMessage" />
<TextView
android:text="@string/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
DisplayMessageActivity.java
package com.example.dell_inspiron.sendmessage;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent Extra = getIntent();
String textView = Extra.getStringExtra("UserInput");
TextView UserInput = (TextView) findViewById(R.id.UserOutput);
UserInput.setText(textView);
final Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
launchActivity();
}
});
}
private void launchActivity() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
activity_display_message.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_display_message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.dell_inspiron.sendmessage.DisplayMessageActivity">
<Button
android:text="@string/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="51dp"
android:id="@+id/button2" />
<TextView
android:text="@string/UserOutput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="91dp"
android:id="@+id/UserOutput" />
<TextView
android:text="@string/enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/enter"
android:textSize="18sp"
android:textColor="@android:color/holo_purple" />
</RelativeLayout>
答案 0 :(得分:0)
您提到您正在从MainActivity获取用户输入,但我在您的代码中看不到类似的内容,但您可以执行以下操作,将某个值从一个活动传递到另一个活动...
像这样更改你的第二项活动
Bundle Extra = getIntent().getExtras();
String textView = Extra.getString("UserInput");
到
Intent Extra = getIntent();
String textView = Extra.getStringExtra("UserInput");
并像这样更改您的MainActivity
int message = R.string.UserInput;
到
String message= getResources().getString(R.string.UserInput);
和
final Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
launchActivity();
}
});
到
Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
startActivity(sendMessage);
}
});
答案 1 :(得分:0)
您正在传递int
,但是会在String
中收到该邮件,因此您的应用程序崩溃了。
<强> MainActivity 强>
int message = R.string.UserInput;
Intent sendMessage = new Intent(MainActivity.this, DisplayMessageActivity.class);
sendMessage.putExtra("UserInput", message);
<强> DisplayMessageActivity 强>
Bundle Extra = getIntent().getExtras();
String textView = Extra.getString("UserInput");
您还应该添加
intent.putExtra("UserInput", message);
进入launchActivity
方法。
解决方案1
将String textView = Extra.getString("UserInput");
更改为int textView= Extra.getIntExtra("UserInput", 0);
解决方案2
要将消息发送到第二个Activity,您需要在MainActivity按钮内单击使用intent。修改您的代码如下。
package com.example.dell_inspiron.sendmessage;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import static android.R.id.message;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String message = getResources().getString(R.string.UserInput);
final Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent sendMessage = new Intent(MainActivity.this,DisplayMessageActivity.class);
sendMessage.putExtra("UserInput", message);
startActivity(sendMessage);
}
});
}
}
解决方案3
这条线看起来不对我
final String message = getResources().getString(R.string.UserInput);
如果你想获得editText
字符串,这就是要走的路
EditText input = (EditText)findViewById(R.id.UserInput); // declare your editText
final String message = input.getText().toString(); // get your input type
答案 2 :(得分:0)
您的第二个Activity存在问题。您在Intents中传递int值但尝试获取String值。
将其更改如下:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Bundle Extra = getIntent().getExtras();
int textView = Extra.getInt("UserInput");
TextView UserInput = (TextView) findViewById(R.id.UserInput2);
UserInput.setText(textView);
final Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
launchActivity();
}
});
}
private void launchActivity() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}