2012-09-04 54 views
0

我從另一個活動加載sharedPreference,並把一些數據從 sharedPreferenece成一個ListView使用arrayAdpter當我編譯我的應用程序的強制關閉 這個活動是我的代碼:arrayAdapter造成應用程序強行關閉

public class DawaaActivity extends Activity implements View.OnClickListener { 
/** Called when the activity is first created. */ 
Button add; 
ListView dataList; 
private SharedPreferences emportPref; 
String[] dawaaList; 
String theName; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    initialaiz(); 
    emportPref = getSharedPreferences("dawaaData", MODE_PRIVATE); 
    theName = emportPref.getString("subject", "no data found"); 
    Toast.makeText(this, theName, Toast.LENGTH_LONG).show(); 
    dawaaList[0] = theName; 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,dawaaList); 
    dataList.setAdapter(adapter); 
} 

private void initialaiz() { 
    // TODO Auto-generated method stub 
    add = (Button) findViewById(R.id.button3); 
    dataList = (ListView) findViewById(R.id.myListView); 
    add.setOnClickListener(this); 
} 

public void onClick(View v) { 
    // TODO Auto-generated method stub 
    Intent data = new Intent(DawaaActivity.this,SettingActivity.class); 
    startActivity(data); 
} 

當我提出這個兩行,如果代碼暗示:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,dawaaList); 
    dataList.setAdapter(adapter) 

應用程序是工作就好

但不填充的ListView當然幫我請

+1

您的logcat錯誤報告在哪裏? –

+0

dawaaList [0] = theName;//錯誤我認爲 – Carnal

回答

3

我想你忘了適當的大小來初始化String[]

而不是

String[] dawaaList; 

嘗試

String[] dawaaList = new String[1]; 

投擲的線例外是,

dawaaList[0] = theName; 
+0

謝謝,這是真正的問題廢話! –

+0

現在通過接受這個答案來增加你的聲望。 :-) – user370305

+1

你值得這個人給3分鐘,直到我可以投票給你的答案:-) –