隐藏EditText&使它在AsyncTask中可见

时间:2014-04-23 15:57:11

标签: android android-edittext android-button

我正在创建一个Android应用程序,它将id_user和id_status发送到带有AsyncTask的远程数据库。如果成功== 1,则EditText和按钮将自动显示。

CommentActivity

package com.portalalumni;

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

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CommentActivity extends Activity {

SessionManager session;
boolean isVisible;

    // Progress Dialog
        private ProgressDialog pDialog;

        // Creating JSON Parser object 
        JSONParser jParser = new JSONParser();

        // url  
        private static String url_cek_pendidikan = "http://192.168.43.13/alumni/cek_pendidikan.php";

        // JSON Node names
        public static final String TAG_SUCCESS = "success"; 

        String id_user,email,id_status;
        private EditText comment;
        Button btnAddComment;

        private final Handler handler = new Handler();

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

        session = new SessionManager(getApplicationContext());
        Toast.makeText(getApplicationContext(),
                "User Login Status: " + session.isLoggedIn(), Toast.LENGTH_LONG)
                .show();

        session.checkLogin();

        //Hashmap from login
        HashMap<String, String> user = session.getUserDetails();
        id_user = user.get(SessionManager.KEY_IDUSER);

        Bundle extras = getIntent().getExtras();
        if(extras!=null){
            id_status = extras.getString("id_status");
        }

        // Loading status and comment in Background Thread
        new LoadCekPenddkActivity().execute();

        comment = (EditText) findViewById(R.id.comment);

        btnAddComment = (Button) findViewById(R.id.dm_send);

        // button click event
        btnAddComment.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // creating comment in background thread
                new LoadUpdateActivity().execute();

                //comment.getText().clear();
            }
        });

        // Show the Up button in the action bar.
        getActionBar().setDisplayHomeAsUpEnabled(true);

    }

    public void Set_Visibility_Visible(){
        this.isVisible = true;
    }

    class LoadCekPenddkActivity extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            /*pDialog = new ProgressDialog(CommentActivity.this);
            pDialog.setMessage("Loading..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();*/
        }

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) { 
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("id_user", id_user));
            params.add(new BasicNameValuePair("id_status", id_status));

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jParser.makeHttpRequest(url_cek_pendidikan,"GET", params);

            // check log cat fro response
            Log.d("Update", json.toString());

            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    comment.setVisibility(View.VISIBLE);
                    btnAddComment.setVisibility(View.VISIBLE);

                } else {
                    comment.setVisibility(View.GONE);
                    btnAddComment.setVisibility(View.GONE);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            //pDialog.dismiss();
        }

    }

}

添加计时器任务和计时器后,我运行代码。应用程序崩溃了,我收到了以下logcat错误:

