活动已被破坏异常

时间:2012-06-14 14:38:43

标签: android android-layout android-fragments android-activity

我尝试从fragmentActivity中创建一个新片段,但每次尝试时都会出现错误,告诉我:Activity已被破坏。

我创建的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方法做一些事情,但我无法确切地知道出了什么问题。

任何人都可以帮我这个..?对我来说理解这一点非常重要,因为我相信碎片是现在基本走的路。

1 个答案:

答案 0 :(得分:1)

你不能这样做:

  TabControllerMeldingen newFragment = new TabControllerMeldingen();

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