0

I've been racking my brain for the past two days trying to get a button to work. Basically I want my button to call 'add a password function'.

If you like to view my entire code, its at github.com/servingbaby/UPM-Epassafe .

Here is my main.xml (The button shows up succesfully however when pushing it nothing happens) main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">

    <TextView
        android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:gravity="center"
        android:text="@string/no_accounts" />

        <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_gravity="center"
        android:text="@string/add_account" android:id="@+id/add" android:minWidth="125dip"></Button>

        <ListView android:id="@android:id/list"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"/>

</LinearLayout>

Here is my AndroidManifest slimmed down to the parts I believe are where I am going wrong.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.epassafe.upm"
          android:versionCode="3"
          android:versionName="1.2">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="16"/>
    <application android:icon="@drawable/upm" 
       ....>
        <activity android:name=".AppEntryActivity">
        </activity>

        <activity android:name=".FullAccountList">
            <meta-data android:name="android.app.default_searchable"
                       android:value=".SearchResults" />
        </activity>

        <activity android:name=".AddEditAccount">
        </activity>
    </application>
</manifest>

Here is my MainActivity.Java, where I believe I should be calling the correct code.

public abstract class MainActivity extends Activity implements OnClickListener{`

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Button countButton = (Button) findViewById(R.id.add);

        countButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this,AddEditAccount.class);
                MainActivity.this.startActivity(intent);
            }
        });

    }
}

But pushing the button nothing happens like I expect. No errors or force close, so Im really at a loss at the moment. Thankyou!

**Edit Thank you all how attempt to help me. I have tried many of the suggested solutions and still haven't figured it out just yet. I have uploaded my files to github with some of the attempted edits I have made as commented. Basically I am just trying to figure out how to add a button and get it to run the necessary code to add an account. (The original code works well, but I would like to improve upon it. Sorry my competence level is still low, but trying to learn my best :) I would really appreciate if anyone could help me with this! Thank you^^

**Edit 2 Again Thankyou all for the advice and suggestions. I figured out a simple solution. I ended up using a Menu Wrapper to create a button click event, and also it turned out I was editing the wrong Java document, when I was suppose to be trying to add the correct code to another one that seem to almost be doing the same thing. Learn something new everyday.

  • try, Intent intent = new Intent(getApplicationContext(),AddEditAccount.class); – krammer May 27 '13 at 11:23
  • May I see AddEditAccount ? – Blackbelt May 27 '13 at 11:36
  • in the android manifest i was not able to find the declartion of the activity MainActivity this may be the reason.please check it out. – deepak825 May 27 '13 at 11:37
  • @blackbelt https://github.com/servingbaby/UPM-Epassafe/blob/master/src/com/epassafe/upm/AddEditAccount.java – Archie.bpgc May 27 '13 at 11:39
  • Have you tried to put a `Log.d()` within your `onClick` to make sure that's even getting fired? – themanatuf May 27 '13 at 11:40
  • Thank you all for helping me with this issue. I'm still learning about coding. I'm attempting now some of the suggested solutions. Still unable to get it working, though not quiting. As blackbelt suggested here is AddEditAccount: https://github.com/servingbaby/UPM-Epassafe/blob/master/src/com/epassafe/upm/AddEditAccount.java – Steven Bennett May 27 '13 at 13:35

4 Answers4

0

First of all you dont need to implement OnClickListener to set a a listner in your button. But maybe you have it for another reason, if not, remove it.

You are declaring wrong the names of the id of your xml i think, take a look at this: Difference between "@id/" and "@+id/" in Android May be because of the @android:id instead of @+id.

Community
  • 1
  • 1
Roi
  • 1,434
  • 2
  • 11
  • 13
  • Thank you for your observation. I have been following examples in other parts of the code to come up with this. I am unsure of the correct way of implementation. – Steven Bennett May 27 '13 at 15:03
0

use this way call Intent

Intent intent = new Intent(MainActivity.this, AddEditAccount.class);
                startActivity(intent);
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
  • Thankyou for your suggestion. I attempted this code, however no change has occurred. I am thinking this part is correct, however somewhere else I am missing something. – Steven Bennett May 27 '13 at 14:59
0

look at your list view

<ListView android:id="@android:id/list"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"/>

you have given width and height as fill parent so your ListView is on your button so on the screen when you click on the button you are not actually clicking on the button. when you click a button the button color changes for a sec that is not happening in your case see it for your self. so remove the listview or set the height below the button and then check your button click will work

prvn
  • 916
  • 5
  • 5
  • Thank you for your suggestion on changing these variables. I have attempted to change them so that the button is the only thing on the xml. However still pushing the button provides no execution. So I believe somewhere else I am missing something. Really would love to learn where I am making my mistake. – Steven Bennett May 27 '13 at 15:01
0

I don't understand why you're implementing OnClickListener to your activity. Remove the implements OnClickListener from your activity. It should work fine.

But since you're doing this for don't-know-what reason, you should make your button clickable like this.

countButton.setOnClickListener(this);

public void onClick(View view)
{
Intent intent = new Intent(MainActivity.this,AddEditAccount.class);
                MainActivity.this.startActivity(intent);
}
Avi Singh
  • 171
  • 2
  • 7
  • Thankyou for your question. Basically I was following a tutorial on how to call an activity. If this is not the best way, I would really love to learn the correct implementation. Thankyou. – Steven Bennett May 27 '13 at 14:58
  • 1
    @StevenBennett Both are correct ways but what you should use depends on the requirement. You should follow this answer I found on Stackoverflow. It really explains this thing in the possible easiest way. Here you go. http://stackoverflow.com/a/8866429/2414595 – Avi Singh May 27 '13 at 16:51