1

As far as I can tell, everything needed for a button to respond is in place. Im baffled so would appreciate a pair of extra eyes on this.

Simple homepage activity, login or create a profile buttons displayed. Click the login button and the next activity should display. Application runs on device but buttons do not respond.

Code

public class HomeActivity extends Activity implements OnClickListener { Button login; Button createProfile; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); login = (Button) findViewById(R.id.loginButton); login.setOnClickListener(this); createProfile = (Button) findViewById(R.id.createProfileButton); createProfile.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.home, menu); return true; } @Override public void onClick(View view) { Intent intent; switch(view.getId()){ case R.id.loginButton: /** Start a new Activity LoginActivity.java */ intent = new Intent(this, LoginActivity.class); this.startActivity(intent); break; case R.id.createProfileButton: /** Start a new Activity About.java */ //intent = new Intent(this, AboutActivity.class); //this.startActivity(intent); break; } } } 

HomeScreen layout

<RelativeLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".HomeActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/loginButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="160dp" android:layout_marginRight="51dp" android:text="Login" /> <Button android:id="@+id/createProfileButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/loginButton" android:layout_alignBottom="@+id/loginButton" android:layout_toLeftOf="@+id/loginButton" android:text="createProfile" /> 

Login Layout

<RelativeLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".HomeActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is the login page!" /> 

Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.nutrtionintuition" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.nutrtionintuition.HomeActivity" 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=".LoginActivity" android:label="@string/app_name" > </activity> </application> 

LoginACtivity

public class LoginActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.home, menu); return true; } } 
16
  • Is this whole content of onClick() method? Because currently your onClick() method do nothing. Only defines intent and switch. Commented Nov 30, 2013 at 18:57
  • 1
    make sure the clickable property is not set to false in the xml. Commented Nov 30, 2013 at 18:57
  • How do you know it's not called? BTW, learn how to use the debugger. These are simple bugs to work out with the debugger. Trying to code without a debugger is like building a car with only one spanner. Not using the debugger is making things harder for you. Debuggers save an enormous amount of time and make everything easier. The debugger will find gold in your garden and unicorns in the car park. Writing Android Java code without using the debugger will cause your teeth to turn blue and your knees to change places with your elbows. Debugging aids digestion and keeps your breath sweet. :) Commented Nov 30, 2013 at 19:01
  • 1
    Paste your xml code for this activity and the second activity class and xml and manifest so that we can get a better picture. Maybe something is wrong in these files. The code you pasted is ok. Paste the rest Commented Nov 30, 2013 at 19:16
  • 2
    post your code for your LoginActivity class Commented Nov 30, 2013 at 19:53

2 Answers 2

2

In both classes (HomeActivity and LoginActivity), you set the content view like this:

setContentView(R.layout.activity_home); 

Your code is working, but the new activity is loading the same layout.


Change
setContentView(R.layout.activity_home);
to
setContentView(R.layout.activity_login);

(or whatever your login layout is named) in your LoginActivity class.

Sign up to request clarification or add additional context in comments.

Comments

0

Your code is OK and it must work. It seems that your activites are not included in your AndroidManifest.xml.

If so, maybe you are actually launching your another activity, but you may have missed to put the right layout id in the setContentView function.

1 Comment

in that case it would have thrown a ClassNotFoundException

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.