arrayAdapter导致应用程序强制关闭

时间:2012-09-04 11:31:04

标签: java android

我有一个活动从另一个活动加载sharedPreference并从中放入一些数据 当我编译我的app强制关闭时,sharedPreferenece使用arrayAdpter进入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当然

请帮助我

1 个答案:

答案 0 :(得分:3)

我认为您忘记使用适当的大小初始化String[]

而不是

String[] dawaaList;

String[] dawaaList = new String[1];

抛出异常的行是,

dawaaList[0] = theName;