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?