-1

我正在尝试运行一个 ListActivity,它需要一个文件名数组,这些文件名将被插入到数据库中。这是通过insert()以下代码完成的。我在ArrayAdapter<String>. 如果我insert()从另一个类调用该函数。所以,我无法在onCreate()方法中定义 ArrayAdapter。请参阅以下代码,它是定义insert()方法 -

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 ;
ArrayAdapter<String> adapterarr;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
   this.info  = new adapter(this);
    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.info = new adapter(this);
this.filename = path; 
Log.v("a4","a4");
if(info == null)
{
    System.out.println("info is null here.");
}
else
{
    System.out.println("info is not null here.");
}
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");


 adapterarr = 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(adapterarr);  
//  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());
    }

*/   

} 

当我运行代码时,我得到了以下 logcat -

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)

请告诉我我在哪里做错了。

提前致谢。

4

2 回答 2

5

你不能public adapter info = new adapter(this);在 onCreate 之前做。将其初始化为 onCreate。

public adapter info; // Removed  = new adapter(this);

并且在onCreate方法中这样做

info = new adapter(this);

另一个怀疑是

public adapter info = new adapter(this);
ArrayAdapter<String> adapter;

我很确定这不会编译。所以验证一下。

于 2013-09-27T10:53:03.600 回答
1

尝试将您的代码移动到 OnResume 方法。

于 2013-09-27T10:54:21.557 回答