为什么String XML会导致nullpointerexception?

时间:2013-06-17 12:42:37

标签: android xml string nullpointerexception

当用户点击viewpager中strings.xml中的某个项目时,我正尝试从ListView中提取字符串。但是,当我尝试从NullPointerException调用字符串时,我得到strings.xml。例如,我构造了一个字符串“VitaminA_definition”,它对应于strings.xml中的同名字符串。我已经检查过以确保我的字符串格式化了&拼写正确。

为什么我会收到NullPointerException

case 1:
    resId = (R.layout.reference_layout);    
    view = inflater.inflate(resId, null);

    vitamins_ListView = (IndexableListView)view.findViewById(R.id.herbList);                        
    vitaminsListresource = res.getStringArray(R.array.vitaminlist);

    vitaminsItems = new ArrayList<String>();
    Collections.addAll(vitaminsItems, vitaminsListresource);                
    Collections.sort(vitaminsItems);

    ArrayAdapter<String> vitamins_adapter = new CustomListAdapter(ReferenceActivity.this, R.layout.list_item_vitamins);
    for (int i=0; i<vitaminsItems.size();i++)
        vitamins_adapter.add(vitaminsItems.get(i));
    vitamins_ListView.setAdapter(vitamins_adapter);
    vitamins_ListView.setFastScrollEnabled(true);
    vitamins_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
            View toolbar = view.findViewById(R.id.vitamin_toolbar);                                            

            String vitamin_definition = vitamins_ListView.getItemAtPosition(position).toString() + "_definition";
            String new_vitamin_definition =  vitamin_definition.replaceAll("\\s+", "").replace("'", "").replace(".", "").replace("(", "").replace(")", "");
            Log.e("vitamin_definition result", new_vitamin_definition);

            String vitamin_deficiency = vitamins_ListView.getItemAtPosition(position).toString() + "_deficiency";
            String new_vitamin_deficiency =  vitamin_deficiency.replaceAll("\\s+", "").replace("'", "").replace(".", "").replace("(", "").replace(")", "");
             Log.e("vitamin_defieciency result", new_vitamin_deficiency);

            String vitamin_herbs = vitamins_ListView.getItemAtPosition(position).toString() + "_herbs";
             String new_vitamin_herbs =  vitamin_herbs.replaceAll("\\s+", "").replace("'", "").replace(".", "").replace("(", "").replace(")", "");
            Log.e("vitamin_herbs result", new_vitamin_herbs);

            String vitamin_foods = vitamins_ListView.getItemAtPosition(position).toString() + "_foods";
            String new_vitamin_foods =  vitamin_foods.replaceAll("\\s+", "").replace("'", "").replace(".", "").replace("(", "").replace(")", "");
            Log.e("vitamin_foods result", new_vitamin_foods);

            try{
                ((TextView)view.findViewById(R.id.vitamin_description)).setText("Definition: " +(ReferenceActivity.this.getResources().getIdentifier(new_vitamin_definition, "string", ReferenceActivity.this.getApplicationInfo().packageName)));                              
                ((TextView)view.findViewById(R.id.vitamin_deficiency)).setText("Deficiency: " +(ReferenceActivity.this.getResources().getIdentifier(new_vitamin_deficiency, "string", ReferenceActivity.this.getApplicationInfo().packageName)));                               
                ((TextView)view.findViewById(R.id.vitamin_herbs)).setText("Herbs: " +(ReferenceActivity.this.getResources().getIdentifier(new_vitamin_herbs, "string", ReferenceActivity.this.getApplicationInfo().packageName)));                              
                ((TextView)view.findViewById(R.id.vitamin_foods)).setText("Foods: " +(ReferenceActivity.this.getResources().getIdentifier(new_vitamin_foods, "string", ReferenceActivity.this.getApplicationInfo().packageName)));
            }catch(Exception e){
                Log.e("Vitamin Info Error", "Error loading Vitamin Info >> " + e.getMessage() + " //" + e.toString());
//String Check.  All strings working here
                            Log.e("String Test", getResources().getString(R.string.VitaminA_definition).toString());
                            Log.e("String Test", getResources().getString(R.string.VitaminA_deficiency).toString());
                            Log.e("String Test", getResources().getString(R.string.VitaminA_herbs).toString());
                            Log.e("String Test", getResources().getString(R.string.VitaminA_foods).toString());
            }

            // Creating the expand animation for the item
            ExpandAnimation expandAni = new ExpandAnimation(toolbar, 500);

            // Start the animation on the toolbar
            toolbar.startAnimation(expandAni);
            }
        });

        ((ViewPager) collection).addView(view, 0);
        return view;

