想要从xml访问片段中的字符串

时间:2016-02-05 10:04:41

标签: android

我想从xml访问字符串名称到这个字符串数组但它给我null对象错误,我该怎么办?我怎么能访问这个字符串?我使用了上下文,但它不起作用,有人可以告诉我该怎么办?

public class ViewProfileAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = 4;
    Context context;
    String a=context.getApplicationContext().getResources().getString(R.string.viewkundli_tab_kundli);
    String b=context.getApplicationContext().getResources().getString(R.string.viewkundli_tab_remedies);
    String c=context.getApplicationContext().getResources().getString(R.string.viewkundli_tab_predictions);
    String d=context.getApplicationContext().getResources().getString(R.string.viewkundli_tab_panchang);

    private String tabTitles[] = new String[] {a,b,c,d };

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

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return ViewKundli_1.newInstance(position);

            case 1:
                return ViewKundli_2.newInstance(position);

            case 2:
                return ViewKundli_3.newInstance(position);

            case 3:
                return ViewKundli_4.newInstance(position);

        }
        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];

    }

}

7 个答案:

答案 0 :(得分:5)

使用getActivity()代替context.getApplicationContext().片段。 对于适配器,您应该使用构造函数初始化上下文变量,然后单独context将起作用

答案 1 :(得分:2)

ViewProfileAdapter类中创建构造函数并在构造函数中传递上下文现在使用StringArray代替string

ViewProfileAdapter.java

private Context context;
private String[] tabTitleArray = null;

public ViewProfileAdapter(FragmentManager fm,
        Context context) {
    super(fm);
    tabTitleArray = context.getResources().getStringArray(
            R.array.tab_title);
    this.context= context;
}

@Override
public int getCount() {
    return tabTitleArray.length();
}

FragmentActivity

ViewProfileAdapter pageAdapter 
                           = new ViewProfileAdapter(getFragmentManager(), this);
 //this- Activity and getActivty() for Fragment

string.xml

<string-array name="tab_titles">
   <item>tab1</item>
   <item>tab2</item>
   <item>tab3</item>
    <item>tab4</item>
</string-array>

答案 2 :(得分:0)

getString(R.string.viewkundli_tab_kundli)直接用于片段

答案 3 :(得分:0)

您需要在班级中添加构造函数:

public ViewProfileAdapter(Context context) {
    this.context = context;
}

在你的活动中:

ViewProfileAdapter adapter = new ViewProfileAdapter(this);
//etc... 

然后你可以去使用:

context.getResources().getString(R.string.viewkundli_tab_kundli);
//etc...

答案 4 :(得分:0)

Context成员未初始化。 您需要将Context传递给Adapter,然后将其用作参考。 在这种情况下,您不能在字段声明中初始化字符串。

Context context;
String a;
String b;
String c;
String d;

private String[] tabTitles;

public ViewProfileAdapter(Context context, FragmentManager fm) {
    super(fm);
    this.context = context;
    a=context.getString(R.string.viewkundli_tab_kundli);
    b=context.getString(R.string.viewkundli_tab_remedies);
    c=context.getString(R.string.viewkundli_tab_predictions);
    d=context.getString(R.string.viewkundli_tab_panchang);
    tabTitles = new String[] {a,b,c,d };
}

// If you need String elsewhere

void method() {
    String s = context.getString(R.string.s);
}

甚至更好

private final String[] tabTitles;

public ViewProfileAdapter(Context context, FragmentManager fm) {
    super(fm);
    tabTitles = context.getResources().getStringArray(R.array.tab_titles);
}

的strings.xml

<string-array name="tab_titles">
    <item>a</item>
    <item>b</item>
    <item>c</item>
    <item>d</item>
</string-array>

答案 5 :(得分:0)

我已根据您的要求修改了我的答案。

public class ViewProfileAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = 4;
    Context context; 
    String a; 
    String b;  
    String c;
    String d;

    private String tabTitles[] = new String[] {a,b,c,d };

    public ViewProfileAdapter(FragmentManager fm, Context context) {
        super(fm);
    this.context = context;//this assignment is for future use of context object
    a=context.getResources().getString(R.string.viewkundli_tab_kundli);
        b=context.getResources().getString(R.string.viewkundli_tab_remedies);
        c=context.getResources().getString(R.string.viewkundli_tab_predictions);
        d=context.getResources().getString(R.string.viewkundli_tab_panchang);
    }

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return ViewKundli_1.newInstance(position);

            case 1:
                return ViewKundli_2.newInstance(position);

            case 2:
                return ViewKundli_3.newInstance(position);

            case 3:
                return ViewKundli_4.newInstance(position);

        }
        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];

    }

}

从您的活动

中调用上面的构造函数,如下所示
new ViewProfileAdapter(getSupportFragmentManager(), getActivity());//Support lib
new ViewProfileAdapter(getFragmentManager(), getActivity());//without support lib and Android API greater than 3.0

从Fragment

中调用上面的构造函数,如下所示
new ViewProfileAdapter(getChildFragmentManager(), getActivity());

答案 6 :(得分:0)

您可以在Application类中初始化类似的内容,

public class Application扩展Application {

public void connectToConference(String name){
        updateAudioRoute(true);
        Map<String, String> parameters = new HashMap<String, String>();

        nameConference = name;
        parameters.put("To","conference:"+name);
        if(device != null){
            connection = device.connect(parameters, null /* ConnectionListener */);
        }

        if (connection == null) {
            Log.w(TAG, "Failed to create new connection");
        }
    }

}

然后在您的适配器中,您可以将此上下文用作

Resources resources = Application.getContext()。getResources();

a = resources.getString(R.string.viewkundli_tab_kundli);