我有一个活动,可以让用户在按下按钮时加载一些数据。该数据被传递给一个列表,并且每个列表项都有不同的数据存储在其中。当他们点击列表项目时,它会启动一个将它们传递给新类别的意图。将意图附加到从SharedPreferences恢复的列表项目
但是,当我存储从按下这个按钮返回的第一个值,即使用onStop()和使用SharedPreferences,并点击它,我得到一个错误。我认为这可能与意图有关 - 列表项不附加到它。下面是部分代码:
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
setListAdapter(adapter);
list = getListView();
list.setOnItemClickListener(this);
if(settings.contains("ARRAY_LIST_VALUE_0"))
{
listItems.add(0, settings.getString("ARRAY_LIST_VALUE_0",tempStore));
adapter.notifyDataSetChanged();
}
reload = (Button)findViewById(R.id.reloadTen);
reload.setOnClickListener(this);
// Original onItemClicked was in here
list.setTextFilterEnabled(true);
}
@Override
public void onItemClick(AdapterView<?> arg0, View listItem, int position, long arg3)
{
// 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);
}
@Override
protected void onStop()
{
super.onStop();
SharedPreferences settings = getSharedPreferences(PREFS_NAME,0);
SharedPreferences.Editor editor = settings.edit();
if(usrPrssdBtn)
{
// 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);
editor.putString("DATA",data[arryTrckr]);
}
}
editor.commit();
}
所以 - 当我运行的活动,然后压回,然后再返回到该活动的存储列表项是存在的。问题是当我点击它崩溃的应用程序。有什么问题?我在这行得到一个NullPointerException:
intent.putExtra("info",data[position]);
什么是取数据“数据“在你的代码 – 2012-04-03 03:21:25
数据是一个数组,它保存来自web解析的结果的值并将它们存储以传递到下一个活动 – Katana24 2012-04-03 10:37:26
用onStop() – 2012-04-03 11:06:48