使用ActionBar的NoSuchMethodError异常

时间:2012-08-18 22:33:24

标签: android android-actionbar

我刚开始使用ActionBar,我得到了NoSuchMethodError Exception。 这是什么原因?

这是例外

java.lang.NoSuchMethodError: com.example.actionbar2.MainActivity.getActionBar

代码:

package com.example.actionbar2;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;



public class MainActivity extends Activity {
public static Context appContext;

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

       //ActionBar gets initiated
        ActionBar actionbar = getActionBar();
      //Tell the ActionBar we want to use Tabs.
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
      //initiating both tabs and set text to it.
        ActionBar.Tab PlayerTab = actionbar.newTab().setText("Fragment A");
        ActionBar.Tab StationsTab = actionbar.newTab().setText("Fragment B");

     //create the two fragments we want to use for display content
        Fragment PlayerFragment = new AFragment();
        Fragment StationsFragment = new BFragment();

    //set the Tab listener. Now we can listen for clicks.
        PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));
        StationsTab.setTabListener(new MyTabsListener(StationsFragment));

   //add the two tabs to the actionbar
        actionbar.addTab(PlayerTab);
        actionbar.addTab(StationsTab);
    }

    public class AFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.afragment, container, false);
        }

    }

    public class BFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.afragment, container, false);
        }

    }

    class MyTabsListener implements ActionBar.TabListener {
        public Fragment fragment;

        public MyTabsListener(Fragment fragment) {
        this.fragment = fragment;
        }


        @Override
        public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
            Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();


        }

        @Override
        public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
            ft.replace(R.id.fragment_container, fragment);

        }

        @Override
        public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
            ft.remove(fragment);

        }

        }
}

1 个答案:

答案 0 :(得分:2)

这发生在Android 2.2及更低版本上,尝试在4.0及更高版本上运行。 您也可能必须使用

    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayHomeAsUpEnabled(true);