我尝试从我的 FragmentActivity 开始一个新的 Fragment,但每次我尝试它时,它都会出现一个错误,告诉我: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 方法做一些事情,但我无法弄清楚到底出了什么问题。
任何人都可以帮我解决这个问题..?理解这一点对我来说真的很重要,因为我相信片段是这些天基本上要走的路。