I’m new to android and I’ve just learned about activities and I decided to create a few.
and my MainActivity.java is,
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button a = (Button) findViewById(R.id.button1);
a.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, WidgetIntro.class));
Button b = (Button) findViewById(R.id.button2);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Alarm.class));
Button c = (Button) findViewById(R.id.button4);
c.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Sms.class));
Button d = (Button) findViewById(R.id.button6);
d.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Call.class));
Button e = (Button) findViewById(R.id.button3);
e.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Notes.class));
Button f = (Button) findViewById(R.id.button5);
f.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Cbdata.class));
}
});
}
});
}
});
}
});
}
});
Though this has no error and I’ve created some text to toast for every button, when I install my application on my phone I’m able to access buttons only in the order I’ve numbered them, that is for ‘Location Widget’ button my button id is button1 so except for this button I’m able to select any other buttons and once I’ve selected this button I can only select button 2 but not any other button, why does this happen?
My activity_main.xml code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="26dp"
android:text="About Us" />
<Button
android:id="@+id/button3"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_above="@+id/button7"
android:layout_alignParentRight="true"
android:layout_marginBottom="36dp"
android:text="Note Remainder" />
<Button
android:id="@+id/button2"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_alignParentLeft="true"
android:text="Alarm" />
<Button
android:id="@+id/button4"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_marginBottom="40dp"
android:text="SMS" />
<Button
android:id="@+id/button6"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button4"
android:layout_alignBottom="@+id/button4"
android:layout_alignParentRight="true"
android:text="Call" />
<Button
android:id="@+id/button1"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_above="@+id/button6"
android:layout_alignParentRight="true"
android:layout_marginBottom="37dp"
android:text="Location Widget" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Hello User, Welcome to Mobile location Info! You Choose from the following options :"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button5"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_alignParentLeft="true"
android:text="Save my locations" />
</RelativeLayout>
}
});
}
}
You have defined each buttons onClick listener inside the onClick listener of the previous button. Therefore, each button after the first one only has an assigned onClick listener when you click the previous button.
Instead, do this:
A tip. In general, when you see more than about 3 levels of closing braces, like this then you know there is a problem: