-1

I am quite new to this android eclipse programing. i have spent days searching the web for an answer but have not been able to find one yet. i have managed to get a listview working using fragments but the string data is hard coded and i would like to use a string from the resources xml file.

this is the hard coded data

public static final String[] message1 = new String[] { "Strawberry",
       "Banana", "Orange", "Mixed" };

this is my string //String array data to display for row data message1

String[] message1 = getResources().getStringArray(R.array.main_page_list_string);

from my strings.xml file

<string-array name="main_page_list_string">
    <item >Instruments</item>
    <item >Capasiters</item>
    <item >Resisters</item>
    <item >Ohms Law</item>
    <item >IP Ratings</item>
    <item>Thermostats</item>
    <item>Converters</item>
    <item >RJ 45 A and B plugs</item><item>Calculators</item>
</string-array>

every time i replace

public static final String[] message1 = new String[] { "Strawberry",
       "Banana", "Orange", "Mixed" };

with

String[] message1 = getResources().getStringArray(R.array.main_page_list_string);

my app crashes and the error message is null point exception.

can anyone tell me how to do this?enter code here thanks

This is the java class file

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class ImageTextListBaseAdapterActivity extends Activity implements
OnItemClickListener {

  ***********************************************************************************
  This is the code i am trying to use
    //String array data to display for row data message1
    String[] myStringData=getResources().getStringArray(R.array.main_page_list_string); 

    This is the code that i am trying to replace 

   ********************************************************************************  
    //    public static final String[] message1 = new String[] { "Strawberry",
    //        "Banana", "Orange", "Mixed" };
   ******************************************************************************
    //String array data to display for row data message2
    public static final String[] descriptions = new String[] {
        "It is an aggregate accessory fruit",
        "It is the largest herbaceous flowering plant", "Citrus Fruit",
    "Mixed Fruits" };

    //icons used in list item rows
    public static final Integer[] images = { R.drawable.ic_launcher,
        R.drawable.songs_gray, R.drawable.videos_gray, R.drawable.photos_gray };

    ListView listView;
    List<RowItem> rowItems;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_mainlist);

        rowItems = new ArrayList<RowItem>();
        for (int i = 0; i < message1.length; i++) {
            RowItem item = new RowItem(images[i], message1[i], descriptions[i]);
            rowItems.add(item);
        }

        listView = (ListView) findViewById(R.id.MainPageListView);
        CustomBaseAdapter adapter = new CustomBaseAdapter(this, rowItems);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        Toast toast = Toast.makeText(getApplicationContext(),
                "Item " + (position + 1) + ": " + rowItems.get(position),
                Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
        toast.show();
    }
}

this is the error from LogCat

07-22 08:41:12.659: E/AndroidRuntime(13799): FATAL EXCEPTION: main 07-22 08:41:12.659: E/AndroidRuntime(13799): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.rogerssteve.android.elecinstro/com.rogerssteve.android.elecinstro.ImageTextListBaseAdapterActivity}: java.lang.NullPointerException 07-22 08:41:12.659: E/AndroidRuntime(13799): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)

i cannot see any line numbers?

buckau
  • 35
  • 2
  • 5
  • Suggest you post your `getResources()` and `getStringArray()` functions so we can see what you're trying to do. I would think at first glance, you might want to look in to some sort of XML parser such as that offered by JAXB to get data from your XML file. – Ryan J Jul 21 '14 at 21:20
  • @RyanJ those functions are part of is Android API – firegnom Jul 21 '14 at 21:26

1 Answers1

1

Hi you are changing a constant static final which is accessible without object instance to a field which requite an instance first off all i would recommend moving your initalisation to constructor of your object

public class Sample {

    String[] message1 ;

    public Sample(){
         message1 = getResources().getStringArray(R.array.main_page_list_string);
    }
}

To tell you a bit more you need to paste your error message (edit : sorry to more specific what i mean is an error message gives you an exact line where was NPE, could you paste exact line ?)

firegnom
  • 833
  • 7
  • 20
  • hi all well i found the answer, this link http://stackoverflow.com/questions/12506045/how-to-use-getstring-on-static-string-before-oncreate explanations the problem and the answer was easy after that thanks all – buckau Jul 22 '14 at 04:51