我一直在尝试创建一个应用程序,将图像发送给另一个人,只发送一个图像。如果用户想要手动输入答案,则应用程序的布局之一具有文本字段,以及两个按钮:一个用于搜索联系人以便用户可以选择联系人,另一个用于发送消息。发送按钮应该确保在尝试发送之前文本字段中有输入。我发现了一种发送文本的方法,这些文本看起来比我找到的其他方法简单得多。我是android开发的新手。我在下面使用的方式是通过意图。它还没有添加图片部分,因为我想确保它首先工作,所以它只是发送hello world。它在程序中似乎没有任何错误,但是当我按下发送按钮时,会出现一个错误,表示该应用程序必须停止。你们知道我应该怎么做才能解决这个问题吗?我已经清理过几次了。谢谢你的帮助。
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
public class Send extends Activity implements OnClickListener {
EditText phoneNo = null;
String phoneNoString;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
// Set up click listener for all buttons
View sendSmsButton = findViewById(R.id.send_sms_button);
sendSmsButton.setOnClickListener(this);
View contactsButton = findViewById(R.id.contacts);
contactsButton.setOnClickListener(this);
// TODO Auto-generated method stub
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_sms_button:
phoneNoString = phoneNo.getEditableText().toString().trim();
invokesSMSApp();
}
}
public void invokesSMSApp() {
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.putExtra("sms_body", "Hello World!");
smsIntent.putExtra("address", phoneNoString);
smsIntent.setType("vnd.android-dir/mms-sms");
startActivity(smsIntent);
}
}
这些是我收到的错误:
07-15 00:56:09.213: E/AndroidRuntime(1098): FATAL EXCEPTION: main
07-15 00:56:09.213: E/AndroidRuntime(1098): java.lang.NullPointerException
07-15 00:56:09.213: E/AndroidRuntime(1098): at com.example.getaroundtoit.Send.onClick(Send.java:31)
07-15 00:56:09.213: E/AndroidRuntime(1098): at android.view.View.performClick(View.java:4204)
07-15 00:56:09.213: E/AndroidRuntime(1098): at android.view.View$PerformClick.run(View.java:17355)
07-15 00:56:09.213: E/AndroidRuntime(1098): at android.os.Handler.handleCallback(Handler.java:725)
07-15 00:56:09.213: E/AndroidRuntime(1098): at android.os.Handler.dispatchMessage(Handler.java:92)
07-15 00:56:09.213: E/AndroidRuntime(1098): at android.os.Looper.loop(Looper.java:137)
07-15 00:56:09.213: E/AndroidRuntime(1098): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-15 00:56:09.213: E/AndroidRuntime(1098): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 00:56:09.213: E/AndroidRuntime(1098): at java.lang.reflect.Method.invoke(Method.java:511)
07-15 00:56:09.213: E/AndroidRuntime(1098): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-15 00:56:09.213: E/AndroidRuntime(1098): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-15 00:56:09.213: E/AndroidRuntime(1098): at dalvik.system.NativeStart.main(Native Method)
07-15 00:56:16.835: E/Trace(1131): error opening trace file: No such file or directory (2)
07-15 00:59:44.342: E/AndroidRuntime(1131): FATAL EXCEPTION: main
07-15 00:59:44.342: E/AndroidRuntime(1131): java.lang.NullPointerException
07-15 00:59:44.342: E/AndroidRuntime(1131): at com.example.getaroundtoit.Send.onClick(Send.java:31)
07-15 00:59:44.342: E/AndroidRuntime(1131): at android.view.View.performClick(View.java:4204)
07-15 00:59:44.342: E/AndroidRuntime(1131): at android.view.View$PerformClick.run(View.java:17355)
07-15 00:59:44.342: E/AndroidRuntime(1131): at android.os.Handler.handleCallback(Handler.java:725)
07-15 00:59:44.342: E/AndroidRuntime(1131): at android.os.Handler.dispatchMessage(Handler.java:92)
07-15 00:59:44.342: E/AndroidRuntime(1131): at android.os.Looper.loop(Looper.java:137)
07-15 00:59:44.342: E/AndroidRuntime(1131): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-15 00:59:44.342: E/AndroidRuntime(1131): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 00:59:44.342: E/AndroidRuntime(1131): at java.lang.reflect.Method.invoke(Method.java:511)
07-15 00:59:44.342: E/AndroidRuntime(1131): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-15 00:59:44.342: E/AndroidRuntime(1131): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-15 00:59:44.342: E/AndroidRuntime(1131): at dalvik.system.NativeStart.main(Native Method)
模拟器在模拟器屏幕上显示一条错误消息,指出应用程序必须停止。
答案 0 :(得分:1)
你有这个:
EditText phoneNo = null;
然后你打电话:
phoneNoString = phoneNo.getEditableText().toString().trim();
但是phoneNo为空,这就是问题所在。看来你在onCreate方法中遗漏了这样的东西:
phoneNo = (EditText)findViewById(<pnoneNo ID goes here>);
希望这会有所帮助......