黑马程序员技术交流社区

标题: java.lang.IllegalStateException: Could not execute method of the activity [打印本页]

作者: 菜小徐    时间: 2014-7-3 21:33
标题: java.lang.IllegalStateException: Could not execute method of the activity
本帖最后由 菜小徐 于 2014-7-4 11:09 编辑
  1. package com.itheima.send;

  2. import java.util.ArrayList;
  3. import android.os.Bundle;
  4. import android.support.v4.app.Fragment;
  5. import android.support.v7.app.ActionBarActivity;
  6. import android.telephony.SmsManager;
  7. import android.view.LayoutInflater;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.EditText;
  13. import android.widget.Toast;

  14. public class MainActivity extends ActionBarActivity {

  15.         private EditText numET;
  16.         private EditText contentET;

  17.         @Override
  18.         protected void onCreate(Bundle savedInstanceState) {
  19.                 super.onCreate(savedInstanceState);
  20.                 setContentView(R.layout.activity_main);

  21.                 if (savedInstanceState == null) {
  22.                         getSupportFragmentManager().beginTransaction()
  23.                                         .add(R.id.container, new PlaceholderFragment()).commit();
  24.                 }
  25.                
  26.                 numET = (EditText) findViewById(R.id.edittext1);
  27.                 contentET = (EditText) findViewById(R.id.edittext2);
  28.                
  29.         }
  30.         
  31.         public void sendsms(View v){
  32.                 String number=numET.getText().toString().trim();
  33.                 String content=contentET.getText().toString().trim();
  34.                
  35.                 if(number.length()==0){
  36.                         Toast.makeText(getApplicationContext(), R.string.numnull, Toast.LENGTH_SHORT).show();
  37.                         return;
  38.                 }
  39.                 if(content.length()==0){
  40.                         Toast.makeText(getApplicationContext(), R.string.contentnull, Toast.LENGTH_SHORT).show();
  41.                         return;
  42.                 }
  43.                
  44.                 SmsManager manager=SmsManager.getDefault();
  45.                 ArrayList<String> list=manager.divideMessage(content);
  46.                 for (String str : list) {
  47.                         manager.sendTextMessage(number, null, str, null, null);
  48.                 }
  49.                
  50.                 contentET.setText("");
  51.                 Toast.makeText(getApplicationContext(), R.string.sendsuccess,Toast.LENGTH_SHORT).show();
  52.                
  53.         }
  54.         
  55.         @Override
  56.         public boolean onCreateOptionsMenu(Menu menu) {

  57.                 // Inflate the menu; this adds items to the action bar if it is present.
  58.                 getMenuInflater().inflate(R.menu.main, menu);
  59.                 return true;
  60.         }

  61.         @Override
  62.         public boolean onOptionsItemSelected(MenuItem item) {
  63.                 // Handle action bar item clicks here. The action bar will
  64.                 // automatically handle clicks on the Home/Up button, so long
  65.                 // as you specify a parent activity in AndroidManifest.xml.
  66.                 int id = item.getItemId();
  67.                 if (id == R.id.action_settings) {
  68.                         return true;
  69.                 }
  70.                 return super.onOptionsItemSelected(item);
  71.         }

  72.         /**
  73.          * A placeholder fragment containing a simple view.
  74.          */
  75.         public static class PlaceholderFragment extends Fragment {

  76.                 public PlaceholderFragment() {
  77.                 }

  78.                 @Override
  79.                 public View onCreateView(LayoutInflater inflater, ViewGroup container,
  80.                                 Bundle savedInstanceState) {
  81.                         View rootView = inflater.inflate(R.layout.fragment_main, container,
  82.                                         false);
  83.                         return rootView;
  84.                 }
  85.         }

  86. }
复制代码
  1. 07-03 13:29:25.616: D/gralloc_goldfish(1888): Emulator without GPU emulation detected.
  2. 07-03 13:29:27.956: W/KeyCharacterMap(1888): No keyboard for id 0
  3. 07-03 13:29:27.956: W/KeyCharacterMap(1888): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
  4. 07-03 13:29:33.326: D/AndroidRuntime(1888): Shutting down VM
  5. 07-03 13:29:33.326: W/dalvikvm(1888): threadid=1: thread exiting with uncaught exception (group=0xb70974f0)
  6. 07-03 13:29:33.326: E/AndroidRuntime(1888): FATAL EXCEPTION: main
  7. 07-03 13:29:33.326: E/AndroidRuntime(1888): java.lang.IllegalStateException: Could not execute method of the activity
  8. 07-03 13:29:33.326: E/AndroidRuntime(1888):         at android.view.View$1.onClick(View.java:2144)
  9. 07-03 13:29:33.326: E/AndroidRuntime(1888):         at android.view.View.performClick(View.java:2485)
  10. 07-03 13:29:33.326: E/AndroidRuntime(1888):         at android.view.View$PerformClick.run(View.java:9080)
  11. 07-03 13:29:33.326: E/AndroidRuntime(1888):         at android.os.Handler.handleCallback(Handler.java:587)
  12. 07-03 13:29:33.326: E/AndroidRuntime(1888):         at android.os.Handler.dispatchMessage(Handler.java:92)
  13. 07-03 13:29:33.326: E/AndroidRuntime(1888):         at android.os.Looper.loop(Looper.java:130)
  14. 07-03 13:29:33.326: E/AndroidRuntime(1888):         at android.app.ActivityThread.main(ActivityThread.java:3683)
  15. 07-03 13:29:33.326: E/AndroidRuntime(1888):         at java.lang.reflect.Method.invokeNative(Native Method)
  16. 07-03 13:29:33.326: E/AndroidRuntime(1888):         at java.lang.reflect.Method.invoke(Method.java:507)
  17. 07-03 13:29:33.326: E/AndroidRuntime(1888):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  18. 07-03 13:29:33.326: E/AndroidRuntime(1888):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  19. 07-03 13:29:33.326: E/AndroidRuntime(1888):         at dalvik.system.NativeStart.main(Native Method)
  20. 07-03 13:29:33.326: E/AndroidRuntime(1888): Caused by: java.lang.reflect.InvocationTargetException
  21. 07-03 13:29:33.326: E/AndroidRuntime(1888):         at java.lang.reflect.Method.invokeNative(Native Method)
  22. 07-03 13:29:33.326: E/AndroidRuntime(1888):         at java.lang.reflect.Method.invoke(Method.java:507)
  23. 07-03 13:29:33.326: E/AndroidRuntime(1888):         at android.view.View$1.onClick(View.java:2139)
  24. 07-03 13:29:33.326: E/AndroidRuntime(1888):         ... 11 more
  25. 07-03 13:29:33.326: E/AndroidRuntime(1888): Caused by: java.lang.NullPointerException
  26. 07-03 13:29:33.326: E/AndroidRuntime(1888):         at com.itheima.send.MainActivity.sendsms(MainActivity.java:74)
  27. 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)中就可以运行,求教,如何解决



作者: 菜小徐    时间: 2014-7-4 11:10
问题自己已解决




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2