按下onClick positiveButton后发送短信

时间:2014-09-22 16:24:50

标签: android eclipse text sms

"恐慌之后会打开一个对话框。按下按钮。对话框确认压脚确实很恐慌。

" YES"按下按钮,我想发送一个短信到一个号码(只是使用我的atm)

这是我到目前为止所做的:

在主菜单中:

public Panic panic;

public void panicSOS(View view) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("SOS Beacon");
    builder.setMessage("Are you sure?");

    builder.setPositiveButton("ON", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {            

                                        //Sets to panic mode
                                        panic.sendRequest(null);
                                        dialog.dismiss(); } } );

    builder.setNegativeButton("OFF", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

                                        //Sets to chill mode

                                        dialog.dismiss(); } } );

    AlertDialog alert = builder.create();
    alert.show();
}

然后在恐慌类

public class Panic extends Activity {

String phoneNumber = "04********"; // TODO: should not be static
String message = "Check-in Request";

/**
 * Factory method for creating a launch intent.
 * @param context
 * @param payload extra string input
 * @return
 */
public static Intent makeIntent(Context context, String payload) {
    return new Intent(context, Panic.class);
}

// sends a checkin request the client instantly via SMS
public void sendRequest(View view) {        
    SmsManager manager = SmsManager.getDefault();
    manager.sendTextMessage(phoneNumber, null, message, null, null);        
}

获取空指针异常和一些其他错误。有什么提示吗?

开始大幅度的logcat粗糙度

09-23 02:22:22.675:D / AndroidRuntime(4518):关闭VM 09-23 02:22:22.675:W / dalvikvm(4518):threadid = 1:线程退出未捕获异常(组= 0x41800d88)

09-23 02:22:22.675:E / AndroidRuntime(4518):致命异常:主

09-23 02:22:22.675:E / AndroidRuntime(4518):流程:com.illusivemen.smartwatchclient,PID:4518

09-23 02:22:22.675:E / AndroidRuntime(4518):java.lang.NullPointerException

09-23 02:22:22.675:E / AndroidRuntime(4518):at com.illusivemen.smartwatchclient.MainMenu $ 2.onClick(MainMenu.java:96)

09-23 02:22:22.675:E / AndroidRuntime(4518):at com.android.internal.app.AlertController $ ButtonHandler.handleMessage(AlertController.java:171)

09-23 02:22:22.675:E / AndroidRuntime(4518):在android.os.Handler.dispatchMessage(Handler.java:102)

09-23 02:22:22.675:E / AndroidRuntime(4518):在android.os.Looper.loop(Looper.java:212)

09-23 02:22:22.675:E / AndroidRuntime(4518):在android.app.ActivityThread.main(ActivityThread.java:5135)

09-23 02:22:22.675:E / AndroidRuntime(4518):at java.lang.reflect.Method.invokeNative(Native Method)

09-23 02:22:22.675:E / AndroidRuntime(4518):at java.lang.reflect.Method.invoke(Method.java:515)

09-23 02:22:22.675:E / AndroidRuntime(4518):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:877)

09-23 02:22:22.675:E / AndroidRuntime(4518):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

09-23 02:22:22.675:E / AndroidRuntime(4518):at dalvik.system.NativeStart.main(Native Method)

09-23 02:22:24.455:I / Process(4518):发送信号。 PID:4518 SIG:9

09-23 02:39:04.480:D / AndroidRuntime(5298):关闭虚拟机

09-23 02:39:04.480:W / dalvikvm(5298):threadid = 1:线程退出未捕获的异常 (组= 0x41800d88)

09-23 02:39:04.490:E / AndroidRuntime(5298):致命异常:主

09-23 02:39:04.490:E / AndroidRuntime(5298):进程:com.illusivemen.smartwatchclient,PID:5298

09-23 02:39:04.490:E / AndroidRuntime(5298):java.lang.NullPointerException

09-23 02:39:04.490:E / AndroidRuntime(5298):at com.illusivemen.smartwatchclient.MainMenu $ 2.onClick(MainMenu.java:96)

09-23 02:39:04.490:E / AndroidRuntime(5298):at com.android.internal.app.AlertController $ ButtonHandler.handleMessage(AlertController.java:171)

09-23 02:39:04.490:E / AndroidRuntime(5298):在android.os.Handler.dispatchMessage(Handler.java:102)

09-23 02:39:04.490:E / AndroidRuntime(5298):在android.os.Looper.loop(Looper.java:212)

09-23 02:39:04.490:E / AndroidRuntime(5298):在android.app.ActivityThread.main(ActivityThread.java:5135)

09-23 02:39:04.490:E / AndroidRuntime(5298):at java.lang.reflect.Method.invokeNative(Native Method)

09-23 02:39:04.490:E / AndroidRuntime(5298):at java.lang.reflect.Method.invoke(Method.java:515)

09-23 02:39:04.490:E / AndroidRuntime(5298):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:877)

09-23 02:39:04.490:E / AndroidRuntime(5298):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

09-23 02:39:04.490:E / AndroidRuntime(5298):at dalvik.system.NativeStart.main(Native Method)

09-23 02:39:05.980:I / Process(5298):发送信号。 PID:5298 SIG:9

1 个答案:

答案 0 :(得分:0)

如果Panic普通的java类,那么就不需要在其中扩展Activity。如果Panic是Activity,则不是通过创建Panic对象来调用方法,而是使用实用程序类并在两个Activity中使用:

public class UtilityClass {

String phoneNumber = "04********"; // TODO: should not be static
String message = "Check-in Request";

public static Intent makeIntent(Context context, String payload) {
    return new Intent(context, Panic.class);
}

// sends a checkin request the client instantly via SMS
public void sendRequest() {        
    SmsManager manager = SmsManager.getDefault();
    manager.sendTextMessage(phoneNumber, null, message, null, null);        
}

现在从AlertDialog和Activity中调用sendRequest

new UtilityClass().sendRequest();