0

我正在尝试将一个值从 FileObserver 传递给一个活动,如下所示 -

public class MyFileObserver extends FileObserver 
{
public String absolutePath;
final FileEvent fileevent  = new FileEvent(this);

final filehelper f_help = new filehelper(fileevent);
public MyFileObserver(String path) 
{
    super(path, FileObserver.ALL_EVENTS);
    absolutePath = path;
}

@Override
public void onEvent(int event, String path) 
{
    if (path == null) 
    {
        return;
    }
    //a new file or subdirectory was created under the monitored directory
    if ((FileObserver.CREATE & event)!=0) 
    {
        FileAccessLogStatic.accessLogMsg += absolutePath + "/" + path + " is created\n";
        Log.v(path+ " in FileObserver ====>>>> ",path);
        fileevent.insert(path);         
    }}}

这是 FileEvent 类中 insert() 的代码-

public class FileEvent extends ListActivity{
public String filename;

final adapter info = new adapter (this);
public FileEvent(MyFileObserver myFileObserver) {
    // TODO Auto-generated constructor stub
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

}

public void insert(String path) {
    // TODO Auto-generated method stub
    this.filename = path;
      int rowcount = info.getrowcountofpersons(); 

       Log.v("rowcount in new list onCreate: ", ""+info.getrowcountofpersons()+"");
       String[] values = new String[rowcount];
       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]);
       }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, values);
        ListView listView = (ListView) findViewById(android.R.id.list);
        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);


       // Assign adapter to List
        setListAdapter(adapter);  
      //  new Bullet(info).execute((Void)null);
    }}

当我运行此代码时,我在 Logcat 中收到以下错误 -

09-24 23:53:36.430: E/AndroidRuntime(5870): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.seperate_fileobserver/com.example.seperate_fileobserver.FileEvent}: java.lang.InstantiationException: can't instantiate class com.example.seperate_fileobserver.FileEvent; no empty constructor
09-24 23:53:36.430: E/AndroidRuntime(5870):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
09-24 23:53:36.430: E/AndroidRuntime(5870):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)

这就是我在 manifest.xml 文件中注册服务的方式 -

    <service 
        android:enabled="true" 
        android:name="com.example.seperate_fileobserver.FileModificationService">
    </service>

是否可以从服务开始活动?

提前致谢。

4

1 回答 1

1

Activity您必须提供任何子类型的空构造函数。如果您想让活动开始参数化 - 使用intent.putExtra添加活动所需的所有参数。

于 2013-09-25T05:13:18.373 回答