user2507301
user2507301

Reputation: 153

How to add an onTabListener for a tab in Android

I have an issue in my application. The issue is that I cannot switch between activities from one tab to another. Here is my code:

     public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            String n = tab.getText().toString();
            Toast.makeText(getApplicationContext(), "You have selected: " + n, Toast.LENGTH_LONG).show();
            if (n =="Converter") {
                startActivity(new Intent ("com.example.currencyconverter.MainActivity"));
                }
            if (n =="Currencies") {
                startActivity(new Intent ("com.example.currencyconverter.FirstActivity"));  
            if (n=="News") {
                startActivity(new Intent ("com.example.currencyconverter.FirstActivity"));  
                  }
                }

        }

My app is crashing. What is the issue? Any help would be greatly appreciated.

Upvotes: 0

Views: 105

Answers (2)

Plato
Plato

Reputation: 2348

Change your String comparisons to use equals and your

startActivity(new Intent ("com.example.currencyconverter.MainActivity"));

to

startActivity(new Intent (this, MainActivity.class));

Upvotes: 1

MDMalik
MDMalik

Reputation: 4001

Java does not compare string like C#

In Java it is

if(n.equals("Converter"))
{   /// do something }

Upvotes: 2

Related Questions