Friday, 27 September 2013

java.lang.IllegalStateException: System services not available to Activities before onCreate() with ArrayAdapter

java.lang.IllegalStateException: System services not available to
Activities before onCreate() with ArrayAdapter

I am trying to run a ListActivity, which takes an array of filenames,
which are to be inserted into the database. This is done by insert() in
the following code. I am getting IllegalStateException at
ArrayAdapter<String>. If I am calling the insert() function from another
class. So, I am unable to define ArrayAdapter in the onCreate() method.
Please see the following code, which is defining insert() method -
public class FileEvent extends ListActivity implements ObserverActivity{
public static final String PREFS_NAME = "MyPreferencesFile";
public String filename;
public String path;
MyFileObserver myFileObserver;
public adapter info = new adapter(this);
ArrayAdapter<String> adapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
listView = (ListView) findViewById(android.R.id.list);
setContentView(R.layout.fileeventlist);
Bundle bundle = getIntent().getExtras();
myFileObserver = new
MyFileObserver("/storage/sdcard/DCIM/SAMPLE_IMAGES/");
myFileObserver.registerObserver(this);
myFileObserver.startWatching();
/* String var_from_prev_intent = bundle.getString("path");
insert(var_from_prev_intent);
SharedPreferences settings = getSharedPreferences (PREFS_NAME,0);
String newpath = "";
this.path = settings.getString("name",newpath);
Log.v("New path in FileEvent : ",this.path);*/
}
protected void onPause(){
myFileObserver.stopWatching();
myFileObserver.unregisterObserver(this);
}
protected void onResume(){
myFileObserver.registerObserver(this);
myFileObserver.startWatching();
}
public void insert(String path) {
// TODO Auto-generated method stub
//try{
Log.v("FileName to insert : ",path);
Log.v("a3","a3");
this.filename = path;
Log.v("a4","a4");
int rowcount = info.getrowcountofpersons();
Log.v("a5","a5");
Log.v("rowcount in new list onCreate: ",
""+info.getrowcountofpersons()+"");
Log.v("a6","a6");
String[] values = new String[rowcount];
Log.v("a7","a7");
for(int i =1;i<=rowcount;i++)
{
values[i-1]=info.getPersonList(i);
//Toast.makeText(getApplicationContext(), values[i-1],
Toast.LENGTH_LONG).show();
System.out.println("in for loop now"+values[i-1]);
}
Log.v("a8","a8");
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
Log.v("a9","a9");
Log.v("a10","a10");
listView.setItemsCanFocus(false);
Log.v("a11","a11");
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Log.v("a12","a12");
// Assign adapter to List
setListAdapter(adapter);
// new Bullet(info).execute((Void)null);
Log.v("a13","a13");
/*}
catch (Exception e)
{
Log.v("Error in insert() definition FileEvent.java :
",e.toString());
}*/
}
protected void onListItemClick(ListView l, View v, int position, long id) {
// try{
Log.v("a14","a14");
super.onListItemClick(l, v, position, id);
Log.v("a15","a15");
// ListView Clicked item index
int itemPosition = position;
Log.v("a16","a16");
// ListView Clicked item value
String itemValue = (String) l.getItemAtPosition(position);
Log.v("a17","a17");
// content.setText("Click : \n Position :"+itemPosition+" \n
ListItem : " +itemValue);
String personname = itemValue;
Log.v("a18","a18");
try
{
System.out.println("paths in FileEvent : "+filename);
info.insert(filename,personname);
Log.v("a19","a19");
}
catch(Exception e)
{
Log.v("a20","a20");
e.printStackTrace();
Log.v("a21","a21");
}
Log.v("a22","a22");
/* }
catch (Exception e)
{
Log.v("Error in insert() definition FileEvent.java :
",e.toString());
}
*/
}
And the following logcat I am getting, when I run the code -
09-27 06:37:23.269: A/FileObserver(2284): Unhandled exception in
FileObserver com.example.sample_fileobserver.MyFileObserver@b11955a0
09-27 06:37:23.269: A/FileObserver(2284): java.lang.IllegalStateException:
System services not available to Activities before onCreate()
09-27 06:37:23.269: A/FileObserver(2284): at
android.app.Activity.getSystemService(Activity.java:4492)
09-27 06:37:23.269: A/FileObserver(2284): at
android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
09-27 06:37:23.269: A/FileObserver(2284): at
android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
09-27 06:37:23.269: A/FileObserver(2284): at
com.example.sample_fileobserver.FileEvent.insert(FileEvent.java:76)
09-27 06:37:23.269: A/FileObserver(2284): at
com.example.sample_fileobserver.MyFileObserver.onEvent(MyFileObserver.java:59)
09-27 06:37:23.269: A/FileObserver(2284): at
android.os.FileObserver$ObserverThread.onEvent(FileObserver.java:125)
09-27 06:37:23.269: A/FileObserver(2284): at
android.os.FileObserver$ObserverThread.observe(Native Method)
09-27 06:37:23.269: A/FileObserver(2284): at
android.os.FileObserver$ObserverThread.run(FileObserver.java:88)
Please tell me where I made mistake.
Thanks in advance.

No comments:

Post a Comment