Android - Array的问题null崩溃我的应用程序

时间:2013-10-11 02:07:23

标签: android arrays json android-asynctask

我的App中有问题。我正在检索我公司时间表上帖子的评论。当帖子有评论它就像一个魅力,但当帖子没有评论时,应用程序崩溃并给我这个错误:

10-11 02:01:06.055: E/WindowManager(931): Activity com.gettford.community.SingleItemView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41767060 that was originally added here
10-11 02:01:06.055: E/WindowManager(931): android.view.WindowLeaked: Activity com.gettford.community.SingleItemView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41767060 that was originally added here
10-11 02:01:06.055: E/WindowManager(931):   at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
10-11 02:01:06.055: E/WindowManager(931):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
10-11 02:01:06.055: E/WindowManager(931):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
10-11 02:01:06.055: E/WindowManager(931):   at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
10-11 02:01:06.055: E/WindowManager(931):   at android.view.Window$LocalWindowManager.addView(Window.java:537)
10-11 02:01:06.055: E/WindowManager(931):   at android.app.Dialog.show(Dialog.java:278)
10-11 02:01:06.055: E/WindowManager(931):   at com.gettford.community.SingleItemView$DownloadJSON.onPreExecute(SingleItemView.java:113)
10-11 02:01:06.055: E/WindowManager(931):   at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
10-11 02:01:06.055: E/WindowManager(931):   at android.os.AsyncTask.execute(AsyncTask.java:511)
10-11 02:01:06.055: E/WindowManager(931):   at com.gettford.community.SingleItemView.onCreate(SingleItemView.java:70)
10-11 02:01:06.055: E/WindowManager(931):   at android.app.Activity.performCreate(Activity.java:4465)
10-11 02:01:06.055: E/WindowManager(931):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-11 02:01:06.055: E/WindowManager(931):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-11 02:01:06.055: E/WindowManager(931):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-11 02:01:06.055: E/WindowManager(931):   at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-11 02:01:06.055: E/WindowManager(931):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-11 02:01:06.055: E/WindowManager(931):   at android.os.Handler.dispatchMessage(Handler.java:99)
10-11 02:01:06.055: E/WindowManager(931):   at android.os.Looper.loop(Looper.java:137)
10-11 02:01:06.055: E/WindowManager(931):   at android.app.ActivityThread.main(ActivityThread.java:4424)
10-11 02:01:06.055: E/WindowManager(931):   at java.lang.reflect.Method.invokeNative(Native Method)
10-11 02:01:06.055: E/WindowManager(931):   at java.lang.reflect.Method.invoke(Method.java:511)
10-11 02:01:06.055: E/WindowManager(931):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-11 02:01:06.055: E/WindowManager(931):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-11 02:01:06.055: E/WindowManager(931):   at dalvik.system.NativeStart.main(Native Method)

我差不多两周前开始编程到Android,所以我对Java很新,我的主要语言是PHP。

我希望你能帮我找到问题。

检索评论的代码是这样的,我已经尝试了“尝试”来捕获错误,但应用程序就死了。

public class DownloadJSON extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(SingleItemView.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Comentarios del post");
        // Set progressdialog message
        mProgressDialog.setMessage("Cargando...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Create an array
        arraylist2 = new ArrayList<HashMap<String, String>>();
        // Retrieve JSON Objects from the given URL address
        jsonobject1 = JSONfunctions
                .getJSONfromURL("http://server.net/file.php?userID="+usuarioID+"&idpost="+idpost+"");

        try {
            // Locate the array name in JSON
            jsonarray1 = jsonobject1.getJSONArray("comentarios");

            for (int i = 0; i < jsonarray1.length(); i++) {
                HashMap<String, String> maps = new HashMap<String, String>();
                jsonobject1 = jsonarray1.getJSONObject(i);
                // Retrive JSON Objects

                maps.put("quiencom", jsonobject1.getString("quien"));
                maps.put("imagencom", jsonobject1.getString("imagen"));
                maps.put("comentario", jsonobject1.getString("comentario"));
                maps.put("fechacom", jsonobject1.getString("fecha"));
                // Set the JSON Objects into the array
                arraylist2.add(maps);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void args) {
        // Locate the listview in listview_main.xml
        listview = (ListView) findViewById(R.id.listcoments);
        // Pass the results into ListViewAdapter.java
        adapter = new ListComentAdapter(SingleItemView.this, arraylist2);
        // Set the adapter to the ListView
        listview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
    }
}

0 个答案:

没有答案