I managed to successfully reload certain settings when the user presses the back button on the phone. When this happens I store their location and the first element in a list. Now the list is generated by pressing an image button. Once the list is generated you can press on any element and it will load a new class.
But when I recall the information from the list and press on it the app crashes giving me a
Null Pointer Exception. I’m aware of what this is but can’t understand why. Here is my code:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView( R.layout.new_sightings );
// Initialize location
user_lat_Value = (TextView)findViewById( R.id.lat_Value );
user_long_Value = (TextView)findViewById( R.id.long_Value );
// Have not received coordinates yet
gotCoord = false;
adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, listItems );
setListAdapter( adapter );
SharedPreferences settings = getSharedPreferences( PREFS_NAME,0 );
if( settings.contains( "ARRAY_LIST_VALUE_0" ) || settings.contains( "LAT" ) )
{
user_lat_Value.setText( settings.getString( "LAT", myNewLat ) );
user_long_Value.setText( settings.getString( "LONG", myNewLon ) );
listItems.add( settings.getString( "ARRAY_LIST_VALUE_0",tempStore ) );
adapter.notifyDataSetChanged();
}
reload = (Button)findViewById( R.id.reloadTen );
reload.setOnClickListener( this );
list = getListView();
list.setOnItemClickListener( new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id )
{
// Start an activity based on what list view item is pressed
Intent intent = new Intent( newSightings.this, newCompass.class );
// Pass the data we retrieved to the next activity
intent.putExtra( "info",data[position] );
startActivity( intent );
}
});
list.setTextFilterEnabled( true );
}
@Override
protected void onStop()
{
super.onStop();
gotCoord = false;
SharedPreferences settings = getSharedPreferences( PREFS_NAME,0 );
SharedPreferences.Editor editor = settings.edit();
// Store the latitude and longitude values and then reload them if known
editor.putString( "LAT",myNewLat );
editor.putString( "LONG",myNewLon );
// Place first element of array list in storage
if( adapter.getCount() > 0 )
{
Toast.makeText( getApplicationContext(), "Leaving Activity, Adapter Size: "+ adapter.getCount()+"\nStoring: "+listItems.get( 0 ).toString(), Toast.LENGTH_SHORT ).show();
tempStore = listItems.get( 0 ).toString();
editor.putString( "ARRAY_LIST_VALUE_0",tempStore );
/**
for( int i = 0; i < storeList.length; i++ )
{
storeList[i] = listItems.get( i ).toString();
editor.putString( "ARRAY_LIST_VALUE_"+i, listItems.get( i ).toString() );
}
*/
}
editor.commit();
}
Correct me if I am wrong but because I reload the data into the list adapter and press it shouldn’t it mimic the behaviour of an item in the list if it had just been loaded?
I’m confused..Any words?
Cheers
Based on your comment, it seems that the data variable is null and therefore data[position] throws an exception. Presumably this is data related to the adapter position but not something that is getting reloaded in your onCreate().