button.onclick()导致"不幸的是xxx已停止工作

时间:2015-08-12 13:37:12

标签: android

Android的新手。尝试基于Web上的示例制作简单的SMS文本信使(用于更高目的)。代码如下。手机是S4 mini(API19)。 " sendSMSmessage()"系统正常工作(文本消息正确发送)被称为onCreate的一部分。一旦我尝试设置onClick中断 - 代码响应(在电话上)"不幸的是xxx已停止工作"。尝试扩展活动和AppCompatActivity(如注释所示)。是否缺少onClick方法调用的某种设置/引用?谢谢你的帮助。

package com.example.tas_pb_usr1.ArtSafe;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

//public class MainActivity extends AppCompatActivity {
    public class MainActivity extends Activity {
//    Button sendBtn = (Button) findViewById(R.id.Sms_send_button);
     Button sendBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       kkBtnSetup();
          sendSMSMessage();
}
    protected void sendSMSMessage() {
        Log.i("Send SMS", "");
/*        String phoneNo = txtphoneNo.getText().toString();
        String message = txtMessage.getText().toString();*/

        String phoneNo = "1234567";
        String currentDateTimeString = DateFormat.getDateTimeInstance().toString();
        Calendar c = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = df.format(c.getTime());
        String message = "Some SMS text   " +  formattedDate;

        try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNo, null, message, null, null);
            Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
        }

        catch (Exception e) {
            Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void kkBtnSetup(){
        sendBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                sendSMSMessage();
            }
        });
    }


}

这是布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send SMS"
        android:id="@+id/Sms_send_button"
        android:layout_below="@+id/textView"
        android:layout_toEndOf="@+id/textView"
        android:layout_marginTop="93dp" />

</RelativeLayout>

6 个答案:

答案 0 :(得分:1)

请使用这种方式,如果您使用此逻辑,则无需添加按钮onclickListner和kkBtnSetup()

<Button
 android:id="@+id/Sms_send_button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Send SMS"
 android:layout_below="@+id/textView"
 android:layout_toEndOf="@+id/textView"
 android:onClick="sendSMSMessage"/>

使用android:onClick属性,我们声明必须存在于父活动上的方法名称。

    public void sendSMSMessage(View v)
        {
        String phoneNo = "1234567";
        String currentDateTimeString = DateFormat.getDateTimeInstance().toString();
        Calendar c = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = df.format(c.getTime());
        String message = "Some SMS text   " +  formattedDate;

        try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNo, null, message, null, null);
            Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
        }

        catch (Exception e) {
            Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
}

答案 1 :(得分:1)

请在onCreate方法中添加该行。

 public class MainActivity extends Activity {

     Button sendBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     sendBtn = (Button) findViewById(R.id.Sms_send_button);
       kkBtnSetup();
          sendSMSMessage();
}
 protected void sendSMSMessage() {
        Log.i("Send SMS", "");
/*        String phoneNo = txtphoneNo.getText().toString();
        String message = txtMessage.getText().toString();*/

        String phoneNo = "1234567";
        String currentDateTimeString = DateFormat.getDateTimeInstance().toString();
        Calendar c = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = df.format(c.getTime());
        String message = "Some SMS text   " +  formattedDate;

        try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNo, null, message, null, null);
            Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
        }

        catch (Exception e) {
            Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void kkBtnSetup(){
        sendBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                sendSMSMessage();
            }
        });
    }


}

答案 2 :(得分:0)

发送按钮未定义且为空,因此您应该成为NullPointerException。添加此行

Button sendBtn = (Button) findViewById(R.id.Sms_send_button);
在调用kkBtnSetup()方法之前

进入onCreate方法

所以你的onCreate方法应如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sendBtn = (Button) findViewById(R.id.Sms_send_button);
    kkBtnSetup();
    sendSMSMessage();
}

答案 3 :(得分:0)

在设置点击监听器之前,您没有初始化按钮。像这样。

Button sendBtn;

你应该使用

sendBtn = (Button) findViewById(R.id.Sms_send_button);

onCreate()里面就像这样

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sendBtn = (Button) findViewById(R.id.Sms_send_button);
    kkBtnSetup();
    sendSMSMessage();
}

答案 4 :(得分:0)

使用

初始化发送按钮
sendBtn = (Button) findViewById(R.id.Sms_send_button);

这一行之后

setContentView(R.layout.activity_main);

答案 5 :(得分:0)

我认为你必须在使用它之前初始化按钮,在你的函数kkBtnSetup()

sendBtn = (Button) findViewById(R.id.Sms_send_button);