我正在使用我的应用程序Neokree Materialtabs。我无法翻译标题。我累了这么多字符串代码。 Android Studio构建到apk并没有给我错误,但是当我启动应用程序时它会自行停止。
代码标签标题:
private String[] tabs = {"Home","Category","Favourite"};
和
pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// when user do a swipe the selected tab change
tabHost.setSelectedNavigationItem(position);
}
});
for(String tab_name:tabs)
{
tabHost.addTab(
tabHost.newTab()
.setText(tab_name)
.setTabListener(MainActivity.this)
);
}
}
MainActivity.java:
public class MainActivity extends ActionBarActivity implements MaterialTabListener {
MaterialTabHost tabHost;
ViewPager pager;
ViewPagerAdapter adapter;
private String[] tabs = {"Home","Category","Favourite"};
Toolbar toolbar;
String strMessage;
private AdView mAdView;
private InterstitialAd mInterstitial;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OneSignal.startInit(this).init();
setContentView(R.layout.activity_main);
toolbar = (Toolbar) this.findViewById(R.id.toolbar);
toolbar.setTitle(getString(R.string.app_name));
this.setSupportActionBar(toolbar);
mAdView = (AdView) findViewById(R.id.adView);
mAdView.loadAd(new AdRequest.Builder().build());
mInterstitial = new InterstitialAd(this);
mInterstitial.setAdUnitId(getResources().getString(R.string.admob_intertestial_id));
mInterstitial.loadAd(new AdRequest.Builder().build());
mInterstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// TODO Auto-generated method stub
super.onAdLoaded();
if (mInterstitial.isLoaded()) {
mInterstitial.show();
}
}
});
tabHost = (MaterialTabHost) this.findViewById(R.id.tabHost);
pager = (ViewPager) this.findViewById(R.id.pager );
adapter = new ViewPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// when user do a swipe the selected tab change
tabHost.setSelectedNavigationItem(position);
}
});
for(String tab_name:tabs)
{
tabHost.addTab(
tabHost.newTab()
.setText(tab_name)
.setTabListener(MainActivity.this)
);
}
}
@Override
public void onTabSelected(MaterialTab tab) {
// TODO Auto-generated method stub
pager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(MaterialTab tab) {
// TODO Auto-generated method stub
}
@Override
public void onTabUnselected(MaterialTab tab) {
// TODO Auto-generated method stub
}
private class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int num) {
switch (num) {
case 0:
return new HomeFragment();
case 1:
return new CategoryFragment();
case 2:
return new FavouriteFragment();
}
return null;
}
@Override
public int getCount() {
return 3;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.home, menu);
final MenuItem searchMenuItem = menu.findItem(R.id.search);
final SearchView searchView = (SearchView)MenuItemCompat.getActionView(searchMenuItem);
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(!hasFocus) {
MenuItemCompat.collapseActionView(searchMenuItem);
searchView.setQuery("", false);
}
}
});
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String arg0) {
// TODO Auto-generated method stub
Constant.SHOP_SEARCH=arg0;
Intent intsearch=new Intent(MainActivity.this,Search.class);
startActivity(intsearch);
return false;
}
@Override
public boolean onQueryTextChange(String arg0) {
// TODO Auto-generated method stub
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
switch (menuItem.getItemId())
{
case R.id.about:
Intent intab=new Intent(MainActivity.this,AboutActivity.class);
startActivity(intab);
break;
default:
return super.onOptionsItemSelected(menuItem);
}
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Toast.makeText(appContext, "BAck", Toast.LENGTH_LONG).show();
AlertDialog.Builder alert = new AlertDialog.Builder(
MainActivity.this);
alert.setTitle(getString(R.string.app_name));
alert.setIcon(R.drawable.ic_launcher);
alert.setMessage(R.string.quit);
alert.setPositiveButton(R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
finish();
}
});
alert.setNegativeButton(R.string.rate,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
final String appName = getPackageName();//your application package name i.e play store application url
try {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id="
+ appName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id="
+ appName)));
}
}
});
alert.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
我试过这段代码:
getResources().getString(R.string.menu_home)
而且:
getString(R.string.menu_home)
我想这样做,但没有工作:
private String[] tabs = {getString(R.string.menu_home),getString(R.string.menu_cat),getString(R.string.menu_fav)};
我也是这样尝试的:
private String[] tabs = {getResources().getString(R.string.menu_home),getResources().getString(R.string.menu_cat),getResources().getString(R.string.menu_fav)};
答案 0 :(得分:0)
尝试getResources().getString(R.string.menu_home);