我正在尝试将一个值从 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>
是否可以从服务开始活动?
提前致谢。