AsyncTask的结果未发布到Activity的TextView中

时间:2019-01-15 03:55:40

标签: java android android-asynctask textview

因此,我试图从REST API中检索所有帖子,这些帖子我将被输出到我的Posts活动中的textview中。我可以成功检索JSON对象并将它们存储在它们对应的ArrayList中。但是,每当我从AsyncTask的ListPosts中的Posts活动中调用onPostExecute函数时,它就说我的postsSect文本视图为空。

我认为出于某种原因,即使我在R.id的{​​{1}}中声明了onCreate也没有得到联系。因此,我在logcat中收到以下错误消息:

Posts

Posts.java

01-14 21:43:57.022 16588-16588/com.example.android.androidcraftsappprototype E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.androidcraftsappprototype, PID: 16588
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
        at android.support.v7.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:249)
        at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
        at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
        at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
        at com.example.android.androidcraftsappprototype.WSAdapter$SendPostsRequest.onPostExecute(WSAdapter.java:186)
        at com.example.android.androidcraftsappprototype.WSAdapter$SendPostsRequest.onPostExecute(WSAdapter.java:104)
        at android.os.AsyncTask.finish(AsyncTask.java:695)
        at android.os.AsyncTask.-wrap1(Unknown Source:0)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

WSAdapter.java

public class Posts extends AppCompatActivity {

    TextView postsSect;
    Button postsDoneBtn;
    WSAdapter.SendPostsRequest PostsHelper;
    StringBuilder postsBuffer = new StringBuilder();

    @Override
    protected void onResume(){
        super.onResume();
        PostsDetails postDetailsHelper = new PostsDetails();
        //postDetailsHelper.ListPosts();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_posts);

        postsDoneBtn = (Button) findViewById(R.id.PostsDoneButton);
        postsSect = (TextView) findViewById(R.id.PostsSection);

        PostsDetails postDetailsHelper = new PostsDetails();

        postDetailsHelper.callPostDetails("http://192.168.0.18:8000/api/");
        //postDetailsHelper.ListPosts();

        postsDoneBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Posts.this, MainActivity.class));

            }
        });
    }

    public class PostsDetails {
        //String post_title, post_content;
        ArrayList<Integer> post_id = new ArrayList<Integer>();
        ArrayList<String> post_title = new ArrayList<String>();
        ArrayList<String> post_content = new ArrayList<String>();

        boolean isPDCalled;



        // sets if Post details are called

        // checks if postsDetails functions are called for AsyncTask
        boolean getIsPDCalled(){
            return isPDCalled;
        }

        // calls the execute for AsyncTask
        private void callPostDetails(String theurl){
            PostsHelper = new WSAdapter().new SendPostsRequest();
            // executes AsyncTask
            PostsHelper.execute(theurl);
        }

        // sets values for the posts arrays
        public void setPost(int p_id, String p_title, String p_content) {
            this.post_id.add(p_id);
            this.post_title.add(p_title);
            this.post_content.add(p_content);
        }

        public ArrayList<Integer> getPostID() {
            return this.post_id;
        }

        public ArrayList<String> getPostTitle() {
            return this.post_title;
        }

        public ArrayList<String> getPostContent() {
            return this.post_content;
        }

        // Lists the posts from the database
        public void ListPosts() {
            /////////// add functionality if a post was deleted and was clicked

            int lastFrJSONArray = getPostID().size() - 1;

            postsSect = (TextView) findViewById(R.id.PostsSection);

            // outputs the id of the very first post, something to put to the textview
            postsSect.setText("id: " + getPostID().get(0) + "\n");
            for (int i = lastFrJSONArray; i >= 0; i--)
            {
                // appending the titles and contents of the current post
                postsSect.append("title: " + getPostTitle().get(i) + "\n");
                postsSect.append("content: " + getPostContent().get(i) + "\n");

                // if this is the last post, then don't need to append id for the next post.
                if (i != 0) {
                    postsSect.append("id: " + getPostID().get(i) + "\n");
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

首先重组代码。为了设置文本,请将要使用的字段设置为静态,然后在AsyncTask的onPostExecute中设置它们的值,否则您可以将jsonstring原样返回到您的活动,然后在其中进行解析并设置值以提高可读性。