使用 AsyncTask with Fragments,ArrayAdapter 要求资源 ID 为 TextView
Using AsyncTask with Fragments,ArrayAdapter requires the resource ID to be a TextView
如何实现AsyncTask 我有片段,我应该在行中写什么
listview = (ListView) ?????? findViewById(R.id.listviewget);
如果我把它放在 onCreateView
listview = (ListView) veiw.findViewById(R.id.listviewget);
我得到错误 below.All 获取代码工作是因为我已经签入 activity 并且它从服务器获取数据但不能在片段中工作
01-22 21:59:32.850: E/ArrayAdapter(32155): You must supply a resource
ID for a TextView 01-22 21:59:32.890: E/AndroidRuntime(32155): FATAL
EXCEPTION: main 01-22 21:59:32.890: E/AndroidRuntime(32155):
java.lang.IllegalStateException: ArrayAdapter requires the resource ID
to be a TextView 01-22 21:59:32.890: E/AndroidRuntime(32155): at
android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
01-22 21:59:32.890: E/AndroidRuntime(32155): at
android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 01-22
21:59:32.890: E/AndroidRuntime(32155): at
android.widget.AbsListView.obtainView(AbsListView.java:2075) 01-22
21:59:32.890: E/AndroidRuntime(32155): at
android.widget.ListView.makeAndAddView(ListView.java:2004) 01-22
21:59:32.890: E/AndroidRuntime(32155): at
android.widget.ListView.fillDown(ListView.java:856) 01-22
21:59:32.890: E/AndroidRuntime(32155): at
android.widget.ListView.fillFromTop(ListView.java:916) 01-22
21:59:32.890: E/AndroidRuntime(32155): at
android.widget.ListView.layoutChildren(ListView.java:1857) 01-22
21:59:32.890: E/AndroidRuntime(32155): at
android.widget.AbsListView.onLayout(AbsListView.java:1905) 01-22
21:59:32.890: E/AndroidRuntime(32155): at
android.view.View.layout(View.java:11359) 01-22 21:59:32.890:
E/AndroidRuntime(32155): at
android.view.ViewGroup.layout(ViewGroup.java:4531) 01-22 21:59:32.890:
E/AndroidRuntime(32155): at
android.widget.RelativeLayout.onLayout(RelativeLayout.java:925) 01-22
21:59:32.890: E/AndroidRuntime(32155): at
android.view.View.layout(View.java:11359) 01-22 21:59:32.890:
E/AndroidRuntime(32155): at
android.view.ViewGroup.layout(ViewGroup.java:4531) 01-22 21:59:32.890:
E/AndroidRuntime(32155): at
android.widget.FrameLayout.onLayout(FrameLayout.java:431) 01-22
21:59:32.890: E/AndroidRuntime(32155): at
android.view.View.layout(View.java:11359) 01-22 21:59:32.890:
E/AndroidRuntime(32155): at
android.view.ViewGroup.layout(ViewGroup.java:4531) 01-22 21:59:32.890:
E/AndroidRuntime(32155): at
android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:801)
01-22 21:59:32.890: E/AndroidRuntime(32155): at
android.view.View.layout(View.java:11359) 01-22 21:59:32.890:
E/AndroidRuntime(32155): at
android.view.ViewGroup.layout(ViewGroup.java:4531) 01-22 21:59:32.890:
E/AndroidRuntime(32155): at
android.widget.FrameLayout.onLayout(FrameLayout.java:431) 01-22
21:59:32.890: E/AndroidRuntime(32155): at
android.view.View.layout(View.java:11359) 01-22 21:59:32.890:
E/AndroidRuntime(32155): at
android.view.ViewGroup.layout(ViewGroup.java:4531) 01-22 21:59:32.890:
E/AndroidRuntime(32155): at
android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
01-22 21:59:32.890: E/AndroidRuntime(32155): at
android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
01-22 21:59:32.890: E/AndroidRuntime(32155): at
android.widget.LinearLayout.onLayout(LinearLayout.java:1399) 01-22
21:59:32.890: E/AndroidRuntime(32155): at
android.view.View.layout(View.java:11359) 01-22 21:59:32.890:
E/AndroidRuntime(32155): at
android.view.ViewGroup.layout(ViewGroup.java:4531) 01-22 21:59:32.890:
E/AndroidRuntime(32155): at
android.widget.FrameLayout.onLayout(FrameLayout.java:431) 01-22
21:59:32.890: E/AndroidRuntime(32155): at
android.view.View.layout(View.java:11359) 01-22 21:59:32.890:
E/AndroidRuntime(32155): at
android.view.ViewGroup.layout(ViewGroup.java:4531) 01-22 21:59:32.890:
E/AndroidRuntime(32155): at
android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1665)
01-22 21:59:32.890: E/AndroidRuntime(32155): at
android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2695) 01-22
21:59:32.890: E/AndroidRuntime(32155): at
android.os.Handler.dispatchMessage(Handler.java:99) 01-22
21:59:32.890: E/AndroidRuntime(32155): at
android.os.Looper.loop(Looper.java:156) 01-22 21:59:32.890:
E/AndroidRuntime(32155): at
android.app.ActivityThread.main(ActivityThread.java:4987) 01-22
21:59:32.890: E/AndroidRuntime(32155): at
java.lang.reflect.Method.invokeNative(Native Method) 01-22
21:59:32.890: E/AndroidRuntime(32155): at
java.lang.reflect.Method.invoke(Method.java:511) 01-22 21:59:32.890:
E/AndroidRuntime(32155): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-22 21:59:32.890: E/AndroidRuntime(32155): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 01-22
21:59:32.890: E/AndroidRuntime(32155): at
dalvik.system.NativeStart.main(Native Method) 01-22 21:59:32.890:
E/AndroidRuntime(32155): Caused by: java.lang.ClassCastException:
android.widget.RelativeLayout cannot be cast to
android.widget.TextView 01-22 21:59:32.890: E/AndroidRuntime(32155):
at
android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
01-22 21:59:32.890: E/AndroidRuntime(32155): ... 39 more
public class MyLibrary extends Fragment {
ListView listview;
List<ParseObject> data;
ProgressDialog mProgressDialog;
ArrayAdapter<String> adapter;
public MyLibrary()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Parse.initialize(getActivity(), "ASD", "ASD");
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
View view=inflater.inflate(R.layout.getnewlay,container, false);
new GetData().execute();
return view;
}
private class GetData extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setTitle("Please wait");
mProgressDialog.setMessage("Getting data...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
ParseQuery query = new ParseQuery("ASD");
query.orderByDescending("_created_at");
try {
data= query.find();
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
listview = (ListView) ?????? findViewById(R.id.listviewget);
adapter = new ArrayAdapter<String>(getActivity(),
R.layout.getnewlay);
for (ParseObject name : data) {
adapter.add((String) name.get("name"));
}
listview.setAdapter(adapter);
mProgressDialog.dismiss();
}
}
}
ArrayAdapter requires the resource ID to be a TextView
意味着 R.layout.getnewlay
布局包含 TextView 但在 RelativeLayout
内部,因此您可以将任何 id 分配给 TextView 并使用 ArrayAdapter
的另一个构造函数,它需要 TextView id:
adapter = new ArrayAdapter<String>(getActivity(),
R.layout.getnewlay,R.id.textView_Id);
或从 xml 中删除 RelativeLayout
布局并使 TextView
作为根视图:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
.....
/>
How to access View from Fragment layout in onPostExecute method:
使用getView
方法:
listview = (ListView)getView().findViewById(R.id.listviewget);
检查您的布局getnewlay。它在布局中应该只有 TextView 作为父级(甚至没有任何布局,如线性或相对布局)
否则你可以用 android.R.layout.simple_list_item_1
代替 R.layout.getnewlay
在布局中传递 TextView 的资源 ID 'getnewlay' 喜欢
adapter = new ArrayAdapter<>(getActivity(), R.layout.getnewlay, R.id.textViewResourceId_in_getnewlay)
如何实现AsyncTask 我有片段,我应该在行中写什么
listview = (ListView) ?????? findViewById(R.id.listviewget);
如果我把它放在 onCreateView
listview = (ListView) veiw.findViewById(R.id.listviewget);
我得到错误 below.All 获取代码工作是因为我已经签入 activity 并且它从服务器获取数据但不能在片段中工作
01-22 21:59:32.850: E/ArrayAdapter(32155): You must supply a resource ID for a TextView 01-22 21:59:32.890: E/AndroidRuntime(32155): FATAL EXCEPTION: main 01-22 21:59:32.890: E/AndroidRuntime(32155): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.widget.AbsListView.obtainView(AbsListView.java:2075) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.widget.ListView.makeAndAddView(ListView.java:2004) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.widget.ListView.fillDown(ListView.java:856) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.widget.ListView.fillFromTop(ListView.java:916) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.widget.ListView.layoutChildren(ListView.java:1857) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.widget.AbsListView.onLayout(AbsListView.java:1905) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.view.View.layout(View.java:11359) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.view.ViewGroup.layout(ViewGroup.java:4531) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.view.View.layout(View.java:11359) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.view.ViewGroup.layout(ViewGroup.java:4531) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.widget.FrameLayout.onLayout(FrameLayout.java:431) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.view.View.layout(View.java:11359) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.view.ViewGroup.layout(ViewGroup.java:4531) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:801) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.view.View.layout(View.java:11359) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.view.ViewGroup.layout(ViewGroup.java:4531) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.widget.FrameLayout.onLayout(FrameLayout.java:431) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.view.View.layout(View.java:11359) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.view.ViewGroup.layout(ViewGroup.java:4531) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.view.View.layout(View.java:11359) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.view.ViewGroup.layout(ViewGroup.java:4531) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.widget.FrameLayout.onLayout(FrameLayout.java:431) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.view.View.layout(View.java:11359) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.view.ViewGroup.layout(ViewGroup.java:4531) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1665) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2695) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.os.Handler.dispatchMessage(Handler.java:99) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.os.Looper.loop(Looper.java:156) 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.app.ActivityThread.main(ActivityThread.java:4987) 01-22 21:59:32.890: E/AndroidRuntime(32155): at java.lang.reflect.Method.invokeNative(Native Method) 01-22 21:59:32.890: E/AndroidRuntime(32155): at java.lang.reflect.Method.invoke(Method.java:511) 01-22 21:59:32.890: E/AndroidRuntime(32155): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 01-22 21:59:32.890: E/AndroidRuntime(32155): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 01-22 21:59:32.890: E/AndroidRuntime(32155): at dalvik.system.NativeStart.main(Native Method) 01-22 21:59:32.890: E/AndroidRuntime(32155): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView 01-22 21:59:32.890: E/AndroidRuntime(32155): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379) 01-22 21:59:32.890: E/AndroidRuntime(32155): ... 39 more
public class MyLibrary extends Fragment {
ListView listview;
List<ParseObject> data;
ProgressDialog mProgressDialog;
ArrayAdapter<String> adapter;
public MyLibrary()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Parse.initialize(getActivity(), "ASD", "ASD");
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
View view=inflater.inflate(R.layout.getnewlay,container, false);
new GetData().execute();
return view;
}
private class GetData extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setTitle("Please wait");
mProgressDialog.setMessage("Getting data...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
ParseQuery query = new ParseQuery("ASD");
query.orderByDescending("_created_at");
try {
data= query.find();
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
listview = (ListView) ?????? findViewById(R.id.listviewget);
adapter = new ArrayAdapter<String>(getActivity(),
R.layout.getnewlay);
for (ParseObject name : data) {
adapter.add((String) name.get("name"));
}
listview.setAdapter(adapter);
mProgressDialog.dismiss();
}
}
}
ArrayAdapter requires the resource ID to be a TextView
意味着 R.layout.getnewlay
布局包含 TextView 但在 RelativeLayout
内部,因此您可以将任何 id 分配给 TextView 并使用 ArrayAdapter
的另一个构造函数,它需要 TextView id:
adapter = new ArrayAdapter<String>(getActivity(),
R.layout.getnewlay,R.id.textView_Id);
或从 xml 中删除 RelativeLayout
布局并使 TextView
作为根视图:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
.....
/>
How to access View from Fragment layout in onPostExecute method:
使用getView
方法:
listview = (ListView)getView().findViewById(R.id.listviewget);
检查您的布局getnewlay。它在布局中应该只有 TextView 作为父级(甚至没有任何布局,如线性或相对布局)
否则你可以用 android.R.layout.simple_list_item_1
代替 R.layout.getnewlay
在布局中传递 TextView 的资源 ID 'getnewlay' 喜欢
adapter = new ArrayAdapter<>(getActivity(), R.layout.getnewlay, R.id.textViewResourceId_in_getnewlay)