我有一个列表,我需要添加到列表视图中,并且从不同的类中获取的数组列表是我的代码:
public class AdminUserDetails extends AppCompatActivity {
ListView listView;
List<String>userlist;
public static final String TAG="userdetails";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_user_details);
if (android.os.Build.VERSION.SDK_INT > 9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
listView=findViewById(R.id.list_item2);
String task= "adminuserdetails";
BackgroundTask backgroundTask= new BackgroundTask(this);
backgroundTask.execute(task);
// ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(AdminUserDetails.this,android.R.layout.simple_list_item_1,userlist);
// listView.setAdapter(arrayAdapter);
// ArrayList<String> userlist= new ArrayList<String>(backgroundTask1.arraylist);
// Log.d(TAG,userlist.toString());
}
public void getArrayList(List<String> arrayList){
userlist= arrayList;
Log.d(TAG,userlist.toString());
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(AdminUserDetails.this,android.R.layout.simple_list_item_1,userlist);
listView.setAdapter(arrayAdapter);
}
}
我收到此错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.a49ersense, PID: 28978
java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:6636)
at android.view.LayoutInflater.from(LayoutInflater.java:284)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:216)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:210)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:196)
at com.example.a49ersense.AdminUserDetails.getArrayList(AdminUserDetails.java:45)
at com.example.a49ersense.BackgroundTask.onPostExecute(BackgroundTask.java:308)
at com.example.a49ersense.BackgroundTask.onPostExecute(BackgroundTask.java:28)
at android.os.AsyncTask.finish(AsyncTask.java:755)
at android.os.AsyncTask.access$900(AsyncTask.java:192)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:772)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
我在arrayadapter遇到错误,请帮助我解决这个错误并指出我做错了什么地方。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。