BaseAdapter类不会在Asynctask – Android中设置Addapter
作者:互联网
我有asynctask收集用户名,评论和数字.它将它们放入字符串中,然后假设调用BaseAdapter类,创建适配器,并将适配器设置为类.但我的代码不起作用,它崩溃了应用程序,这是我的代码
public class DashboardActivity extends ListActivity {
String comments[];
String usernames[];
String numbers[];
ListView lstComments;
class CreateCommentLists extends BaseAdapter{
Context ctx_invitation;
String[] listComments;
String[] listNumbers;
String[] listUsernames;
public CreateCommentLists(String[] comments, String[] usernames, String[] numbers)
{
super();
listComments = comments;
listNumbers = usernames;
listUsernames = numbers;
}
@Override
public int getCount() {
if(null == listComments)
{
return 0;
}
// TODO Auto-generated method stub
return listComments.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listComments[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = null;
try
{
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater)ctx_invitation.getSystemService(inflater);
v = li.inflate(R.layout.list_item, null);
TextView commentView = (TextView)v.findViewById(R.id.listComment);
TextView NumbersView = (TextView)v.findViewById(R.id.listNumber);
TextView usernamesView = (TextView)v.findViewById(R.id.listPostedBy);
commentView.setText(listComments[position]);
NumbersView.setText(listNumbers[position]);
usernamesView.setText(listUsernames[position]);
}
catch(Exception e)
{
e.printStackTrace();
}
return v;
}
}
class loadComments extends AsyncTask<JSONObject, String, JSONObject> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
protected JSONObject doInBackground(JSONObject... params) {
JSONObject json2 = CollectComments.collectComments(usernameforcomments, offsetNumber);
return json2;
}
@Override
protected void onPostExecute(JSONObject json2) {
try {
if (json2.getString(KEY_SUCCESS) != null) {
registerErrorMsg.setText("");
String res2 = json2.getString(KEY_SUCCESS);
if(Integer.parseInt(res2) == 1){
JSONArray commentArray = json2.getJSONArray(KEY_COMMENT);
String comments[] = new String[commentArray.length()];
for ( int i=0; i<commentArray.length(); i++ ) {
comments[i] = commentArray.getString(i);
}
JSONArray numbersArray = json2.getJSONArray(KEY_NUMBER);
String numbers[] = new String[numbersArray.length()];
for ( int i=0; i<numbersArray.length(); i++ ) {
numbers[i] = numbersArray.getString(i);
}
JSONArray usernameArray = json2.getJSONArray(KEY_USERNAME);
String usernames[] = new String[usernameArray.length()];
for ( int i=0; i<usernameArray.length(); i++ ) {
usernames[i] = usernameArray.getString(i);
}
CreateCommentLists mycmlist = new CreateCommentLists(comments, usernames, numbers);
lstComments = (ListView)findViewById(android.R.id.list);
lstComments.setAdapter(mycmlist);
}//end if key is == 1
else{
// Error in registration
registerErrorMsg.setText(json2.getString(KEY_ERROR_MSG));
}//end else
}//end if
} //end try
catch (JSONException e) {
e.printStackTrace();
}//end catch
}
}
new loadComments().execute();
}
这是我的logCat
07-17 17:07:13.436: E/AndroidRuntime(23880): FATAL EXCEPTION: main
07-17 17:07:13.436: E/AndroidRuntime(23880): java.lang.NullPointerException
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.widget.ListView.measureScrapChild(ListView.java:1163)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.widget.ListView.measureHeightOfChildren(ListView.java:1294)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.widget.ListView.onMeasure(ListView.java:1155)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.view.View.measure(View.java:13011)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.view.View.measure(View.java:13011)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:579)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:392)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.view.View.measure(View.java:13011)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1017)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.widget.LinearLayout.onMeasure(LinearLayout.java:555)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.view.View.measure(View.java:13011)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.view.View.measure(View.java:13011)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.view.View.measure(View.java:13011)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.view.View.measure(View.java:13011)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-17 17:07:13.436: E/AndroidRuntime(23880): at com.navdrawer.SimpleSideDrawer.onMeasure(SimpleSideDrawer.java:443)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.view.View.measure(View.java:13011)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-17 17:07:13.436: E/AndroidRuntime(23880): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2162)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.view.View.measure(View.java:13011)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1091)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2505)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.os.Looper.loop(Looper.java:154)
07-17 17:07:13.436: E/AndroidRuntime(23880): at android.app.ActivityThread.main(ActivityThread.java:4945)
07-17 17:07:13.436: E/AndroidRuntime(23880): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 17:07:13.436: E/AndroidRuntime(23880): at java.lang.reflect.Method.invoke(Method.java:511)
07-17 17:07:13.436: E/AndroidRuntime(23880): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-17 17:07:13.436: E/AndroidRuntime(23880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-17 17:07:13.436: E/AndroidRuntime(23880): at dalvik.system.NativeStart.main(Native Method)
解决方法:
您是否检查过从getView()方法返回的View v是否为空?捕获异常捕获所有异常.在我看来并不是真的有帮助.这里发生的是你没有启动Context ctx_invitation.您应该在构造函数中执行此操作.现在,由于ctx_invitation为null,它会导致一个由catch块捕获的NullPointerException.和视图v保持为空.
更改CreateCommentLists的构造函数:
public CreateCommentLists(String[] comments, String[] usernames, String[] numbers, DashboardActivity context)
{
super();
listComments = comments;
listNumbers = usernames;
listUsernames = numbers;
ctx_invitation = context;
}
也改变以下内容:
CreateCommentLists mycmlist = new CreateCommentLists(comments, usernames, numbers, DashboardActivity.this);
标签:android,string,android-asynctask,baseadapter 来源: https://codeday.me/bug/20190925/1817023.html