如何在android中将数据从一个屏幕传输到另一个屏幕?
我正在尝试将数据从一个页面传输到另一页面,但出现了一些问题。下面是代码。在我按下第二页按钮后,它强制关闭应用程序
这是我的第一个程序:
package com.life.insurance;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
/*import android.view.Menu;*/
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class LifeInsuranceActivity extends Activity implements OnClickListener{
/* private static final int MENU_STARS = 1;
private static final int MENU_REFRESH = 2;*/
Context mCtx;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button submit =(Button)findViewById(R.id.button1);
submit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.button1:
mCtx = this;
String intext = new String();
intext=((EditText)findViewById(R.id.editText1)).getText().toString();
Intent myIntent = new Intent(mCtx, MenuSelection.class);
Bundle bundle = new Bundle(1);
bundle.putString("ganna", intext);
myIntent.putExtras(bundle);
/*Start Activity*/
mCtx.startActivity(myIntent);
/*Start ActivityForResult*/
((Activity)mCtx).startActivityForResult(myIntent, 2);
setContentView(R.layout.menuselection);
break;
}
}
}
这是我的第二个活动,
package com.life.insurance;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MenuSelection extends Activity implements OnClickListener{
String ot;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menuselection);
Button search =(Button)findViewById(R.id.imageButton1);
search.setOnClickListener(this);
Bundle extras = this.getIntent().getExtras();
ot =extras.getString("ganna").toString();
final TextView r =(TextView)findViewById(R.id.txt1);
r.setText(ot);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.imageButton1:
break;
}
}
}
请帮助我执行此操作
I am trying to transfer data from one page to another but some problem is coming. below is the code.its force closing the app after i am pressing the second page button
Here my first program:
package com.life.insurance;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
/*import android.view.Menu;*/
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class LifeInsuranceActivity extends Activity implements OnClickListener{
/* private static final int MENU_STARS = 1;
private static final int MENU_REFRESH = 2;*/
Context mCtx;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button submit =(Button)findViewById(R.id.button1);
submit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.button1:
mCtx = this;
String intext = new String();
intext=((EditText)findViewById(R.id.editText1)).getText().toString();
Intent myIntent = new Intent(mCtx, MenuSelection.class);
Bundle bundle = new Bundle(1);
bundle.putString("ganna", intext);
myIntent.putExtras(bundle);
/*Start Activity*/
mCtx.startActivity(myIntent);
/*Start ActivityForResult*/
((Activity)mCtx).startActivityForResult(myIntent, 2);
setContentView(R.layout.menuselection);
break;
}
}
}
This is my second Activity
package com.life.insurance;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MenuSelection extends Activity implements OnClickListener{
String ot;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menuselection);
Button search =(Button)findViewById(R.id.imageButton1);
search.setOnClickListener(this);
Bundle extras = this.getIntent().getExtras();
ot =extras.getString("ganna").toString();
final TextView r =(TextView)findViewById(R.id.txt1);
r.setText(ot);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.imageButton1:
break;
}
}
}
please help me to do this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有 logcat 转储,很难回答,但我认为有一些事情需要检查。首先,发送字符串应该是直接的:
如果您想发送复杂的对象或捆绑包,您可以这样做,但我坚持您发送和读取字符串的问题。
我认为在你的情况下使用 Bundle 是没有必要的。
其次,您正在调用 startActivity 和 startActivityForResult。呼叫其中之一。由于您正在寻找响应,因此您可能想使用后者。
最后,确保您已将第二个活动添加到清单中,如下所示。您还希望在启动后续活动时使用活动的上下文,而不是按钮的上下文。
这里似乎有几个问题可能导致崩溃。
It's tough to answer without a logcat dump, but I think there are a few things to check. First, sending a String should be straight forward:
If you want to send complex objects or bundles, you can do that but I am sticking to your problem of sending and reading a String.
I think using Bundle, in your case, isn't necessary.
Secondly, you are calling startActivity and startActivityForResult. Call one or the other. Since you are looking for a response, you probably want to use the latter.
Finally, make sure you've added your second activity to your manifest as suggested below. You also want to use the contexts of your Activity, not of the buttons when starting subsequent activities.
There seems to be several issues here which could be causing a crash.