为什么我得到getString错误?以及如何解决?

The*_*mad 1 android getstring fragmentpageradapter

我正在测试FragmentPagerAdapter,之前我在一个类中完成了所有这些操作.一切正常,但是一旦我分离了SectionsPagerAdapter类,getString就不能在getPageTitle函数下工作了.

我知道getPageTitle是PagerAdapter类的一部分,但我想知道在这个类中包含该函数的最佳方法是什么.我需要延长课程吗?

SectionsPageAdapter类

import java.util.Locale;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

// A FragmentPagerAdapter that returns a fragment corresponding to one of the sections/tabs/pages. 
public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override  
public Fragment getItem(int position) {  
    // getItem is called to instantiate the fragment for the given page.  
    // Return a DummySectionFragment (defined as a static inner class  
    // below) with the page number as its lone argument.  
    Fragment fragment = new DummySectionFragment();  
    Bundle args = new Bundle();  
    args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);  
    fragment.setArguments(args);  
    return fragment;
}

@Override
public CharSequence getPageTitle(int position) {  
    Locale l = Locale.getDefault();  
    switch (position) {  
    case 0:  
        return getString(R.string.myFriendsTab).toUpperCase(l);  
    case 1:
        return getString(R.string.myDealsTab).toUpperCase(l);  
    case 2:
        return getString(R.string.featuredDealsTab).toUpperCase(l);
    case 3:
        return getString(R.string.browseCategoriesTab).toUpperCase(l);  
    case 4:
        return getString(R.string.localDealsTab).toUpperCase(l);  
    }  
    return null;  
}

@Override  
public int getCount() {  
     // Show 5 total pages.  
     return 5;  
}
}
Run Code Online (Sandbox Code Playgroud)

MainActivity类

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainActivity extends FragmentActivity {
    // Fragment PagerAdapter keeps every loaded fragment in memory. 
    // If too memory intensive, switch to FragmentStatePagerAdapter.
    SectionsPagerAdapter mSectionsPagerAdapter;

    ViewPager mViewPager; // ViewPager that will host section contents.

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

        // Creates the adapter that will return a fragment for each of the primary sections.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), null);

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

pan*_*ini 6

getString(int)只适用于有机会获得一类Context- Fragments,Activities等等.

鉴于这是一个Adapter类,它将无法直接访问a Context,因此您应该使用构造函数传递一个.

private Context mContext = null;

public SectionsPagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    mContext = context;
}
Run Code Online (Sandbox Code Playgroud)

然后使用成员字段进行访问 getString(int)

return mContext.getString(R.string.myFriendsTab).toUpperCase(1);
Run Code Online (Sandbox Code Playgroud)