我正在开发一个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);
}
当我尝试通过点击按钮接收数据时,它会强行关闭。
在这个项目中,我在第一项活动中有两项活动,我正在阅读电话号码。将其发送到第二个活动只是为了将来保存。在第二个活动中,我有一个编辑文本,其中我通过语音识别系统添加文本,之后我发送数据电话号码和编辑文本字段从第二个活动到第一个活动通过上面显示的clcking按钮(在代码的第二部分) )。当我点击完成按钮(填写第一个活动中的两个字段)时,它正在发送电话号码但不编辑文本内容(猜测),然后它在电话号码中添加电话号码,但不添加消息。
最新的日志猫 当应用程序没有关闭时
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)
我想在这里提到的更多的事情是,当我从第二个活动返回到main时没有记录任何消息,我没有设置编辑文本的内容,我试图将内容发送到第一个活动,如图所示它工作的第二部分代码......理想情况下它应该强制关闭,因为编辑文本没有数据..我无法理解为什么当时不强制关闭它。
答案 0 :(得分:1)
捆绑使用:
使用它来获取Intent
:
Intent intent = getIntent();
final Bundle bundle = intent.getExtras();
if(bundle != null) {
String username = bundle.getString("username");
String phoneno = bundle.getString("phoneno");
}
强制关闭:
str
和phoneno
不是null
你的intent.putExtra()
命令。这可能会导致你的部队关闭。id
“main_act”是在 唯一 ID中定义的
按钮的XML布局文件。答案 1 :(得分:1)
好的,经过一番挖掘后,我发现了问题是什么......
此问题与Bundle
的值无关。
问题是,您正试图在名为setText()
的{{1}}上致电getText()
和EditText
。此字段从未在phonePhoneno
的{{1}}方法中初始化。
因此,要解决此问题onCreate
,请将MainActivity
初始化为NullPointerException
phonePhoneno
内EditText
的{{1}},然后您就可以了; - )
也许它应该像这样初始化?
main.xml
我不知道?
说实话,它看起来像是一种类型或“自动完成”错误:P
只是一点建议:清理你的代码,你可能已经找到了自己的MainActivity
; - )
答案 2 :(得分:0)
大卫曼珀尔说你做错了什么意思是意图改变了。 如果你的意图没有帮助,你可以用另一种方式。还有其他方法可以传递数据另一个活动。
例如:
Application对象是另一种方式,有时在多个活动中处理相同的状态(而不是必须将其放到任何地方)或者比原语和字符串更复杂的对象时更容易。 您可以扩展Application,然后设置/获取您想要的任何内容,并使用getApplication()从任何Activity(在同一个应用程序中)访问它。
或者尝试在Activity的onCreate方法中从Intent获取数据