IllegalStateException when click on button in android hello world program

前端 未结 5 1840
耶瑟儿~
耶瑟儿~ 2021-01-19 16:06

I\'m new to android and I\'m trying to implement MyFirstApp given on android google developers site, So app contains one textbox and button, if you enter any text in textfie

5条回答
  •  轮回少年
    2021-01-19 16:40

    Your app is looking for the sendMessage() method in an activity where the method is not implimented

    java.lang.IllegalStateException:Could not find a method MainActivity.sendMessage(View) in the activity class com.example.myfirstapp.DisplayMessageActivity for onClick handler on view class android.widget.Button

    03-15 18:00:03.430: E/AndroidRuntime(592):

    is this the xml layout name activity_display_message_1 where you declare the Button? you must put the method sendMessage also in the same activity

    EDIT: FULL SOLUTION

    activity_main.xml

     
        

    activity_display_message.xml

    
    
        
    
    

    MainActivity

    public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
        @Override
             protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    }
    
    
        /** Called when the user clicks the Send button */
        public void sendMessage(View view) {
            // Do something in response to button
            Intent intent = new Intent(this, DisplayMessageActivity.class);
            EditText editText = (EditText) findViewById(R.id.edit_message);
            String message = editText.getText().toString();
            intent.putExtra(EXTRA_MESSAGE, message);
            startActivity(intent);
        }
    }
    

    DisplayMessageActivity.java

      public class DisplayMessageActivity extends Activity {
    
        @Override
         protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message_1);
    
    
        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    
        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);
    }
    

    }

    Update also you manifest file because you've inverse the launcher

    
    
    
        
    
        
     
               
                    
                    
                 
            
    
    
    
        
    
     
    

提交回复
热议问题