Android:自定义ArrayList不起作用

时间:2013-01-31 18:45:23

标签: android listview

我三天以来一直在搜索这个问题。请帮助!

我正在尝试在AsynTask中制作自定义的ArrayList,但不关心,因为它不能在外面工作。

首先我使用了setListAdapter,但是我的类未定义,甚至我的类扩展了ListActivity。然后,我尝试使用ListView setAdapter,但它总是返回nullpointerexception。

这是我的第一个代码:

package Comments;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.Toast;
import com.example.helloandroid.R;
import com.example.podcastq.ListActivity;

public class PrepareComments extends ListActivity {

static String titulo;
static String parentid;
static Context contexto;
static ArrayList<String> resultado;
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.buscar_listview);

}

public PrepareComments(Context contexto) {
    super();
    PrepareComments.contexto=contexto;
}


public void SetParams(String titulo, String parentid) {
    PrepareComments.titulo = titulo;
    PrepareComments.parentid = parentid;
}


public void ShowComments() {

    new AsyncTaskSearchName().execute(titulo, parentid);
}

private class AsyncTaskSearchName
extends
AsyncTask<String, String, SubmitResponseResult> {

    ProgressDialog dialog;


    @Override
    protected void onPreExecute() {
        try {
            dialog = new ProgressDialog(PrepareComments.contexto);
            dialog.setTitle(R.string.cargando);
            dialog.setMessage(PrepareComments.contexto.getString(R.string.obtInfoServer));
            dialog.setCancelable(false);
            dialog.show();
        } catch (final Exception e) {
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(PrepareComments.contexto, e.getMessage(),Toast.LENGTH_SHORT).show();
                    Log.e("ERROR", e.getStackTrace().toString());
                }
            });
        }
    }

    @Override
    protected SubmitResponseResult doInBackground(String... strings) {

        JoomlaServices oc = new JoomlaServices();

        try {
            SubmitResponseResult sr = oc.getComments(PrepareComments.titulo);
            PrepareComments.resultado = sr.getResult();

            PrepareComments.parentid = sr.getId();

        }  catch (final Exception e) {
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(PrepareComments.contexto,PrepareComments.contexto.getString(R.string.connectionError), Toast.LENGTH_SHORT).show();
                    Log.e("ERROR", e.getCause().toString());

                }
            });
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(String... responses) {

        runOnUiThread(new Runnable() {
            public void run() {
                try {
                    ListView lv = (ListView) findViewById(android.R.id.list); //NULLPOINTEREXCEPTION IN THIS LINE!!

                    ListViewSuggestionAdapter lvSuggestionAdapt = new ListViewSuggestionAdapter(
                            PrepareComments.contexto, R.layout.listview_item_left, PrepareComments.resultado);  

                    lv.setAdapter(lvSuggestionAdapt);
                } catch (Exception e) {
                    Log.i("MI_ERROR", e.getMessage());
                }
            }
        });
    }

    @Override
    protected void onPostExecute(final SubmitResponseResult result) {

        runOnUiThread(new Runnable() {
            public void run() {
                try {
                    dialog.dismiss();
                } catch (Exception e) {
                    Log.i("MI_ERROR", e.getMessage());
                }

            }
        });
    }
}

}

有这方面的问题:

ListView lv = (ListView) findViewById(android.R.id.list); //NULLPOINTEREXCEPTION IN THIS LINE!!

                    ListViewSuggestionAdapter lvSuggestionAdapt = new ListViewSuggestionAdapter(
                            PrepareComments.contexto, R.layout.listview_item_left, PrepareComments.resultado);  

                    lv.setAdapter(lvSuggestionAdapt);

这是我的第二个代码:

package Comments;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.Toast;
import com.example.helloandroid.R;
import com.example.podcastq.ListActivity;

public class PrepareComments extends ListActivity {

static String titulo;
static String parentid;
static Context contexto;
static ArrayList<String> resultado;
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.buscar_listview);

}

public PrepareComments(Context contexto) {
    super();
    PrepareComments.contexto=contexto;
}


public void SetParams(String titulo, String parentid) {
    PrepareComments.titulo = titulo;
    PrepareComments.parentid = parentid;
}


public void ShowComments() {

    new AsyncTaskSearchName().execute(titulo, parentid);
}

