IllegalStateException when click on button in android hello world program

前端 未结 5 1801
耶瑟儿~
耶瑟儿~ 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:33

    //Just change your manifest file, its not getting path of your "DisplayMessageActivity" for //sample as below :

     android:name="com.example.checkproject.DisplayMessageActivity"
    

    or

    android:name =".DisplayMessageActivity"
    
    0 讨论(0)
  • 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</p>
    <p>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

     <LinearLayout 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" >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_send"
            android:onClick="sendMessage" />
    </LinearLayout>
    

    activity_display_message.xml

    <LinearLayout 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" >
    
        <EditText android:id="@+id/edit_message"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="@string/edit_message" />
    </LinearLayout>
    

    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

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.myfirstapp"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="11" />
    
        <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/title_activity_display_message">
               <intent-filter >
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter> 
            </activity>
    
    
    <activity
                android:name=".DisplayMessageActivity" />
        </application>
    
    </manifest> 
    
    0 讨论(0)
  • 2021-01-19 16:43

    You are using two time setContentView(). You can use only one setContentView .

    0 讨论(0)
  • 2021-01-19 16:48

    Edit:

    Change your Manifest:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.myfirstapp"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="11" />
    
        <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/title_activity_display_message">
               <intent-filter >
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter> 
            </activity>
    
    
    <activity
                android:name=".DisplayMessageActivity" />
        </application>
    
    </manifest>
    

    DisplayMessgaeActivity:

    public class DisplayMessageActivity extends Activity {
    
            @Override
             protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
    
    
            // 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);
    
            // Set the text view as the activity layout
            setContentView(textView);
    
    
    
        }
           }
    

    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_display_message_1);
    }
    
        /** Called when the user clicks the Send button */
        public void sendMessage() {
            // 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);
        }
    
    }
    

    your layout should have a life cycle method onCreate It should have a onCreate method and you need to set setContentview(R.layout.yourlayout) for the MainActivity

    0 讨论(0)
  • 2021-01-19 16:53
    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);
        this.startActivity(intent);
    }
    
    0 讨论(0)
提交回复
热议问题