0

我有一个活动,它从另一个活动加载 sharedPreference,并在我编译我的应用程序时使用 arrayAdpter 将一些数据从 sharedPreferenece 放入 listView 强制关闭这是我的代码:

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 当然

请帮帮我

4

1 回答 1

3

我想你忘了String[]用适当的大小初始化,

代替

String[] dawaaList;

尝试

String[] dawaaList = new String[1];

抛出异常的行是,

dawaaList[0] = theName;
于 2012-09-04T11:33:57.853 回答