简体   繁体   English

如何在Android中为标签页添加onTabListener

[英]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. 任何帮助将不胜感激。

Java does not compare string like C# Java不比较C#这样的字符串

In Java it is 在Java中

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

Change your String comparisons to use equals and your 将您的字符串比较更改为使用equals和

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM