I have an Android program using the IntelliJ 12 Community Edition IDE. I need to navigate between the different layouts (.xml) I made with the use of buttons. But, whenever I run it using the emulator, it only opens the main.xml screen, whenever I click the buttons, it says that: “Unfortunately, ITax has stopped working.”
This is my code for the MyActivity.java class:
package com.example.ITax;
import android.app.Activity;
import android.content.Intent;
android.os.Bundle;
android.view.View;
import android.widget.Button;
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.btn_info);
Button button2 = (Button)findViewById(R.id.btn_tutorial);
Button button3 = (Button)findViewById(R.id.btn_calc);
Button button4 = (Button)findViewById(R.id.btn_back_from_calcu);
Button button5 = (Button)findViewById(R.id.btn_next_from_calcu);
Button button6 = (Button)findViewById(R.id.btn_back_from_monthlyorannual);
Button button7 = (Button)findViewById(R.id.btn_next_from_monthlyorannual);
Button button8 = (Button)findViewById(R.id.btn_back_from_civilstatus);
Button button9 = (Button)findViewById(R.id.btn_next_from_civilstatus);
Button button10 = (Button)findViewById(R.id.btn_back_from_inputamount_monthly);
Button button11 = (Button)findViewById(R.id.btn_compute_from_monthly);
Button button12 = (Button)findViewById(R.id.btn_back_from_outputamount_monthly);
Button button13 = (Button)findViewById(R.id.btn_home_from_outputamount_monthly);
}
public void Open_BasicInfo()
{
Intent i = new Intent(this, OpenBasicInfo.class);
startActivity(i);
}
public void Open_Tutorial()
{
Intent i = new Intent(this, OpenTutorial.class);
startActivity(i);
}
public void Open_Calculator()
{
Intent i = new Intent(this, OpenCalculator.class);
startActivity(i);
}
public void Open_MonthlyOrAnnually()
{
Intent i = new Intent(this, OpenMonthlyOrAnnually.class);
startActivity(i);
}
public void Open_CivilStatus()
{
Intent i = new Intent(this, OpenCivilStatus.class);
startActivity(i);
}
public void Open_InputAmountsFromMonthly()
{
Intent i = new Intent(this, OpenInputAmountsFromMonthly.class);
startActivity(i);
}
public void Open_OutputAmountsFromMonthly()
{
Intent i = new Intent(this, OutputAmountsFromMonthly.class);
startActivity(i);
}
public void Open_Main()
{
Intent i = new Intent(this, MyActivity.class);
startActivity(i);
}
}
I already created a class for each layout and declared in the Android manifest like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ITax"
android:versionCode="1"
android:versionName="ITax 1.0">
<uses-sdk android:minSdkVersion="1"/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name=".MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".OpenCalculator">
</activity>
<activity android:name=".OpenCivilStatus">
</activity>
<activity android:name=".OpenInputAmountsFromMonthly">
</activity>
<activity android:name=".OutputAmountsFromMonthly">
</activity>
<activity android:name=".OpenTutorial">
</activity>
<activity android:name=".OpenBasicInfo">
</activity>
<activity android:name=".OpenMonthlyOrAnnually">
</activity>
<activity android:name=".OpenMain">
</activity>
</application>
</manifest>
I already put the code android:onClick = “name of method” in all of the buttons.
Help! what seems to be the wrong I did here? 🙁
My application in the emulator stops working whenever I already clicked the buttons. >.<
In order to comply with the xml android:onclick=”MethodName”. Your activity must implement method that looks like this…
All of your onclick events are missing passing the View parameter.
Personally I don’t like using the xml, because if you make a spelling mistake in your code. Your activity will crash because it can’t inflate the xml. This imo leaves you vulnerable to silly mistakes. Here a link in more details about all the ways to do a button onclick event In the end it’s up to you.