0

以下のコードでは、インターネットから json をダウンロードし、リストに表示したいと考えています。リストが空の場合、別のアクティビティに移動しますが、他のアクティビティは開始されません。エラーはありませんが、開始アクティビティはありません。ご協力いただきありがとうございます

package ir.mohammadi.android.nightly;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class AllNotes extends ListActivity {

    ProgressDialog pDialog;

    ArrayList<HashMap<String, String>> noteList;

    JSONArray notes = null;

    private static String KEY_SUCCESS = "success";
    private static String KEY_ERROR_MSG = "error_message";
    private static String KEY_NOTE_ID = "note_id";
    private static String KEY_NOTE_SUBJECT = "note_subject";
    private static String KEY_NOTE_DATE = "note_date";

    private static String EXECUTE_RESULT = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note_list);

        noteList = new ArrayList<HashMap<String, String>>();
        new LoadAllNotes().execute();
        ListView lv = getListView();
    }

    public class LoadAllNotes extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(AllNotes.this);
            pDialog.setMessage("لطفا صبر کنید...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        protected String doInBackground(String... args) {

            UserFunctions userFunctions = new UserFunctions();

            JSONObject jSon = userFunctions.getAllNotes("1");

            Log.i("AllNotes >> jSon >>", jSon.toString());
            try {
                String success = jSon.getString(KEY_SUCCESS);
                if (success == "1") {
                    notes = jSon.getJSONArray("notes");

                    for (int i = 0; i < notes.length(); i++) {
                        JSONObject c = notes.getJSONObject(i);

                        String id = c.getString(KEY_NOTE_ID);
                        String subject = c.getString(KEY_NOTE_SUBJECT);
                        String date = c.getString(KEY_NOTE_DATE);

                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put(KEY_NOTE_ID, id);
                        map.put(KEY_NOTE_SUBJECT, subject);
                        map.put(KEY_NOTE_DATE, date);

                        noteList.add(map);
                    }

                } else {

                    finish();
                    Toast.makeText(getApplicationContext(),
                            jSon.getString(KEY_ERROR_MSG), Toast.LENGTH_SHORT).show();

                    Log.i("AllNotes >> No nightly >>", "...");

                    Intent i = new Intent(getApplicationContext(), login.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(String file_url) {
            pDialog.dismiss();

            runOnUiThread(new Runnable() {

                public void run() {
                    ListAdapter adapter = new SimpleAdapter(AllNotes.this,
                            noteList, R.layout.list_item, new String[] {
                                    KEY_NOTE_ID, KEY_NOTE_SUBJECT,
                                    KEY_NOTE_DATE }, new int[] {
                                    R.id.list_lbl_id, R.id.list_lbl_subject,
                                    R.id.list_lbl_date });
                    setListAdapter(adapter);
                }
            });
        }
    }
}

Logcat json :

11-08 22:17:44.467: I/getAllNotes params before getting from net >>(599): [tag=getNotesList, user_id=1]
11-08 22:17:47.647: I/Input stream >>(599): org.apache.http.conn.EofSensorInputStream@44ededd8
11-08 22:17:47.767: I/JSON string builder >>(599): a{"error":"1","error_message":"\u0634\u0645\u0627 \u0647\u0646\u0648\u0632 \u0634\u0628\u0627\u0646\u0647 \u0627\u06cc \u0646\u0646\u0648\u0634\u062a\u0647 \u0627\u06cc\u062f."}
11-08 22:17:47.797: I/getAllNotes params after getting from net >>(599): {"error":"1","error_message":"dont have any note"}
11-08 22:17:47.797: I/AllNotes >> jSon >>(599): {"error":"1","error_message":"dont have any note"}
4

2 に答える 2

1

問題の最も可能性の高い原因は次の行です。

String success = jSon.getString(KEY_SUCCESS);

あなたはそれを投稿しませんでしたが、その最後の行の下にJSONExceptionスタックトレースがあるに違いありません。 getString()キーが見つからず、ログアウトしたJSONデータに「成功」​​キーがない場合は、例外がスローされます。この例外により、事後に記述したすべてのコードがまったく呼び出されなくなります。そのため、何も起こらないことがわかります。

注意すべき他のいくつかのこと:

  1. success == "1"文字列の等式を行うための適切な方法ではありません。演算子はオブジェクトの==同等性(同じオブジェクト)をチェックします。2つの文字列が同じかどうかをチェックする場合は、success.equals("1")代わりに使用します。
  2. onPostExecute()常にメインスレッドで呼び出されます。メソッドから呼び出す必要はありませんrunOnUiThread()...冗長です。
  3. 技術的には、これまでと同じようにバックグラウンドスレッドからアクティビティを開始することは問題ありませんが、この方法でトーストを表示することはできません。例外を修正するToast.makeText()と、メインスレッド以外のスレッドからトーストを表示できないため、行が失敗することになります。

一般に、メインスレッドですべてのUI操作を実行するのが最善であり、設計のポイントとして、画面を操作または変更するコードを移動onPostExecute()して、適切なタイミングで呼び出すことができるようにする必要があります。それがその目的です。

于 2012-11-08T19:16:09.827 に答える
0

これはトリッキーになります。注意すべき点:

 Intent i = new Intent(getApplicationContext(), login.class);

ログイン クラスのタイプがマニフェストにあることを確認してください。通常、インスタンス化された変数ではなく、クラスの名前でクラスを参照します。正直なところ、違いはありませんが、試してみる価値はあります。

異なるコンテキストを使用すると、奇妙な副作用が発生することがあります。startActivity は、これにかなり敏感なものだと思います。AllNotes.this を試して、実行中のアクティビティのコンテキストを取得できます。これを UI スレッドで実行する必要がある可能性はほとんどないため、うまくいかない場合は runOnUiThread() を試すことができます。

于 2012-11-08T19:11:42.610 に答える