Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从活动类启动片段事务时Dialog.show()上的活动泄漏窗口异常_Java_Android_Android Fragments_Android Dialog - Fatal编程技术网

Java 从活动类启动片段事务时Dialog.show()上的活动泄漏窗口异常

Java 从活动类启动片段事务时Dialog.show()上的活动泄漏窗口异常,java,android,android-fragments,android-dialog,Java,Android,Android Fragments,Android Dialog,当我单击CreateWorkout按钮时,它将转到FragmentTransaction中指定的fragment UserWorkoutPlanFragment类。但当交易开始时,我得到了一个错误。 我得到这个错误: 05-22 19:23:35.873: E/WindowManager(1042): Activity com.fitserv.user.profilemenu.NewWorkout has leaked window com.android.internal.policy.imp

当我单击CreateWorkout按钮时,它将转到FragmentTransaction中指定的fragment UserWorkoutPlanFragment类。但当交易开始时,我得到了一个错误。 我得到这个错误:

05-22 19:23:35.873: E/WindowManager(1042): Activity com.fitserv.user.profilemenu.NewWorkout has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4170dd80 V.E..... R.....ID 0,0-308,96} that was originally added here
05-22 19:23:35.873: E/WindowManager(1042): android.view.WindowLeaked: Activity com.fitserv.user.profilemenu.NewWorkout has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4170dd80 V.E..... R.....ID 0,0-308,96} that was originally added here
05-22 19:23:35.873: E/WindowManager(1042):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
05-22 19:23:35.873: E/WindowManager(1042):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
05-22 19:23:35.873: E/WindowManager(1042):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
05-22 19:23:35.873: E/WindowManager(1042):  at android.app.Dialog.show(Dialog.java:281)
05-22 19:23:35.873: E/WindowManager(1042):  at com.fitserv.user.profilemenu.NewWorkout$NewWorkoutPlan.onPreExecute(NewWorkout.java:90)
05-22 19:23:35.873: E/WindowManager(1042):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
05-22 19:23:35.873: E/WindowManager(1042):  at android.os.AsyncTask.execute(AsyncTask.java:534)
05-22 19:23:35.873: E/WindowManager(1042):  at com.fitserv.user.profilemenu.NewWorkout$1.onClick(NewWorkout.java:70)
05-22 19:23:35.873: E/WindowManager(1042):  at android.view.View.performClick(View.java:4204)
05-22 19:23:35.873: E/WindowManager(1042):  at android.view.View$PerformClick.run(View.java:17355)
05-22 19:23:35.873: E/WindowManager(1042):  at android.os.Handler.handleCallback(Handler.java:725)
05-22 19:23:35.873: E/WindowManager(1042):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-22 19:23:35.873: E/WindowManager(1042):  at android.os.Looper.loop(Looper.java:137)
05-22 19:23:35.873: E/WindowManager(1042):  at android.app.ActivityThread.main(ActivityThread.java:5041)
05-22 19:23:35.873: E/WindowManager(1042):  at java.lang.reflect.Method.invokeNative(Native Method)
05-22 19:23:35.873: E/WindowManager(1042):  at java.lang.reflect.Method.invoke(Method.java:511)
05-22 19:23:35.873: E/WindowManager(1042):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-22 19:23:35.873: E/WindowManager(1042):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-22 19:23:35.873: E/WindowManager(1042):  at dalvik.system.NativeStart.main(Native Method)
这是我的代码:

public class NewWorkout extends Activity {

    // Progress Dialog
    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();
    EditText inputWorkoutName;
    EditText inputWorkoutDate;
    EditText inputExceriseName;
    EditText inputSets;
    EditText inputKg;
    EditText inputReps;
    EditText inputNotes;


    // url to create new product
    private static String url_create_workout = "http://ec2-54-77-51-119.eu-west-1.compute.amazonaws.com/android_connect/create_workout.php";

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

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

        // Edit Text
        inputWorkoutName = (EditText) findViewById(R.id.inputWorkoutName);
        inputWorkoutDate = (EditText) findViewById(R.id.inputWorkoutDate);
        inputExceriseName = (EditText) findViewById(R.id.inputExceriseName);
        inputSets = (EditText) findViewById(R.id.inputSets);
        inputKg = (EditText) findViewById(R.id.inputKg);
        inputReps = (EditText) findViewById(R.id.inputReps);
        inputNotes = (EditText) findViewById(R.id.inputNotes);