04-23 22:15:34.359: E/AndroidRuntime(1050): FATAL EXCEPTION: AsyncTask #4
04-23 22:15:34.359: E/AndroidRuntime(1050): java.lang.RuntimeException: An error occured while executing doInBackground()
04-23 22:15:34.359: E/AndroidRuntime(1050):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at java.lang.Thread.run(Thread.java:856)
04-23 22:15:34.359: E/AndroidRuntime(1050): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
04-23 22:15:34.359: E/AndroidRuntime(1050):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:3939)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:701)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at android.view.View.requestLayout(View.java:12555)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at android.view.View.requestLayout(View.java:12555)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at android.view.View.requestLayout(View.java:12555)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at android.view.View.requestLayout(View.java:12555)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at android.support.v4.widget.DrawerLayout.requestLayout(DrawerLayout.java:762)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at android.view.View.requestLayout(View.java:12555)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:268)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at android.view.View.requestLayout(View.java:12555)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at android.view.View.setFlags(View.java:6659)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at android.view.View.setVisibility(View.java:4564)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at com.portalalumni.CommentActivity$LoadCekPenddkActivity.doInBackground(CommentActivity.java:521)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at com.portalalumni.CommentActivity$LoadCekPenddkActivity.doInBackground(CommentActivity.java:1)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
04-23 22:15:34.359: E/AndroidRuntime(1050):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-23 22:15:34.359: E/AndroidRuntime(1050):     ... 5 more
04-23 22:15:35.999: E/WindowManager(1050): Activity com.portalalumni.CommentActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41439380 that was originally added here
04-23 22:15:35.999: E/WindowManager(1050): android.view.WindowLeaked: Activity com.portalalumni.CommentActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41439380 that was originally added here
04-23 22:15:35.999: E/WindowManager(1050):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:343)
04-23 22:15:35.999: E/WindowManager(1050):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:245)
04-23 22:15:35.999: E/WindowManager(1050):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
04-23 22:15:35.999: E/WindowManager(1050):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
04-23 22:15:35.999: E/WindowManager(1050):  at android.view.Window$LocalWindowManager.addView(Window.java:537)
04-23 22:15:35.999: E/WindowManager(1050):  at android.app.Dialog.show(Dialog.java:274)
04-23 22:15:35.999: E/WindowManager(1050):  at com.portalalumni.CommentActivity$LoadCekPenddkActivity.onPreExecute(CommentActivity.java:497)
04-23 22:15:35.999: E/WindowManager(1050):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
04-23 22:15:35.999: E/WindowManager(1050):  at android.os.AsyncTask.execute(AsyncTask.java:511)
04-23 22:15:35.999: E/WindowManager(1050):  at com.portalalumni.CommentActivity.onCreate(CommentActivity.java:105)
04-23 22:15:35.999: E/WindowManager(1050):  at android.app.Activity.performCreate(Activity.java:4465)
04-23 22:15:35.999: E/WindowManager(1050):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-23 22:15:35.999: E/WindowManager(1050):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
04-23 22:15:35.999: E/WindowManager(1050):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
04-23 22:15:35.999: E/WindowManager(1050):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
04-23 22:15:35.999: E/WindowManager(1050):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
04-23 22:15:35.999: E/WindowManager(1050):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 22:15:35.999: E/WindowManager(1050):  at android.os.Looper.loop(Looper.java:137)
04-23 22:15:35.999: E/WindowManager(1050):  at android.app.ActivityThread.main(ActivityThread.java:4340)
04-23 22:15:35.999: E/WindowManager(1050):  at java.lang.reflect.Method.invokeNative(Native Method)
04-23 22:15:35.999: E/WindowManager(1050):  at java.lang.reflect.Method.invoke(Method.java:511)
04-23 22:15:35.999: E/WindowManager(1050):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-23 22:15:35.999: E/WindowManager(1050):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-23 22:15:35.999: E/WindowManager(1050):  at dalvik.system.NativeStart.main(Native Method)
04-23 22:15:36.149: E/WindowManager(1050): Activity com.portalalumni.CommentActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4145bd68 that was originally added here
04-23 22:15:36.149: E/WindowManager(1050): android.view.WindowLeaked: Activity com.portalalumni.CommentActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4145bd68 that was originally added here
04-23 22:15:36.149: E/WindowManager(1050):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:343)
04-23 22:15:36.149: E/WindowManager(1050):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:245)
04-23 22:15:36.149: E/WindowManager(1050):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
04-23 22:15:36.149: E/WindowManager(1050):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
04-23 22:15:36.149: E/WindowManager(1050):  at android.view.Window$LocalWindowManager.addView(Window.java:537)
04-23 22:15:36.149: E/WindowManager(1050):  at android.app.Dialog.show(Dialog.java:274)
04-23 22:15:36.149: E/WindowManager(1050):  at com.portalalumni.CommentActivity$LoadStatusActivity.onPreExecute(CommentActivity.java:228)
04-23 22:15:36.149: E/WindowManager(1050):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
04-23 22:15:36.149: E/WindowManager(1050):  at android.os.AsyncTask.execute(AsyncTask.java:511)
04-23 22:15:36.149: E/WindowManager(1050):  at com.portalalumni.CommentActivity.onCreate(CommentActivity.java:106)
04-23 22:15:36.149: E/WindowManager(1050):  at android.app.Activity.performCreate(Activity.java:4465)
04-23 22:15:36.149: E/WindowManager(1050):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-23 22:15:36.149: E/WindowManager(1050):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
04-23 22:15:36.149: E/WindowManager(1050):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
04-23 22:15:36.149: E/WindowManager(1050):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
04-23 22:15:36.149: E/WindowManager(1050):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
04-23 22:15:36.149: E/WindowManager(1050):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 22:15:36.149: E/WindowManager(1050):  at android.os.Looper.loop(Looper.java:137)
04-23 22:15:36.149: E/WindowManager(1050):  at android.app.ActivityThread.main(ActivityThread.java:4340)
04-23 22:15:36.149: E/WindowManager(1050):  at java.lang.reflect.Method.invokeNative(Native Method)
04-23 22:15:36.149: E/WindowManager(1050):  at java.lang.reflect.Method.invoke(Method.java:511)
04-23 22:15:36.149: E/WindowManager(1050):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-23 22:15:36.149: E/WindowManager(1050):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-23 22:15:36.149: E/WindowManager(1050):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

doInBackground()在一个单独的帖子中运行,因此您无法触及其中View内的ActivityCalledFromWrongThreadException,这就是您看到的原因AsyncTask。相反,您可以在doInBackground()但<{1}}的任何方法中调用此方法。

由于您似乎正在测试某些内容的结果,因此我建议您使用onPostExecute()方法进行设置,因为一旦您退出doInBackground()方法,这将是下一个调用。

答案 1 :(得分:0)

您收到错误是因为您尝试使用doInBackground()方法更新UI。将更新UI部件移至onPostExecute()方法,如下所示,

             if (success == 1) {
                    comment.setVisibility(View.VISIBLE);
                    btnAddComment.setVisibility(View.VISIBLE);

                } else {
                    comment.setVisibility(View.GONE);
                    btnAddComment.setVisibility(View.GONE);