我有一个异步任务,它收集用户名、评论和数字并将它们放入字符串中。然后假设这些字符串用于 BaseAdapter,但是当 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(Context ctx_invitation, String[] comments, String[] usernames, String[] numbers)
{
super();
this.ctx_invitation = ctx_invitation;
listComments = comments;
listNumbers = usernames;
listUsernames = numbers;
}
@Override
public int getCount() {
// 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;
}
}
final CreateCommentLists mycmlist = new CreateCommentLists(this, comments, usernames, numbers);
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);
}*/
}//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-16 22:00:05.866: D/AndroidRuntime(10072): Shutting down VM
07-16 22:00:05.866: W/dalvikvm(10072): threadid=1: thread exiting with uncaught exception (group=0x40ac9228)
07-16 22:00:05.896: E/AndroidRuntime(10072): FATAL EXCEPTION: main
07-16 22:00:05.896: E/AndroidRuntime(10072): java.lang.NullPointerException
07-16 22:00:05.896: E/AndroidRuntime(10072): at com.example.DashboardActivity$1CreateCommentLists.getCount(DashboardActivity.java:389)
07-16 22:00:05.896: E/AndroidRuntime(10072): at android.widget.ListView.setAdapter(ListView.java:460)
07-16 22:00:05.896: E/AndroidRuntime(10072): at com.example.DashboardActivity$1loadComments.onPostExecute(DashboardActivity.java:539)
07-16 22:00:05.896: E/AndroidRuntime(10072): at com.example.DashboardActivity$1loadComments.onPostExecute(DashboardActivity.java:1)
07-16 22:00:05.896: E/AndroidRuntime(10072): at android.os.AsyncTask.finish(AsyncTask.java:602)
07-16 22:00:05.896: E/AndroidRuntime(10072): at android.os.AsyncTask.access$600(AsyncTask.java:156)
07-16 22:00:05.896: E/AndroidRuntime(10072): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
07-16 22:00:05.896: E/AndroidRuntime(10072): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 22:00:05.896: E/AndroidRuntime(10072): at android.os.Looper.loop(Looper.java:154)
07-16 22:00:05.896: E/AndroidRuntime(10072): at android.app.ActivityThread.main(ActivityThread.java:4945)
07-16 22:00:05.896: E/AndroidRuntime(10072): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 22:00:05.896: E/AndroidRuntime(10072): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 22:00:05.896: E/AndroidRuntime(10072): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-16 22:00:05.896: E/AndroidRuntime(10072): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-16 22:00:05.896: E/AndroidRuntime(10072): at dalvik.system.NativeStart.main(Native Method)