0

我一直在尝试创建一个将图像发送给另一个人的应用程序,只有一个图像。该应用程序的其中一种布局有一个文本字段,如果用户想要手动输入答案,还有两个按钮:一个用于在联系人中搜索以便用户选择联系人,另一个用于发送消息。如果尝试发送,发送按钮应该确保在文本字段中有输入。我找到了一种发送文本的方法,它似乎比我找到的其他方法简单得多。我是android开发的新手。我在下面使用的方式是通过意图。它还没有添加图片部分,因为我想确保它首先工作,所以它只是发送你好世界。程序中似乎没有任何错误,但是当我按下发送按钮时,会出现一个错误,提示该应用程序必须停止。你们知道我应该怎么做才能修复它吗?我已经打扫过几次了。谢谢您的帮助。

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)

模拟器会在模拟器屏幕上显示一条错误消息,指出应用程序必须停止。

4

1 回答 1

1

你有这个:

EditText phoneNo = null;

然后你打电话给:

phoneNoString = phoneNo.getEditableText().toString().trim();

但 phoneNo 为空,这就是问题所在。看来您在 onCreate 方法中缺少类似的内容:

phoneNo = (EditText)findViewById(<pnoneNo ID goes here>);

希望这可以帮助...

于 2013-07-15T01:53:55.450 回答