0

I am very new to Andriod Programming and this is probably very basic question.

In my Application, the first page contains just button (for simplicity) login button.

After the user clicks login button it displays the toast and then I need to navigate to new class(page B) where I want to connect to a specific health sensor. Problem 1. I tried implementing the just the basic part with onClickListener for button and then when clicked, go to next page, where enable Bluetooth,etc. I could not get to next page

MainActivity.java :

public class MainActivity extends AppCompatActivity {

Button button;
PollingTest pd;
@Override
protected void  onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getBaseContext(),"Logged In",Toast.LENGTH_SHORT).show();
            pd = new PollingTest();
            pd.call();
        }
    });
}

}

Second Page (Where wanted to Control BT). Never got to this page while testing on the tablet: - For now just included if I could get a Toast atleast from this page: -

public class PollingTest extends Activity {

BluetoothAdapter btAdapter;
Button btn2;
protected void call() {
    //super.onCreate(savedInstanceState);
    //setContentView(R.layout.pairinglistactivity);
    btn2 = (Button)findViewById(R.id.pollB);
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
        }
    });
}

}

Here app crashes after clicking on Login button in first page.

I have actually got some different errors with different code, I was not able to make proper Toast or turn BT on in the second page as it was trying them in static method.( very Confusing:( )

Please help me. I know this v v basic Q..

EDIT:

Sorry, this Q is already answered here: - Moving from one activity to another Activity in Android

Community
  • 1
  • 1
Sagar Chilukuri
  • 1,430
  • 2
  • 17
  • 29

1 Answers1

0

You don't start an activity by instantiating it like a normal Java class. So this is wrong

pd = new PollingTest();
pd.call();

you should be using an Intent

and follow the Activity Lifecycle

so you would want something like

Intent i = new Intent(MainActivity.this, PollingTest.class);
startActivity(i);

then override onCreate() in PollingTest.java and put what is in call() in there or call that method from onCreate().

Also, a Toast should use Activity Context

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Thanks Sir, but should I add Intent in the onClickListener of the login button or on the onActivityResult method? – Sagar Chilukuri Sep 30 '15 at 14:34
  • In the `onClick()` if you want the button to start the activity. Also, there's a lot in your post so I missed that you want to return to this Activity. [See how to pass data back](http://stackoverflow.com/questions/20558689/back-to-previous-activity-with-intent/20558774#20558774) – codeMagic Sep 30 '15 at 14:36