Karam Alem
Karam Alem

Reputation: 32

arrayAdapter caused the application to force close

I have an activity that load sharedPreference from another activity and put some data from the sharedPreferenece into a listView using the arrayAdpter when I compile my app its forced close this is me code:

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);
}

when I make this two line if code hinted :

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

the app is work just fine

but not filling the listView Of course

help me please

Upvotes: 0

Views: 133

Answers (1)

user370305
user370305

Reputation: 109257

I think you forgot to initialize String[] with proper size,

Instead of

String[] dawaaList;

try

String[] dawaaList = new String[1];

The line which throws Exception is,

dawaaList[0] = theName;

Upvotes: 3

Related Questions