2016-02-05 59 views
-3

我想从xml访问字符串名称到这个字符串数组中,但它给了我空对象错误,我该怎么办?我怎样才能访问这个字符串?我已经使用上下文,但它不工作,有人可以告诉我,我该怎么做?想要访问XML中的一个片段中的字符串

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]; 

    } 

} 
+0

你没有初始化你的上下文变量。只要使用getApplicationContext()。getResources().... – X3Btel

+0

@bipin你可以接受帮助答案咆哮任何其他人从你的贡献获得帮助:) – MilapTank

回答

5

使用getActivity()而不是context.getApplicationContext().的片段。 并为您的适配器,您应该使用一个构造函数来初始化上下文变量,然后独自context将工作

+0

它是一个片段不活动,所以我怎么能使用getActivity()?它给了我错误 – bipin

0

getString(R.string.viewkundli_tab_kundli)直接作用在片段的情况下

+0

String a = getString(R.string.view_kundli_menu);不工作 – bipin

+0

你会得到一些错误..你能分享你的错误日志 – Nitesh

1

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> 
+0

其工作现在谢谢大家! – bipin

0

你需要一个构造函数添加到类:

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

而在你的活动:

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

那么你是好去使用:

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

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

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> 
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 

调用上述建筑工,如下从片段

new ViewProfileAdapter(getChildFragmentManager(), getActivity()); 
+0

我不确定'ViewProfileAdapter'实际上是一个Fragment的内部类。无论如何,这仍然不是最好的方法。正如我的回答中所述,最好在构造函数中传递Context。 –

+0

无论您是否传递Context,它仍然是Activity的一部分。 – lavan

0

您可以在应用程序类是这样的初始化上下文,

public class Application extends Application {

private static Context context; 

public static synchronized Context getContext() { 
    return context; 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    context = this; 

    ); 
} 

}

,然后在你的适配器,您可以使用该上下文关系

资源资源= Application.getContext()getResources()。

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

相关问题