Buttons wont work in 2nd class

  • Replies:3
  • OpenNot stickiedUnanswered
  • Forum posts: 2

Nov 4, 2014 7:30:16 PM via Website

Hi Guys,

I was wondering if you could help me out with the below issue basically I have 2 classes which the 2nd one doesn't show the toast captions yet the first one does am I doing something wrong here below is my code. any help would be greatly appreciated

1st class
package freelanceitceltd.ftpwithoutme_beta;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class FTPWELCOME extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    manualentry();
}

private void manualentry() {

    //create a reference to the button
    Button manualscene = (Button) findViewById(R.id.connectftp);
    manualscene = (Button) findViewById(R.id.connectftp);

    //set the click listener to run the code.
    manualscene.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(
                    FTPWELCOME.this,
                    "Please fill out all of the below fields in order to connect or save your server parameters", Toast.LENGTH_LONG)
                    .show();
            //navigates to the next step
            setContentView(R.layout.activity_manualconfig);

        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.ftpwelcome, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

2nd class
package freelanceitceltd.ftpwithoutme_beta;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class manualconfig extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_manualconfig);
    savesetup();
    connect();
}

public void savesetup() {

    //create a reference to the button
    Button savedata = (Button) findViewById(R.id.saveconfig);
    savedata = (Button) findViewById(R.id.saveconfig);

    //set the click listener to run the code.
    savedata.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(
                    manualconfig.this,
                    "Your connection setup details have been saved", Toast.LENGTH_LONG)
                    .show();



        }
    });

}

public void connect() {

    //create a reference to the button
    Button comms = (Button) findViewById(R.id.connectme);
    comms = (Button) findViewById(R.id.connectme);

    //set the click listener to run the code.
    comms.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(
                    manualconfig.this,
                    "Your connection details have been saved also", Toast.LENGTH_LONG)
                    .show();
            //navigates to the next step
            setContentView(R.layout.activity_manualconfig);

        }
    });
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.manualconfig, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

  • Forum posts: 118

Nov 7, 2014 10:50:27 AM via Website

In the first class, you're only changing the layout view. Because you're only changing views, you don't have the other activity's methods and variables. That's why you need to switch between activities instead using Intent.

Search for this in your first class:

//navigates to the next step
setContentView(R.layout.activity_manualconfig);

Replace it with the following code:

Intent intent = new Intent(this, manualconfig .class);
startActivity(intent);

Intent allows you to switch between activities.

For more about intent, search on Google. Can't post URLs to external websites yet :)

  • Forum posts: 2

Nov 8, 2014 1:50:03 PM via Website

Thanks kevin you've been a great help :)

regards

appyhappy

  • Forum posts: 118

Nov 10, 2014 1:51:49 PM via Website

appyhappy

Thanks kevin you've been a great help :)

regards

appyhappy

No problem :) If it solved your problem (I think it did), make sure you set this topic as 'answered'. Do that by hovering your mouse over the "More" in the top right corner of this topic's first post and click on "Set as Answered". (cool)

Just so that others with a similar problem know it can be solved with the answers in this topic ;)