本帖最后由 菜小徐 于 2014-7-4 11:09 编辑
- package com.itheima.send;
- import java.util.ArrayList;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v7.app.ActionBarActivity;
- import android.telephony.SmsManager;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends ActionBarActivity {
- private EditText numET;
- private EditText contentET;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- if (savedInstanceState == null) {
- getSupportFragmentManager().beginTransaction()
- .add(R.id.container, new PlaceholderFragment()).commit();
- }
-
- numET = (EditText) findViewById(R.id.edittext1);
- contentET = (EditText) findViewById(R.id.edittext2);
-
- }
-
- public void sendsms(View v){
- String number=numET.getText().toString().trim();
- String content=contentET.getText().toString().trim();
-
- if(number.length()==0){
- Toast.makeText(getApplicationContext(), R.string.numnull, Toast.LENGTH_SHORT).show();
- return;
- }
- if(content.length()==0){
- Toast.makeText(getApplicationContext(), R.string.contentnull, Toast.LENGTH_SHORT).show();
- return;
- }
-
- SmsManager manager=SmsManager.getDefault();
- ArrayList<String> list=manager.divideMessage(content);
- for (String str : list) {
- manager.sendTextMessage(number, null, str, null, null);
- }
-
- contentET.setText("");
- Toast.makeText(getApplicationContext(), R.string.sendsuccess,Toast.LENGTH_SHORT).show();
-
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.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();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- /**
- * A placeholder fragment containing a simple view.
- */
- public static class PlaceholderFragment extends Fragment {
- public PlaceholderFragment() {
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.fragment_main, container,
- false);
- return rootView;
- }
- }
- }
复制代码- 07-03 13:29:25.616: D/gralloc_goldfish(1888): Emulator without GPU emulation detected.
- 07-03 13:29:27.956: W/KeyCharacterMap(1888): No keyboard for id 0
- 07-03 13:29:27.956: W/KeyCharacterMap(1888): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
- 07-03 13:29:33.326: D/AndroidRuntime(1888): Shutting down VM
- 07-03 13:29:33.326: W/dalvikvm(1888): threadid=1: thread exiting with uncaught exception (group=0xb70974f0)
- 07-03 13:29:33.326: E/AndroidRuntime(1888): FATAL EXCEPTION: main
- 07-03 13:29:33.326: E/AndroidRuntime(1888): java.lang.IllegalStateException: Could not execute method of the activity
- 07-03 13:29:33.326: E/AndroidRuntime(1888): at android.view.View$1.onClick(View.java:2144)
- 07-03 13:29:33.326: E/AndroidRuntime(1888): at android.view.View.performClick(View.java:2485)
- 07-03 13:29:33.326: E/AndroidRuntime(1888): at android.view.View$PerformClick.run(View.java:9080)
- 07-03 13:29:33.326: E/AndroidRuntime(1888): at android.os.Handler.handleCallback(Handler.java:587)
- 07-03 13:29:33.326: E/AndroidRuntime(1888): at android.os.Handler.dispatchMessage(Handler.java:92)
- 07-03 13:29:33.326: E/AndroidRuntime(1888): at android.os.Looper.loop(Looper.java:130)
- 07-03 13:29:33.326: E/AndroidRuntime(1888): at android.app.ActivityThread.main(ActivityThread.java:3683)
- 07-03 13:29:33.326: E/AndroidRuntime(1888): at java.lang.reflect.Method.invokeNative(Native Method)
- 07-03 13:29:33.326: E/AndroidRuntime(1888): at java.lang.reflect.Method.invoke(Method.java:507)
- 07-03 13:29:33.326: E/AndroidRuntime(1888): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
- 07-03 13:29:33.326: E/AndroidRuntime(1888): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
- 07-03 13:29:33.326: E/AndroidRuntime(1888): at dalvik.system.NativeStart.main(Native Method)
- 07-03 13:29:33.326: E/AndroidRuntime(1888): Caused by: java.lang.reflect.InvocationTargetException
- 07-03 13:29:33.326: E/AndroidRuntime(1888): at java.lang.reflect.Method.invokeNative(Native Method)
- 07-03 13:29:33.326: E/AndroidRuntime(1888): at java.lang.reflect.Method.invoke(Method.java:507)
- 07-03 13:29:33.326: E/AndroidRuntime(1888): at android.view.View$1.onClick(View.java:2139)
- 07-03 13:29:33.326: E/AndroidRuntime(1888): ... 11 more
- 07-03 13:29:33.326: E/AndroidRuntime(1888): Caused by: java.lang.NullPointerException
- 07-03 13:29:33.326: E/AndroidRuntime(1888): at com.itheima.send.MainActivity.sendsms(MainActivity.java:74)
- 07-03 13:29:33.326: E/AndroidRuntime(1888): ... 14 more
复制代码 numET = (EditText) findViewById(R.id.edittext1); contentET = (EditText) findViewById(R.id.edittext2); 这两句放在onCreate里面就会报java.lang.IllegalStateException: Could not execute method of the activity 这个错误,但放在自定义的方法体(sendsms)中就可以运行,求教,如何解决
|