的strings.xml

 <string-array name="vitaminlist">      
    <item>Vitamin A</item>
    <item>Vitmain B1 (Thiamine)</item>
    <item>Vitmain B2 (Riboflavin)</item>
    <item>Vitmain B3 (Niacin)</item>
    <item>Vitmain B4 </item>
    <item>Vitmain B5 (Pantothenic Acid)</item>
    <item>Vitmain B6 (Pyridoxine)</item>
    <item>Vitmain B12 (Cyanocobalamin, Cobalt)</item>       
    <item>Vitmain B13 (Orotic Acid)</item>
    <item>Vitmain B13 (Pangamic Acid)</item>
    <item>Vitamin C (Asorbic Acid)</item>
    <item>Vitamin D (Ergosterol)</item>
    <item>Vitmain E</item>
    <item>Vitamin F (Fatty Acid)</item>
    <item>Vitamin G</item> 
    <item>Vitamin H (Biotin)</item>
    <item>Vitamin K</item>
    <item>Vitamin L</item>
    <item>Vitamin P (Bioflavonoids)</item>    
    <item>Vitamin P (Rutin)</item> 
    <item>Vitamin T</item>
    <item>Vitamin U</item>
    <item>Choline</item>
    <item>Folic Acid (Inositol and Vitamin B)</item>       
    <item>PABA(Para-Amino-Benzino-Acid)</item>    
</string-array>

<string name="VitaminA_definition">Maintains health of tissue, hair; builds resistance, helps eyes, nasal passage, lungs, stress, etc.</string>
<string name="VitaminA_deficiency">Eye inflammation, weak vision, respiratory problems, skin diseases, poor sense of taste and smell, get infections easily, teeth and gum problems.</string>       
<string name="VitaminA_herbs">Alfalfa, Burdock, Comfrey, Dandelion, Eyebright, Okra Pods, Paprika, Parsley, Rosehip, Watercress.</string>
<string name="VitaminA_foods">Carrots, Green Leafy Vegetables, Melons, Tomatoes, Yams, Fruits.</string>

这是logcat:

 06-17 08:53:02.328: E/vitamin_definition result(13309): VitaminA_definition
 06-17 08:53:02.328: E/vitamin_defieciency result(13309): VitaminA_deficiency
 06-17 08:53:02.336: E/vitamin_herbs result(13309): VitaminA_herbs
 06-17 08:53:02.336: E/vitamin_foods result(13309): VitaminA_foods
 06-17 08:53:02.336: W/System.err(13309): java.lang.NullPointerException
 06-17 08:53:02.344: W/System.err(13309):   at com.bryanjrichardson.VITALITY.ReferenceActivity$MyPagerAdapter$2.onItemClick(ReferenceActivity.java:398)
 06-17 08:53:02.344: W/System.err(13309):   at android.widget.AdapterView.performItemClick(AdapterView.java:292)
 06-17 08:53:02.344: W/System.err(13309):   at android.widget.AbsListView.performItemClick(AbsListView.java:1082)
 06-17 08:53:02.344: W/System.err(13309):   at android.widget.AbsListView$PerformClick.run(AbsListView.java:2635)
 06-17 08:53:02.344: W/System.err(13309):   at android.widget.AbsListView$1.run(AbsListView.java:3306)
 06-17 08:53:02.344: W/System.err(13309):   at android.os.Handler.handleCallback(Handler.java:605)
 06-17 08:53:02.344: W/System.err(13309):   at android.os.Handler.dispatchMessage(Handler.java:92)
 06-17 08:53:02.344: W/System.err(13309):   at android.os.Looper.loop(Looper.java:137)
 06-17 08:53:02.344: W/System.err(13309):   at android.app.ActivityThread.main(ActivityThread.java:4722)
 06-17 08:53:02.344: W/System.err(13309):   at java.lang.reflect.Method.invokeNative(Native Method)
 06-17 08:53:02.344: W/System.err(13309):   at java.lang.reflect.Method.invoke(Method.java:511)
 06-17 08:53:02.352: W/System.err(13309):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
 06-17 08:53:02.352: W/System.err(13309):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
 06-17 08:53:02.352: W/System.err(13309):   at dalvik.system.NativeStart.main(Native Method)
 06-17 08:53:02.352: E/Vitamin Info Error(13309): Error loading Vitamin Info >> null //java.lang.NullPointerException

0 个答案:

没有答案