private class AsyncTaskSearchName
extends
AsyncTask<String, String, SubmitResponseResult> {

    ProgressDialog dialog;


    @Override
    protected void onPreExecute() {
        try {
            dialog = new ProgressDialog(PrepareComments.contexto);
            dialog.setTitle(R.string.cargando);
            dialog.setMessage(PrepareComments.contexto.getString(R.string.obtInfoServer));
            dialog.setCancelable(false);
            dialog.show();
        } catch (final Exception e) {
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(PrepareComments.contexto, e.getMessage(),Toast.LENGTH_SHORT).show();
                    Log.e("ERROR", e.getStackTrace().toString());
                }
            });
        }
    }

    @Override
    protected SubmitResponseResult doInBackground(String... strings) {

        JoomlaServices oc = new JoomlaServices();

        try {
            SubmitResponseResult sr = oc.getComments(PrepareComments.titulo);
            PrepareComments.resultado = sr.getResult();

            PrepareComments.parentid = sr.getId();


        }  catch (final Exception e) {
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(PrepareComments.contexto,PrepareComments.contexto.getString(R.string.connectionError), Toast.LENGTH_SHORT).show();
                    Log.e("ERROR", e.getCause().toString());

                }
            });
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(String... responses) {

        runOnUiThread(new Runnable() {
            public void run() {
                try {
                                       ListViewSuggestionAdapter lvSuggestionAdapt = new ListViewSuggestionAdapter(
                            PrepareComments.contexto, R.layout.listview_item_left, PrepareComments.resultado);  

                                            PrepareComments.setListAdapter(lvSuggestionAdapt); //UNDEFINED FUNCTION FOR PREPARECOMMENTS IN THIS LINE

                } catch (Exception e) {
                    Log.i("MI_ERROR", e.getMessage());
                }
            }
        });
    }

    @Override
    protected void onPostExecute(final SubmitResponseResult result) {

        runOnUiThread(new Runnable() {
            public void run() {
                try {
                    dialog.dismiss();
                } catch (Exception e) {
                    Log.i("MI_ERROR", e.getMessage());
                }

            }
        });
    }
}

}

有这方面的问题:

ListViewSuggestionAdapter lvSuggestionAdapt = new ListViewSuggestionAdapter(
                            PrepareComments.contexto, R.layout.listview_item_left, PrepareComments.resultado);  

                                                PrepareComments.setListAdapter(lvSuggestionAdapt); //UNDEFINED FUNCTION FOR PREPARECOMMENTS IN THIS LINE

我对ListView的xml定义是:

<ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:clickable="false"
            android:longClickable="false" >
        </ListView>

谢谢!

已编辑:LogCat

01-31 22:10:08.995: E/AndroidRuntime(283): FATAL EXCEPTION: main
01-31 22:10:08.995: E/AndroidRuntime(283): java.lang.NullPointerException
01-31 22:10:08.995: E/AndroidRuntime(283):  at Comments.PrepareComments$AsyncTaskSearchName$3.run(PrepareComments.java:141)
01-31 22:10:08.995: E/AndroidRuntime(283):  at android.os.Handler.handleCallback(Handler.java:587)
01-31 22:10:08.995: E/AndroidRuntime(283):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-31 22:10:08.995: E/AndroidRuntime(283):  at android.os.Looper.loop(Looper.java:123)
01-31 22:10:08.995: E/AndroidRuntime(283):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-31 22:10:08.995: E/AndroidRuntime(283):  at java.lang.reflect.Method.invokeNative(Native Method)
01-31 22:10:08.995: E/AndroidRuntime(283):  at java.lang.reflect.Method.invoke(Method.java:521)
01-31 22:10:08.995: E/AndroidRuntime(283):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-31 22:10:08.995: E/AndroidRuntime(283):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-31 22:10:08.995: E/AndroidRuntime(283):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

使用

ListView lv = (ListView)PrepareComments.this.findViewById(android.R.id.list);

而不是

ListView lv = (ListView) findViewById(android.R.id.list);

使用onProgressUpdate中的ListView初始化lv

注意:无需在runOnUiThreadonPreExecute内使用onPostExecute,因为这些方法在UI线程上调用,然后我们可以访问或更新UI元素而不使用runOnUiThread