java.lang.IllegalStateException:使用ArrayAdapter的onCreate()之前的活动无法使用系统服务<;字符串>;
我正在尝试运行一个ListActivity,它接受一个文件名数组,这些文件名将被插入到数据库中。这是通过以下代码中的java.lang.IllegalStateException:使用ArrayAdapter的onCreate()之前的活动无法使用系统服务<;字符串>;,java,android,Java,Android,我正在尝试运行一个ListActivity,它接受一个文件名数组,这些文件名将被插入到数据库中。这是通过以下代码中的insert()完成的。我在ArrayAdapter收到非法状态异常。如果我从另一个类调用insert()函数。因此,我无法在onCreate()方法中定义ArrayAdapter。请参阅下面的代码,它定义了insert()方法- public class FileEvent extends ListActivity implements ObserverActivity{ pub
insert()
完成的。我在ArrayAdapter
收到非法状态异常。如果我从另一个类调用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());
}
*/
}
公共类FileEvent扩展ListActivity实现ObserverActivity{
公共静态最终字符串PREFS_NAME=“MyPreferencesFile”;
公共字符串文件名;
公共字符串路径;
MyFileObserver MyFileObserver;
公共适配器信息;
阵列适配器;
列表视图列表视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
this.info=新适配器(此);
listView=(listView)findViewById(android.R.id.list);
setContentView(R.layout.fileeventlist);
Bundle Bundle=getIntent().getExtras();
myFileObserver=新的myFileObserver(“/storage/sdcard/DCIM/SAMPLE\u IMAGES/”;
myFileObserver.registerObserver(这个);
myFileObserver.startWatching();
/*字符串var_from_prev_intent=bundle.getString(“路径”);
插入(var_from_prev_intent);
SharedReferences设置=GetSharedReferences(首选项名称,0);
字符串newpath=“”;
this.path=settings.getString(“name”,newpath);
Log.v(“FileEvent中的新路径:”,this.path)*/
}
受保护的void onPause(){
myFileObserver.stopWatching();
myFileObserver.unregisterObserver(此);
}
受保护的void onResume(){
myFileObserver.registerObserver(这个);
myFileObserver.startWatching();
}
公共void插入(字符串路径){
//TODO自动生成的方法存根
试一试{
Log.v(“要插入的文件名:”,路径);
对数v(“a3”、“a3”);
this.info=新适配器(此);
this.filename=路径;
对数v(“a4”、“a4”);
if(info==null)
{
System.out.println(“此处信息为空”);
}
其他的
{
System.out.println(“此处信息不为空。”);
}
int rowcount=info.getrowcountofpersons();
对数v(“a5”、“a5”);
Log.v(“创建时新列表中的rowcount:”,“+info.getrowcountofpersons()+”);
对数v(“a6”、“a6”);
字符串[]值=新字符串[行计数];
Log.v(“a7”、“a7”);
对于(int i=1;i您不能在onCreate之前执行public adapter info=new adapter(this);
。将其初始化为onCreate
像
在onCreate
方法中
info = new adapter(this);
还有一个嫌疑犯
public adapter info = new adapter(this);
ArrayAdapter<String> adapter;
public adapter info=新适配器(此);
阵列适配器;
我非常确定这不会编译。所以请验证它。尝试将代码移动到OnResume方法。在onCreate中初始化信息当我删除公共适配器信息=新适配器时,是否在manifest fileNo@Pankaj Kumar中添加了权限(此);
,其他一些使用info
变量的函数正在变为null。因此不能这样做。@nki是的,你是对的。这就是我从答案中删除该行的原因:).adapter是什么?如果此类然后将ArrayAdapter
更改为ArrayAdapter ArrayAdapter
以及此适配器的所有情况。是的,这是一个类。我应该保留相同的名称吗?很抱歉,我不知道如何使用ArrayAdapter。我已将ArrayAdapteradapter
更改为AdapterArrayAdapterArrayArr
。但也得到了同样的例外。做一件事,用adapter
重命名adapter
类。在java中,类名以大写字母开头。然后按照我的回答将其初始化为onCreate。@Naeem A.Malik,我不能这样做,因为我正在从另一个类调用insert()
。
info = new adapter(this);
public adapter info = new adapter(this);
ArrayAdapter<String> adapter;