“不幸的是应用程序停止了”

时间:2015-01-27 06:23:54

标签: android smsmanager

我在andriod手机中返回发送短信的代码。 因为我写了如下代码。

package com.example.embp;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.util.*;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    boolean b;
    EditText phno;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Button btn = new Button(this);
        setContentView(btn);

        btn.setText("Send msg");
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                  String phoneNo = phno.getText().toString();
                if (!b) {
                    try {
                        sendSMS(phoneNo, "msg1");
                        Toast.makeText(MainActivity.this, "SMS Sent", Toast.LENGTH_LONG)
                                .show();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        Toast.makeText(MainActivity.this, e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }

                }
            }

        });

    }

    public void sendSMS(String number, String msg) throws Exception {
    Log.i("sendsms","");
        if (!b) {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(number, null, msg, null, null);
        }
        b = true;
    }
}

当我跑步时,它显示没有什么是好的。 当它在手机中使用时,当我按下按钮时它显示"不幸的是停止"

3 个答案:

答案 0 :(得分:1)

您尚未初始化您的EditText phno,也没有初始化您的布尔值b。在使用之前执行此操作:

  phno = new EditText(this);
  b=false;

或者如果你在xml布局中得到这个EditText:

  phno = (EditText)findViewById(R.id.YourEditTextID);

答案 1 :(得分:0)

验证两件事: 添加 2.电话号码不为空

答案 2 :(得分:0)

试试这个对我有用:

public class MainActivity extends Activity {
   boolean b;
   EditText phno;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.activity_main);
    LinearLayout ll = (LinearLayout)findViewById(R.id.info);   
    phno= new EditText (this);
    ll.addView(phno);
    Button btn = new Button(this);
    ll.addView(btn);
    btn.setText("Send msg");
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
              String phoneNo = phno.getText().toString();
            if (!b) {
                try {
                    sendSMS(phoneNo, "msg1");
                    Toast.makeText(MainActivity.this, "SMS Sent", Toast.LENGTH_LONG)
                            .show();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(MainActivity.this, e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }

            }
        }

    });

}

public void sendSMS(String number, String msg) throws Exception {
Log.i("sendsms","");
    if (!b) {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(number, null, msg, null, null);
    }
    b = true;
}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

</LinearLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.SEND_SMS"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>