0

我在我的活动中膨胀了这样的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal" >

    <FrameLayout
        android:id="@+id/category_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <FrameLayout
        android:id="@+id/sub_category_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <FrameLayout
        android:id="@+id/sub_sub_category_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

这是我的活动AsyncTask。问题是 - 在执行 Asynctask 之后,活动布局没有更新。它保持空白。

package me.kaidul.uhunt;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import me.kaidul.uhunt.ChaptersListFragment.OnChapterSelectListener;
import me.kaidul.uhunt.SubChaptersListFragment.OnSubChapterSelectListener;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.google.gson.stream.JsonReader;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class CompetitiveProgrammingActivity extends SherlockFragmentActivity
        implements OnChapterSelectListener, OnSubChapterSelectListener {

    public static List<Chapter> chapterList = new ArrayList<Chapter>();
    private ProcessTask processTask = null;
    Fragment chapterFragment = new ChaptersListFragment();
    Fragment subChapterFragment = new SubChaptersListFragment();
    Fragment subSubChapterFragment = new SubSubChaptersListFragment();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        processTask = new ProcessTask(this);
        processTask.execute();
    }

    protected class ProcessTask extends AsyncTask<Void, Void, Void> {

        private CompetitiveProgrammingActivity activity;


        public ProcessTask(CompetitiveProgrammingActivity activity) {
            this.activity = activity;
        }

        @Override
        protected Void doInBackground(Void... params) {
            InputStream inputStream = null;
            try {
                inputStream = getAssets().open(
                        CommonUtils.FILE_COMPETITIVE_PROGRAMMING_3);

                JsonReader reader = new JsonReader(new InputStreamReader(
                        inputStream));

                reader.beginArray(); // array #1
                while (reader.hasNext()) {
                    String chapterTitle = null;
                    List<SubChapter> subList = new ArrayList<SubChapter>();
                    reader.beginObject(); // object #2
                    while (reader.hasNext()) {
                        reader.skipValue();
                        chapterTitle = reader.nextString();
                        reader.skipValue();
                        reader.beginArray(); // array #3
                        while (reader.hasNext()) {
                            String subChapterTitle = null;
                            List<SubSubChapter> subSubList = new ArrayList<SubSubChapter>();
                            reader.beginObject(); // object #4
                            while (reader.hasNext()) {
                                reader.skipValue();
                                subChapterTitle = reader.nextString();
                                reader.skipValue();
                                reader.beginArray(); // array #5
                                while (reader.hasNext()) {
                                    reader.beginArray(); // array #6
                                    String subSubChapterTitle = reader
                                            .nextString(); // sub-sub-category
                                                            // title
                                    List<ProblemList> problemsList = new ArrayList<ProblemList>();
                                    while (reader.hasNext()) {
                                        int signedProblemID = reader.nextInt(); // problemNo
                                        String title = reader.nextString();
                                        if (signedProblemID < 0)
                                            problemsList.add(new ProblemList(
                                                    Math.abs(signedProblemID),
                                                    title, true));
                                        else
                                            problemsList.add(new ProblemList(
                                                    signedProblemID, title,
                                                    false));
                                    }
                                    reader.endArray(); // array #6
                                    subSubList.add(new SubSubChapter(
                                            subSubChapterTitle, problemsList));
                                }
                                reader.endArray(); // array #5
                            }
                            reader.endObject(); // object #4
                            subList.add(new SubChapter(subChapterTitle,
                                    subSubList));
                        }
                        reader.endArray(); // array #3
                    }
                    reader.endObject(); // object #2
                    chapterList.add(new Chapter(chapterTitle, subList));
                }
                reader.endArray(); // array #1
                reader.close();

            } catch (IOException e) {
                // nothing
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        // nothing
                    }
                }
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            activity.setContentView(R.layout.competitive_programming_exercise);
            FragmentTransaction transaction = activity.getSupportFragmentManager()
                    .beginTransaction();
            if (activity.findViewById(R.id.fragment_container) != null) {
                transaction.replace(R.id.fragment_container,
                        chapterFragment);
            } else {
                // these fragments are not upated
                transaction.replace(R.id.category_fragment, chapterFragment);
                transaction.replace(R.id.sub_category_fragment,
                        subChapterFragment);
                transaction.replace(R.id.sub_sub_category_fragment,
                        subSubChapterFragment);
            }
            transaction.commit();
        }

    }

    static protected class Chapter {
        String chapterTitle;
        List<SubChapter> subchapterList;

        public Chapter(String chapterTitle, List<SubChapter> subchapterList) {
            this.chapterTitle = chapterTitle;
            this.subchapterList = subchapterList;
        }

    }

    static protected class SubChapter {
        String subChapterTitle;
        List<SubSubChapter> subsubchapterList;

        public SubChapter(String subChapterTitle,
                List<SubSubChapter> subsubchapterList) {
            this.subChapterTitle = subChapterTitle;
            this.subsubchapterList = subsubchapterList;
        }

    }

    static protected class SubSubChapter {
        String subSubChapterTitle;
        List<ProblemList> problemList;

        public SubSubChapter(String subSubChapterTitle,
                List<ProblemList> problemList) {
            this.subSubChapterTitle = subSubChapterTitle;
            this.problemList = problemList;
        }

    }

    static public class ProblemList {
        Integer problemNo;
        String problemTitle;
        boolean isStarred;

        public ProblemList(Integer problemNo, String problemTitle,
                boolean isStarred) {
            this.problemNo = problemNo;
            this.isStarred = isStarred;
            this.problemTitle = problemTitle;
        }

    }

    @Override
    public void onChapterSelected(int position) {
        SubChaptersListFragment subChaptersListFrag = (SubChaptersListFragment) getSupportFragmentManager()
                .findFragmentById(R.id.sub_category_fragment);
        if (subChaptersListFrag != null) {
            subChaptersListFrag.updateList(position);
        } else {
            subChapterFragment = new SubChaptersListFragment();
            Bundle args = new Bundle();
            args.putInt(SubChaptersListFragment.CHAPTER_POSITION, position);
            subChapterFragment.setArguments(args);
            FragmentTransaction transaction = getSupportFragmentManager()
                    .beginTransaction();
            transaction.replace(R.id.fragment_container, subChapterFragment);
            transaction.commit();
        }
    }

    @Override
    public void onSubChapterSelected(int prev, int position) {
        SubSubChaptersListFragment subSubChaptersListFrag = (SubSubChaptersListFragment) getSupportFragmentManager()
                .findFragmentById(R.id.sub_sub_category_fragment);
        if (subSubChaptersListFrag != null) {
            subSubChaptersListFrag.updateList(prev, position);
        } else {
            subSubChapterFragment = new SubSubChaptersListFragment();
            Bundle args = new Bundle();
            args.putIntArray(SubSubChaptersListFragment.POSITIONS, new int[] {
                    prev, position });
            subSubChapterFragment.setArguments(args);
            FragmentTransaction transaction = getSupportFragmentManager()
                    .beginTransaction();
            transaction.replace(R.id.fragment_container, subSubChapterFragment);
            transaction.commit();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (processTask != null
                && processTask.getStatus() != AsyncTask.Status.FINISHED) {
            processTask.cancel(true);
        }
    }

}
4

1 回答 1

1

我犯了一个错误。我有两个班级CompetitiveProgrammingCompetitiveProgrammingActivity. 我误用并混合了它们,这就是错误发生的原因。

于 2013-08-22T00:22:36.957 回答