gpt4 book ai didi

android - 带有 ActionBar 的 NoSuchMethodError 异常

转载 作者:行者123 更新时间:2023-11-29 22:00:38 25 4
gpt4 key购买 nike

我刚从 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);

}

}
}

最佳答案

这发生在 android 2.2 及更低版本上,请尝试在 4.0 及更高版本上运行它。你也可能需要使用

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

关于android - 带有 ActionBar 的 NoSuchMethodError 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12022725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com