我有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)
答案 0 :(得分:1)
您是否检查过从getView()方法返回的View
v是否为空?捕获Exception
捕获所有异常。在我看来并不是真的有帮助。这里发生的事情是你没有发起Context ctx_invitation
。您应该在构造函数中执行此操作。现在,由于ctx_invitation
为空,它会导致NullPointerException
被catch块捕获。并且View
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);
答案 1 :(得分:1)
我希望这可能有所帮助,
onPostExecute包含(Inside AsyncTask)
ExampleAdapter sectionedAdapter = new EfficientAdapter(ClassInfoThread.this,getBaseContext()); listView.setAdapter(sectionedAdapter);
并在您的适配器类中添加此构造函数
public ExampleAdapter(ExampleThread exampleThread,Context context){ mInflater = LayoutInflater.from(context); }
祝你好运。Nandakishore P
答案 2 :(得分:0)
现在ctx_invitation永远不会赋值。您可能需要一个构造函数来为ctx_invitation赋值。
答案 3 :(得分:0)
在Google上查看this question,看起来您可能正在从适配器的getView方法返回一个空视图。尝试将一些断点扔到getView方法中,并找出返回null视图的位置。
此外,您在哪里存储了BaseAdapter中对ctx_invitation的引用。
在构造函数中,您可能希望在
中传递它