        // Create button
        Button btnCreateWorkout = (Button) findViewById(R.id.btnCreateWorkout);
        // button click event
        btnCreateWorkout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // creating new product in background thread
                new NewWorkoutPlan().execute();
            }
        });
    }

    /**
     * Background Async Task to Create new product
     * */
    class NewWorkoutPlan extends AsyncTask<String, String, String> {

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

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {
            String workout_name = inputWorkoutName.getText().toString();
            String workout_date = inputWorkoutDate.getText().toString();
            String exercise_name = inputExceriseName.getText().toString();
            String sets = inputSets.getText().toString();
            String weight_kg = inputKg.getText().toString();
            String reps = inputReps.getText().toString();
            String notes = inputNotes.getText().toString();

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("workout_name", workout_name));
            params.add(new BasicNameValuePair("workout_date", workout_date));
            params.add(new BasicNameValuePair("exercise_name", exercise_name));
            params.add(new BasicNameValuePair("sets", sets));
            params.add(new BasicNameValuePair("weight_kg", weight_kg));
            params.add(new BasicNameValuePair("reps", reps));
            params.add(new BasicNameValuePair("notes", notes));

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

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

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

                if (success == 1) {
                    /**
                    // successfully created product
                    Intent i = new Intent(getApplicationContext(), UserProfileActivity.class);
                    startActivity(i);
                    //closing this screen
                    finish();
                    **/

                    Fragment newFragment = new UserWorkoutPlanFragment();
                      FragmentTransaction transaction = getFragmentManager().beginTransaction();
                       transaction.replace(R.id.frame_container, newFragment);
                        transaction.commit();

                    UserWorkoutPlanFragment fd = new UserWorkoutPlanFragment();
                      FragmentTransaction  ft = getFragmentManager().beginTransaction();
                                  ft.replace(R.id.frame_container, fd); 

                                 // content_frame is your FrameLayout container
                                  ft.addToBackStack(null);
                                  ft.commit();  

                } else {
                    // failed to create product
                }
            } 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();

        }

    }
}
有人能帮我吗


谢谢

pDialog导致了车窗泄漏。发生这种情况是因为您正在doInBackground中执行片段事务,并在postExecute上调用dismise方法


您应该在调用Disclease后在onPostExecute中发布事务代码。我知道您是Android新手,但可能您已经用其他语言开发了,不是吗?无论如何,您应该使用设计模式进行编码,并在它自己的特定类中执行您想要执行的操作。这样做,您将避免许多类似这样的错误。 看看这个关于设计模式的网站:

您需要在UI线程中执行FragmentTransaction。可以通过调用runOnUiThread从doInBackground方法中执行此操作:


在logcat错误中,存在:

在 com.fitserv.user.profilemenu.NewWorkout$NewWorkoutPlan.onPreExecuteNewWorkout.java:90 05-22 19:23:35.873:E/WindowManager 1042:at android.os.AsyncTask.ExecuteOnExecuteAsyncTask.java:586

所以问题在于onPreExecute方法。我以前没有使用ProgressDialog,但问题在于上下文参数。在这种情况下,新的锻炼。这个。这不是正确的上下文

守则建议:

pDialog=新建ProgressDialog getApplicationContext; pDialog=新建ProgressDialog getActivity; pDialog=new ProgressDialog main activity.this; 注意:对于MainActivity,如果声明为静态,则可以使用此代码。 我认为,主要问题是背景


仅供参考:我发现有趣的是,发布的答案有些不同,但有些答案集中在对话框上。

我该如何做呢?我发现您依赖的是一个整数变量“成功”。您可以像asynctask一样声明asynctask,然后在doInBackGround中返回onPostExecute将自动接收的'success'变量。然后你可以检查这个值并调用事务代码。我不再收到这个错误了,但是我现在收到一个找不到id错误的视图这是问题的链接,请帮我看看。这里是问题的答案现在我得到了一个错误,没有找到id为0x7f0900a3 com.fitserv.androidapp:id/frame_容器,用于fragment UserWorkoutPlanFragment{40d0b748 0 id=0x7f0900a3},您知道这可能是错误的原因吗case@user3661128这是一个无关的错误。请问另一个问题。是的,这里是@user3661128,如果发布的答案对你有帮助,只需单击作为最佳答案或投票作为唯一的帮助。我想你确实做了上面建议的代码更改。我现在得到了一个id错误的无视图。这是问题的链接,请帮我看看。下面是问题的答案
runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Fragment newFragment = new UserWorkoutPlanFragment();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_container, newFragment);
        transaction.commit();

        UserWorkoutPlanFragment fd = new UserWorkoutPlanFragment();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.frame_container, fd);

        // content_frame is your FrameLayout container
        ft.addToBackStack(null);
        ft.commit();

    }
});