我尝试从fragmentActivity开始一个新的片段,但是每次尝试时,都会出现一个错误,告诉我:活动已被破坏。

我创建的tabController的源代码:

package com.crosscommunications.kvodeventer;

import java.io.File;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

public class TabControllerMeldingen extends FragmentActivity {

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

        final File f = new File("/data/data/com.crosscommunications.kvodeventer/files/data.txt");

        if (f.exists()) {
            System.out.println("File existed");
            addFragment(new KVOCards(), false, FragmentTransaction.TRANSIT_NONE);

        } else {
            System.out.println("File not found!");
            addFragment(new KVOMeldingen(), false, FragmentTransaction.TRANSIT_NONE);
        }

    }

    public void addFragment(Fragment fragment, boolean addToBackStack, int transition) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.simple_fragment, fragment);
        ft.setTransition(transition);
        if (addToBackStack)
            ft.addToBackStack(null);
        ft.commit();
    }

    public void finishFragmentOrActivity(View v) {
        FragmentManager manager = getSupportFragmentManager();
        if (manager.getBackStackEntryCount() > 0)
            getSupportFragmentManager().popBackStack();
        else
            finish();
    }

    public void launchNewFragment(View v) {
        addFragment(new KVOOver(), true, FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    }
}


在大多数情况下,此代码将打开KVOMeldingen类。因此,KVOMeldingen的来源是:

package com.crosscommunications.kvodeventer;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class KVOMeldingen extends Fragment {

    static String Username;
    static String Password;
    static String LoginResult;
    static String LoginOk = "vet";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View fragmentView = inflater.inflate(R.layout.kvomeldingen, container, false);
        final EditText etUsername = (EditText) fragmentView.findViewById(R.id.etUsername);
        final EditText etPassword = (EditText) fragmentView.findViewById(R.id.etPassword);
        Button bLogin = (Button) fragmentView.findViewById(R.id.bLogin);
        Button bCreateAccount = (Button) fragmentView.findViewById(R.id.bCreateAccount);
        Button bResetPassword = (Button) fragmentView.findViewById(R.id.bResetPassword);

        bLogin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (etUsername.length() <= 0) {
                    etUsername.setError("Veld mag niet leeg zijn");
                } else if (etPassword.length() <= 0) {
                    etPassword.setError("Veld mag niet leeg zijn");
                } else {
                    Username = etUsername.getText().toString();
                    Password = etPassword.getText().toString();
                }
                LoginTask NDLT = new LoginTask();
                NDLT.execute();
            }
        });

        bCreateAccount.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (etUsername.length() <= 0) {
                    etUsername.setError("Veld mag niet leeg zijn");
                } else if (etPassword.length() <= 0) {
                    etPassword.setError("Veld mag niet leeg zijn");
                } else {
                    Username = etUsername.getText().toString();
                    Password = etPassword.getText().toString();
                }
                RegisterTask RegTask = new RegisterTask();
                RegTask.execute();
            }

        });

        bResetPassword.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (etUsername.length() <= 0) {
                    etUsername.setError("Veld mag niet leeg zijn");
                } else if (etPassword.length() <= 0) {
                    etPassword.setError("Veld mag niet leeg zijn");
                } else {
                    Username = etUsername.getText().toString();
                    Password = etPassword.getText().toString();
                }
                ForgotTask forgTask = new ForgotTask();
                forgTask.execute();

            }

        });

        return fragmentView;
    }

    class LoginTask extends AsyncTask<Void, Void, JSONObject> {

        ProgressDialog waitingDialog;

        @Override
        protected void onPreExecute() {
            waitingDialog = new ProgressDialog(getActivity());
            waitingDialog.setMessage("Laden...");
            waitingDialog.show();
            super.onPreExecute();
        }

        @Override
        protected JSONObject doInBackground(Void... params) {

            JSONObject json = JsonFunctions
                    .getJsonLoginResult("http://api.crossalertdeventer.nl/login.json");
            return json;

        }

        @Override
        protected void onPostExecute(JSONObject json) {
            super.onPostExecute(json);
            if (waitingDialog.isShowing()) {
                waitingDialog.dismiss();
            }
            try {

                LoginResult = json.getString("login");

                Log.d("LoginResult", LoginResult);

                if (LoginResult.equals("success")) {

                    WriteSettings(getActivity(), Username, Password,
                            "data.txt");

                    TabControllerMeldingen newFragment = new TabControllerMeldingen();
                    newFragment.addFragment(new KVOCards(), false, FragmentTransaction.TRANSIT_NONE);


                } else if (LoginResult.equals("failed")) {
                    final AlertDialog alertDialog = new AlertDialog.Builder(
                            getActivity()).create();
                    alertDialog.setTitle("Fout");
                    alertDialog
                            .setMessage("Gebruikersnaam of wachtwoord incorrect");
                    alertDialog.setButton("Ok",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    alertDialog.dismiss();
                                }

                            });
                    alertDialog.show();
                }

            } catch (Exception e) {
                Log.e("KVOMeldingen", "error" + e.getMessage());

            }

        }
}


这个来源实际上比这个来源更大(我将其切成两半)。

但是您看到在AsyncTask中,我尝试从TabControllerMeldingen类调用该方法以添加新片段。调用此方法时,应用崩溃

我已经阅读了stackoverflow上的另一个线程,该线程必须使用Super Oncreate方法执行某些操作,但是我无法弄清楚到底出了什么问题。

有人可以帮我这个忙吗?对我来说,理解这一点非常重要,因为我相信这些天基本上是碎片化的方式。

最佳答案

您不能这样做:

  TabControllerMeldingen newFragment = new TabControllerMeldingen();


必须通过框架创建所有活动。但是,您已经在其中(使用getActivity()方法),因此在这种情况下需要执行相同的操作。获取活动并在其上添加片段。

07-26 06:01