Reputation: 27
I have a button on Main's android xml file which once clicked will display another view/activity. My problem is the error message displays that the application must end unexpectedly.
Here is the button
<Button android:id="@+id/showmeurcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="OnShowMeUrCode"
>
The method OnShowMeUrCode is defined in the MainActivity class as
private void OnShowMeUrCode(View btn)
{
Intent urCode=new Intent(this,CodePage.class);
startActivity(urCode);
}
CodePage
is generated from a class of the same name
public class CodePage extends Activity
{
....
}
That is all I have done in the hope that I could accomplish the simple task with Intent to display another view but I run in an unexpected error and my program fails short.
Upvotes: 0
Views: 61
Reputation: 3329
You need to change your OnShowMeUrCode() function to be public, not private. Since it's part of the Activity class, your Button won't have access to it if it's private.
Plus it's in the docs: http://developer.android.com/reference/android/widget/Button.html
Upvotes: 2
Reputation: 7089
Based on your code without the error log output, I guess you didn't pass a correct Context to the method.
This is your code:
private void OnShowMeUrCode(View btn)
{
Intent urCode=new Intent(this,CodePage.class);
startActivity(urCode);
}
Try replace the relevant line with:
Intent urCode=new Intent(MainActivity.this,CodePage.class);
Say, I have two Activities, A and B. If I'm calling B from A, I should write something like:
Intent i = new Intent(A.this, B.class);
startActivity(i);
Besides, you need to register your Activity in AndroidManifest.xml each time you create a new activity. In your case, please check if there are 2 activities in your AndroidManifest.xml
Upvotes: 0