我按照Android Developers的本教程编写我的应用程序以接收从其他应用程序发送的文本(特别是处理从浏览器共享发送的文本)。
我希望共享文本出现在我的应用程序的 EditText 框中(因为它会在调用 Messaging 进行共享时自动出现在 New Message 的正文中)。
这是我所做的:
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
}
}
else {
// Handle other intents, such as being started from the home screen
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
this.edWord.setText(sharedText);
}
}
我已经private EditText edWord = null;
根据需要声明并更新了 Manifest。但是,Eclipse 总是抛出以下错误:
05-17 16:32:08.239: E/AndroidRuntime(1507): FATAL EXCEPTION: main
05-17 16:32:08.239: E/AndroidRuntime(1507): java.lang.RuntimeException: Unable to start activity ComponentInfo{doyle.myapp/doyle.myapp.Myapp}: java.lang.NullPointerException
05-17 16:32:08.239: E/AndroidRuntime(1507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-17 16:32:08.239: E/AndroidRuntime(1507): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-17 16:32:08.239: E/AndroidRuntime(1507): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-17 16:32:08.239: E/AndroidRuntime(1507): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-17 16:32:08.239: E/AndroidRuntime(1507): at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 16:32:08.239: E/AndroidRuntime(1507): at android.os.Looper.loop(Looper.java:137)
05-17 16:32:08.239: E/AndroidRuntime(1507): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-17 16:32:08.239: E/AndroidRuntime(1507): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 16:32:08.239: E/AndroidRuntime(1507): at java.lang.reflect.Method.invoke(Method.java:511)
05-17 16:32:08.239: E/AndroidRuntime(1507): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-17 16:32:08.239: E/AndroidRuntime(1507): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-17 16:32:08.239: E/AndroidRuntime(1507): at dalvik.system.NativeStart.main(Native Method)
05-17 16:32:08.239: E/AndroidRuntime(1507): Caused by: java.lang.NullPointerException
05-17 16:32:08.239: E/AndroidRuntime(1507): at doyle.myapp.Myapp.handleSendText(Myapp.java:573)
05-17 16:32:08.239: E/AndroidRuntime(1507): at doyle.myapp.Myapp.onCreate(Myapp.java:437)
05-17 16:32:08.239: E/AndroidRuntime(1507): at android.app.Activity.performCreate(Activity.java:4465)
05-17 16:32:08.239: E/AndroidRuntime(1507): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-17 16:32:08.239: E/AndroidRuntime(1507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-17 16:32:08.239: E/AndroidRuntime(1507): ... 11 more
我想知道我的代码有什么问题。我应该怎么做才能解决问题?你能帮忙吗?
非常感谢您提前。