Reputation: 107
I try to start a new fragment from my fragmentActivity, but every time I try it, it comes up with a error, that tells me : Activity has been destroyed.
Source code from a tabController i've created:
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);
}
}
This code opens the KVOMeldingen class in most cases. So source of KVOMeldingen is:
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());
}
}
}
This source is actually bigger than this (i've cut it in half).
But you see in the AsyncTask, i try to call the method from the TabControllerMeldingen class to add a new fragment. When this method get's called, the app crashes
I've read on another thread on stackoverflow that it has to do something with the Super Oncreate method, but I can't figure out exactly what is going wrong.
Can anybody help me with this..? it's really important for me to understand this, because i believe fragments is the way to go basically these days.
Upvotes: 1
Views: 3495
Reputation: 30168
You can't do this:
TabControllerMeldingen newFragment = new TabControllerMeldingen();
All activities have to be created through the framework. However, you are already inside one (you use the getActivity()
method), so you need to do the same in this case. Get the activity and add the fragment on that.
Upvotes: 1