0

我正在开发一个 Android 项目,我必须将数据从一个活动发送到另一个活动。但是,当我将数据从一个活动发送到另一个活动时,它不会将数据从一个活动发送到另一个活动。

从其他人接收数据的部分代码是:

Intent intent=getIntent();
String str,str1;
str=intent.getExtras().getString("phoneno"); 
Log.d("completre", "complete"); 
phonePhoneno.setText(str);

Log.d("complete","complete1");



if(intent.getExtras()==null) {
    txtMessage.setText("u duffer write program properly");
} else {
    str1=intent.getExtras().toString();
    txtMessage.setText(str1);
    Log.d("complete","complete2");
}   

我用于从其他活动发送数据的部分代码是:

if(v.getId()==R.id.main_act) {
    String str=txtText.getText().toString();
    Intent intent=new Intent(getApplicationContext(), MainActivity.class);
    intent.putExtra("username",str);
    intent.putExtra("phoneno", phoneno);
    startActivity(intent);
} 

当我尝试通过单击按钮接收数据时,它会强制关闭。

在这个项目中,我在第一个活动中进行了两项活动,我正在阅读电话号码。将其发送到第二个活动只是为了将其保存以备将来使用。在第二个活动中,我有一个编辑文本,其中我通过语音识别系统添加文本,之后我通过上面显示的单击按钮将数据电话号码和编辑文本字段从第二个活动发送到第一个活动(在代码的第二部分) . 它正在发送电话号码但不编辑文本内容(猜测),当我单击完成按钮(在第一个活动中填写两个字段)时,它会在电话号码字段中添加电话号码但不添加消息。

应用程序未自行关闭时的最新日志猫

  04-13 13:33:46.206: I/TextToSpeech(834): Sucessfully bound to com.svox.pico
04-13 13:33:46.206: W/TextToSpeech(834): speak failed: not bound to TTS engine
04-13 13:33:47.005: I/TextToSpeech(834): Connected to ComponentInfo{com.svox.pico/com.svox.pico.PicoService}
04-13 13:33:47.125: E/test(834): hhhhhhhhhhhhhhhhh
04-13 13:33:47.125: I/Choreographer(834): Skipped 174 frames!  The application may be doing too much work on its main thread.
04-13 13:33:47.575: I/Choreographer(834): Skipped 69 frames!  The application may be doing too much work on its main thread.
04-13 13:33:56.535: W/IInputConnectionWrapper(834): showStatusIcon on inactive InputConnection
04-13 13:33:56.535: I/Choreographer(834): Skipped 174 frames!  The application may be doing too much work on its main thread.
04-13 13:33:56.975: D/dalvikvm(834): GC_CONCURRENT freed 127K, 9% free 3017K/3288K, paused 37ms+5ms, total 108ms
04-13 13:34:00.274: V/(834): Got a contact result: content://com.android.contacts/data/3
04-13 13:34:00.674: V/(834): Got phone no : (986) 743-561
04-13 13:34:00.914: I/Choreographer(834): Skipped 164 frames!  The application may be doing too much work on its main thread.
04-13 13:34:01.244: I/Choreographer(834): Skipped 37 frames!  The application may be doing too much work on its main thread.
04-13 13:34:05.494: I/Choreographer(834): Skipped 139 frames!  The application may be doing too much work on its main thread.
04-13 13:34:05.974: I/Choreographer(834): Skipped 60 frames!  The application may be doing too much work on its main thread.
04-13 13:34:12.965: I/TextToSpeech(834): Sucessfully bound to com.svox.pico
04-13 13:34:12.965: W/TextToSpeech(834): speak failed: not bound to TTS engine
04-13 13:34:13.065: I/TextToSpeech(834): Connected to ComponentInfo{com.svox.pico/com.svox.pico.PicoService}
04-13 13:34:13.155: E/test(834): hhhhhhhhhhhhhhhhh
04-13 13:34:13.155: I/Choreographer(834): Skipped 35 frames!  The application may be doing too much work on its main thread.
04-13 13:34:14.425: I/Choreographer(834): Skipped 308 frames!  The application may be doing too much work on its main thread.
04-13 13:34:15.235: I/Choreographer(834): Skipped 95 frames!  The application may be doing too much work on its main thread.
04-13 13:34:16.265: I/Choreographer(834): Skipped 31 frames!  The application may be doing too much work on its main thread.
 04-13 13:34:20.025: D/completre(834): complete
 04-13 13:34:20.035: D/AndroidRuntime(834): Shutting down VM
 04-13 13:34:20.035: W/dalvikvm(834): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
 04-13 13:34:20.085: E/AndroidRuntime(834): FATAL EXCEPTION: main
 04-13 13:34:20.085: E/AndroidRuntime(834): java.lang.NullPointerException
 04-13 13:34:20.085: E/AndroidRuntime(834):     at com.example.sms.MainActivity.onClick(MainActivity.java:322)
 04-13 13:34:20.085: E/AndroidRuntime(834):     at android.view.View.performClick(View.java:4204)
 04-13 13:34:20.085: E/AndroidRuntime(834):     at android.view.View$PerformClick.run(View.java:17355)
 04-13 13:34:20.085: E/AndroidRuntime(834):     at android.os.Handler.handleCallback(Handler.java:725)
 04-13 13:34:20.085: E/AndroidRuntime(834):     at android.os.Handler.dispatchMessage(Handler.java:92)
 04-13 13:34:20.085: E/AndroidRuntime(834):     at android.os.Looper.loop(Looper.java:137)
 04-13 13:34:20.085: E/AndroidRuntime(834):     at android.app.ActivityThread.main(ActivityThread.java:5041)
 04-13 13:34:20.085: E/AndroidRuntime(834):     at java.lang.reflect.Method.invokeNative(Native Method)
 04-13 13:34:20.085: E/AndroidRuntime(834):     at java.lang.reflect.Method.invoke(Method.java:511)
 04-13 13:34:20.085: E/AndroidRuntime(834):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 04-13 13:34:20.085: E/AndroidRuntime(834):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 04-13 13:34:20.085: E/AndroidRuntime(834):     at dalvik.system.NativeStart.main(Native Method)

