當用戶按下手機上的後退按鈕時,我設法成功地重新加載了某些設置。發生這種情況時,我將它們的位置和第一個元素存儲在列表中。現在通過按圖像按鈕生成列表。一旦列表生成,你可以按任何元素,它會加載一個新的類。列表項目未啓動當由共享首選項重新加載時的活動
但是,當我從列表中記得的信息,並按下它的應用程序崩潰給我一個 空指針異常。我知道這是什麼,但不明白爲什麼。這裏是我的代碼:
@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();
}
糾正我,如果我錯了,而是因爲我重新加載數據到列表中的適配器,然後按它不應該是模仿在列表中的項目的行爲,如果它剛剛被裝?
我很困惑......任何單詞?
乾杯
什麼是NullPointerException? – Samuel 2012-04-02 20:08:52
第114行 - 哪一個是intent.putExtra()行 – Katana24 2012-04-02 22:23:59