Problems with Fragments: Activity has been destroyed

0

My navigation drawer is correct, but when you click on a drawer item, and it will give a replace in the fragments, the application stops, and in logcat an error message appears saying

  

Activity has been destroyed

Code:

class DrawerItemClickListener extends FragmentActivity implements         ListView.OnItemClickListener {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    selectItem(position);
}

private void selectItem(int position) {

    Fragment fragment = null; 

    switch(position)
    {
    case 0:
        fragment = new DadosCadastraisDilmaFragment();
        break;
    case 1:
        fragment = new DadosCadastraisAecioFragment();
        break;
    ...
            default:
            break;
    }

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.content_frame, fragment).commit();
}
}

public class MainActivity extends ActionBarActivity{

private String [] listaCandidatos;
private DrawerLayout drawerLayout; 
private ListView drawerList;

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

    // inicializar a lista do drawer
    listaCandidatos = getResources().getStringArray(R.array.lista_candidatos);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerList = (ListView) findViewById(R.id.left_drawer);

    // Set the adapter for the list view
    drawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item,R.id.drawerListItemTextView, listaCandidatos));
    // Set the list's click listener 
    drawerList.setOnItemClickListener(new DrawerItemClickListener());
} 
    
asked by anonymous 01.10.2014 / 16:19

1 answer

0

The problem is that DrawerItemClickListener inherits from FragmentActivity . And you are doing Fragment resets to Activity ( DrawerItemClickListener ) using FragmentManager , which was not created by ActivityManager (part of the Android framework).

The message:

  

Activity has been destroyed

This is caused because this Activity has not been started. Then for him it is destroyed.

Remove this inheritance and make DrawerItemClickListener declared non-static within MainActivity .

More or less like this:

public class MainActivity extends ActionBarActivity {

    // Restante do codigo

    public class DrawerItemClickListener implements ListView.OnItemClickListener {

        // Restante do seu código

    }
}
    
01.10.2014 / 17:19