我想在这里提到的更多事情是,当我从第二个活动返回到主要活动而不记录任何消息时,我没有设置编辑文本的内容,并且我尝试将内容发送到第一个活动,如第二部分所示它工作的代码......理想情况下它应该强制关闭自己,因为编辑文本没有数据......我不明白为什么当时它不强制关闭自己。

4

3 回答 3

1

捆绑使用

使用它从您的变量中获取变量Intent

Intent intent = getIntent();
final Bundle bundle = intent.getExtras();
if(bundle != null) {
    String username = bundle.getString("username");
    String phoneno = bundle.getString("phoneno");
}

强制关闭

  • 确保变量strphonenonull适合您的intent.putExtra()命令。这可能会导致您的力量关闭。
  • 确保id“main_act”是在 XML 布局文件中为您的按钮定义的唯一ID。
于 2013-04-12T21:16:17.200 回答
1

好吧,经过一番挖掘,我发现了问题所在......

该问题与Bundle.

问题是你试图打电话setText()getText()打电话。该字段从未在.EditTextphonePhonenoonCreateMainActivity

所以,要解决这个问题,在你的forNullPointerException中初始化phonePhoneno为正确的,你很好;-)EditTextmain.xmlMainActivity

也许它应该像这样初始化?

phonePhoneno = (EditText) findViewById(R.id.phonenofromcontact);

我不知道?

老实说,这看起来像是一种类型或“自动完成”错误:P

只是一个小建议:清理你的代码,你可能已经Exception自己找到了;-)

于 2013-04-13T14:24:46.220 回答
0

David Manperl 说你做错了什么是正确的,你的意思是在 Intent 的地方发生了变化。如果你有一个意图没有帮助,你可以使用另一种方式。还有其他方法可以将数据传递给另一个活动。

例如:

  1. 共享首选项

  2. 不过,Application 对象是另一种方式,在处理跨多个活动的相同状态(而不是必须在任何地方获取/放置它)或比原语和字符串更复杂的对象时,它有时更容易。您可以扩展应用程序,然后在那里设置/获取您想要的任何内容,并使用 getApplication() 从任何 Activity(在同一应用程序中)访问它。

或者尝试在Activity的onCreate方法中从Intent中获取数据

于 2013-04-13T15:29:16.003 回答