Illegalstateexception When Click On Button In Android Hello World Program
Solution 1:
Edit:
Change your Manifest:
<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.example.myfirstapp"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="11" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name=".MainActivity"android:label="@string/title_activity_display_message"><intent-filter ><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name=".DisplayMessageActivity" /></application></manifest>
DisplayMessgaeActivity:
publicclassDisplayMessageActivityextendsActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intentIntentintent= getIntent();
Stringmessage= intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text viewTextViewtextView=newTextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
}
MainActivity:
publicclassMainActivityextendsActivity {
publicfinalstaticStringEXTRA_MESSAGE="com.example.myfirstapp.MESSAGE";
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message_1);
}
/** Called when the user clicks the Send button */publicvoidsendMessage() {
// Do something in response to buttonIntentintent=newIntent(this, DisplayMessageActivity.class);
EditTexteditText= (EditText) findViewById(R.id.edit_message);
Stringmessage= 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
Solution 2:
You are using two time setContentView()
. You can use only one setContentView
.
Solution 3:
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 onview class android.widget.Button</p><p>03-1518: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
<LinearLayoutxmlns: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" ><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/button_send"android:onClick="sendMessage" /></LinearLayout>
activity_display_message.xml
<LinearLayoutxmlns: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" ><EditTextandroid:id="@+id/edit_message"android:layout_weight="1"android:layout_width="0dp"android:layout_height="wrap_content"android:hint="@string/edit_message" /></LinearLayout>
MainActivity
publicclassMainActivityextendsActivity {
publicfinalstaticStringEXTRA_MESSAGE="com.example.myfirstapp.MESSAGE";
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the Send button */publicvoidsendMessage(View view) {
// Do something in response to buttonIntentintent=newIntent(this, DisplayMessageActivity.class);
EditTexteditText= (EditText) findViewById(R.id.edit_message);
Stringmessage= editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
DisplayMessageActivity.java
publicclassDisplayMessageActivityextendsActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message_1);
// Get the message from the intentIntentintent= getIntent();
Stringmessage= intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text viewTextViewtextView=newTextView(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"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.example.myfirstapp"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="11" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name=".MainActivity"android:label="@string/title_activity_display_message"><intent-filter ><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name=".DisplayMessageActivity" /></application></manifest>
Solution 4:
//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"
Solution 5:
publicvoidsendMessage(View view) {
// Do something in response to buttonIntentintent=newIntent(this, DisplayMessageActivity.class);
EditTexteditText= (EditText) findViewById(R.id.edit_message);
Stringmessage= editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
this.startActivity(intent);
}
Post a Comment for "Illegalstateexception When Click On Button In Android Hello World Program"