私はこの問題について 3 日間検索してきました。助けてください!
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);
そして、ここに私の2番目のコードがあります:
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)