Scott Lounsbury
Scott Lounsbury

Reputation: 120

Buttons not working properly

In my main.xml I have 5 buttons all of which lead to different pages but when I launch the app the buttons were their but when clicked they did not work. I think the problem may be in the AndroidManifest.xml

Menu.Java

package com.invoice;

import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;

public class menu extends Activity implements OnClickListener {
    /** called the activity is first created. */
    Button button1, button2, button3, button4, button5;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button1 = (Button)findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);
        button3 = (Button)findViewById(R.id.button3);
        button4 = (Button)findViewById(R.id.button4);
        button5 = (Button)findViewById(R.id.button5);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
    }   
    public void onClick(View v) {
        //TODO Auto-generate method stub
        //figure out which button was pressed
        switch (v.getId()) {
        case R.id.button1:
            //do button1 action
            break;
        case R.id.button2:
            //do button2 action
            break;
        case R.id.button3:
            //do button3 action
            break;
        case R.id.button4:
            //do button4 action
            break;
        case R.id.button5:
            //do button5 action
            break;
        }


        }
    }

Android Manifest (only the button activity)

android:name=".menu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.invoice.MENU" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

        </activity>
    <activity android:name = ".Help" />
    <activity android:name = ".Job" />
    <activity android:name=".Fourm" />
    <activity android:name=".Receipt"/>
    <activity android:name=".Reportissue"/>

Job.java (same code is in every button java file)

package com.invoice;


import android.app.Activity;
import android.os.Bundle;

public class Job extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.job);
    }

}  

Main.xml

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="87dp"
        android:text="@string/report_issue"
        android:width="300dp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_marginBottom="29dp"
        android:text="@string/help"
        android:width="300dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button2"
        android:layout_alignLeft="@+id/button3"
        android:layout_marginBottom="36dp"
        android:text="@string/fourm"
        android:width="300dp" android:height="50dp"/>

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/button1"
        android:layout_marginTop="15dp"
        android:height="150dp"
        android:text="@string/reciept"
        android:width="145dp" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button5"
        android:layout_alignBottom="@+id/button5"
        android:layout_toLeftOf="@+id/button5"
        android:height="150dp"
        android:text="@string/job"
        android:width="150dp" />

    <DigitalClock
        android:id="@+id/digitalClock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="38dp"
         />

</RelativeLayout>

Upvotes: 0

Views: 942

Answers (3)

user2023808
user2023808

Reputation: 1

This what happens when you root your android. Rooting is a great thing to do to your android phone because it will open up the features on your phone. However, it's very risky so I suggest if you are going to root, Root at your own risk!

Upvotes: 0

GrIsHu
GrIsHu

Reputation: 23638

Your xml file name you have defined here is Menu.xml and in your java file you are using main layout file. Why so ?

You should use the layout file Menu.xml in your java file. Just change the layout file name and then try to implement the onClick. It should work.

public void onClick(View v) {
    //TODO Auto-generate method stub
    //figure out which button was pressed
    switch (v.getId()) {
    case R.id.button1:
        //do button1 action
       System.out.println("Button1 Clicked.");
         break;
     }
   }

Upvotes: 1

Usman Kurd
Usman Kurd

Reputation: 7023

Your Buttons are in Menu.xml File where as you are loding R.Layout.main and performing Button functionality there 1st load the correct refrenced XMl in which buttons are defined then Run and if still have issues then share logcat trace here

Upvotes: 0

Related Questions