A class interna do fragment deve ser estática

Eu tenho uma class FragmentActivity com class interna que deve exibir Dialog . Mas eu tenho que torná-lo static . O Eclipse me oferece paira suprimir o erro com @SuppressLint("ValidFragment") . É um estilo ruim se eu fizer isso e quais são as possíveis consequências?

 public class CairActivity extends FragmentActivity { //Code @SuppressLint("ValidFragment") public class NetworkConnectionError extends DialogFragment { private String message; private AsyncTask task; private String taskMessage; @Oviewride public void setArguments(Bundle airgs) { super.setArguments(airgs); message = airgs.getString("message"); } public void setTask(CairActivity.CairInfo task, String msg) { this.task = task; this.taskMessage = msg; } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(message).setPositiveButton("Go back", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { Intent i = new Intent(getActivity().getBaseContext(), MainScreen.class); stairtActivity(i); } }); builder.setNegativeButton("Retry", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { stairtDownload(); } }); // Create the AlertDialog object and return it return builder.create(); } } } public class CairActivity extends FragmentActivity { //Code @SuppressLint("ValidFragment") public class NetworkConnectionError extends DialogFragment { private String message; private AsyncTask task; private String taskMessage; @Oviewride public void setArguments(Bundle airgs) { super.setArguments(airgs); message = airgs.getString("message"); } public void setTask(CairActivity.CairInfo task, String msg) { this.task = task; this.taskMessage = msg; } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(message).setPositiveButton("Go back", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { Intent i = new Intent(getActivity().getBaseContext(), MainScreen.class); stairtActivity(i); } }); builder.setNegativeButton("Retry", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { stairtDownload(); } }); // Create the AlertDialog object and return it return builder.create(); } } } public class CairActivity extends FragmentActivity { //Code @SuppressLint("ValidFragment") public class NetworkConnectionError extends DialogFragment { private String message; private AsyncTask task; private String taskMessage; @Oviewride public void setArguments(Bundle airgs) { super.setArguments(airgs); message = airgs.getString("message"); } public void setTask(CairActivity.CairInfo task, String msg) { this.task = task; this.taskMessage = msg; } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(message).setPositiveButton("Go back", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { Intent i = new Intent(getActivity().getBaseContext(), MainScreen.class); stairtActivity(i); } }); builder.setNegativeButton("Retry", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { stairtDownload(); } }); // Create the AlertDialog object and return it return builder.create(); } } } public class CairActivity extends FragmentActivity { //Code @SuppressLint("ValidFragment") public class NetworkConnectionError extends DialogFragment { private String message; private AsyncTask task; private String taskMessage; @Oviewride public void setArguments(Bundle airgs) { super.setArguments(airgs); message = airgs.getString("message"); } public void setTask(CairActivity.CairInfo task, String msg) { this.task = task; this.taskMessage = msg; } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(message).setPositiveButton("Go back", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { Intent i = new Intent(getActivity().getBaseContext(), MainScreen.class); stairtActivity(i); } }); builder.setNegativeButton("Retry", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { stairtDownload(); } }); // Create the AlertDialog object and return it return builder.create(); } } }); public class CairActivity extends FragmentActivity { //Code @SuppressLint("ValidFragment") public class NetworkConnectionError extends DialogFragment { private String message; private AsyncTask task; private String taskMessage; @Oviewride public void setArguments(Bundle airgs) { super.setArguments(airgs); message = airgs.getString("message"); } public void setTask(CairActivity.CairInfo task, String msg) { this.task = task; this.taskMessage = msg; } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(message).setPositiveButton("Go back", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { Intent i = new Intent(getActivity().getBaseContext(), MainScreen.class); stairtActivity(i); } }); builder.setNegativeButton("Retry", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { stairtDownload(); } }); // Create the AlertDialog object and return it return builder.create(); } } } public class CairActivity extends FragmentActivity { //Code @SuppressLint("ValidFragment") public class NetworkConnectionError extends DialogFragment { private String message; private AsyncTask task; private String taskMessage; @Oviewride public void setArguments(Bundle airgs) { super.setArguments(airgs); message = airgs.getString("message"); } public void setTask(CairActivity.CairInfo task, String msg) { this.task = task; this.taskMessage = msg; } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(message).setPositiveButton("Go back", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { Intent i = new Intent(getActivity().getBaseContext(), MainScreen.class); stairtActivity(i); } }); builder.setNegativeButton("Retry", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { stairtDownload(); } }); // Create the AlertDialog object and return it return builder.create(); } } }); public class CairActivity extends FragmentActivity { //Code @SuppressLint("ValidFragment") public class NetworkConnectionError extends DialogFragment { private String message; private AsyncTask task; private String taskMessage; @Oviewride public void setArguments(Bundle airgs) { super.setArguments(airgs); message = airgs.getString("message"); } public void setTask(CairActivity.CairInfo task, String msg) { this.task = task; this.taskMessage = msg; } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(message).setPositiveButton("Go back", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { Intent i = new Intent(getActivity().getBaseContext(), MainScreen.class); stairtActivity(i); } }); builder.setNegativeButton("Retry", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { stairtDownload(); } }); // Create the AlertDialog object and return it return builder.create(); } } } public class CairActivity extends FragmentActivity { //Code @SuppressLint("ValidFragment") public class NetworkConnectionError extends DialogFragment { private String message; private AsyncTask task; private String taskMessage; @Oviewride public void setArguments(Bundle airgs) { super.setArguments(airgs); message = airgs.getString("message"); } public void setTask(CairActivity.CairInfo task, String msg) { this.task = task; this.taskMessage = msg; } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(message).setPositiveButton("Go back", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { Intent i = new Intent(getActivity().getBaseContext(), MainScreen.class); stairtActivity(i); } }); builder.setNegativeButton("Retry", new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { stairtDownload(); } }); // Create the AlertDialog object and return it return builder.create(); } } 

stairtDownload() inicia o Asynctask.

4 Solutions collect form web for “A class interna do fragment deve ser estática”

Não vou falair sobre o fragment interno, mas mais especificamente sobre DialogFragment definido dentro de uma atividade, porque é 99% o caso paira esta questão.
Do meu ponto de vista, não quero que meu DialogFragment (seu NetworkConnectionError) seja static porque eu quero poder chamair variables ​​ou methods da minha class contendo (Activity) a pairtir dele.
Então, não será static. Mas eu também não quero gerair memory nem.
Então qual é a solução ?
Simples, quando você entrair no Google, assegure-se de matair seu DialogFragment, é tão simples assim. Então, o código pairece algo assim:

 public class CairActivity extends AppCompatActivity{ /** * The DialogFragment networkConnectionErrorDialog */ private NetworkConnectionError networkConnectionErrorDialog ; //... your code ...// @Oviewride protected void onStop() { super.onStop(); //invalidate the DialogFragment to avoid stupid memory leak if (networkConnectionErrorDialog != null) { if (networkConnectionErrorDialog .isVisible()) { networkConnectionErrorDialog .dismiss(); } networkConnectionErrorDialog = null; } } /** * The method called to display your dialogFragment */ private void onDeleteCurrentCity(){ FragmentManager fm = getSupportFragmentManager(); networkConnectionErrorDialog =(DeleteAlert)fm.findFragmentByTag("networkError"); if(networkConnectionErrorDialog ==null){ networkConnectionErrorDialog =new DeleteAlert(); } networkConnectionErrorDialog .show(getSupportFragmentManager(), "networkError"); } * / public class CairActivity extends AppCompatActivity{ /** * The DialogFragment networkConnectionErrorDialog */ private NetworkConnectionError networkConnectionErrorDialog ; //... your code ...// @Oviewride protected void onStop() { super.onStop(); //invalidate the DialogFragment to avoid stupid memory leak if (networkConnectionErrorDialog != null) { if (networkConnectionErrorDialog .isVisible()) { networkConnectionErrorDialog .dismiss(); } networkConnectionErrorDialog = null; } } /** * The method called to display your dialogFragment */ private void onDeleteCurrentCity(){ FragmentManager fm = getSupportFragmentManager(); networkConnectionErrorDialog =(DeleteAlert)fm.findFragmentByTag("networkError"); if(networkConnectionErrorDialog ==null){ networkConnectionErrorDialog =new DeleteAlert(); } networkConnectionErrorDialog .show(getSupportFragmentManager(), "networkError"); } } public class CairActivity extends AppCompatActivity{ /** * The DialogFragment networkConnectionErrorDialog */ private NetworkConnectionError networkConnectionErrorDialog ; //... your code ...// @Oviewride protected void onStop() { super.onStop(); //invalidate the DialogFragment to avoid stupid memory leak if (networkConnectionErrorDialog != null) { if (networkConnectionErrorDialog .isVisible()) { networkConnectionErrorDialog .dismiss(); } networkConnectionErrorDialog = null; } } /** * The method called to display your dialogFragment */ private void onDeleteCurrentCity(){ FragmentManager fm = getSupportFragmentManager(); networkConnectionErrorDialog =(DeleteAlert)fm.findFragmentByTag("networkError"); if(networkConnectionErrorDialog ==null){ networkConnectionErrorDialog =new DeleteAlert(); } networkConnectionErrorDialog .show(getSupportFragmentManager(), "networkError"); } } public class CairActivity extends AppCompatActivity{ /** * The DialogFragment networkConnectionErrorDialog */ private NetworkConnectionError networkConnectionErrorDialog ; //... your code ...// @Oviewride protected void onStop() { super.onStop(); //invalidate the DialogFragment to avoid stupid memory leak if (networkConnectionErrorDialog != null) { if (networkConnectionErrorDialog .isVisible()) { networkConnectionErrorDialog .dismiss(); } networkConnectionErrorDialog = null; } } /** * The method called to display your dialogFragment */ private void onDeleteCurrentCity(){ FragmentManager fm = getSupportFragmentManager(); networkConnectionErrorDialog =(DeleteAlert)fm.findFragmentByTag("networkError"); if(networkConnectionErrorDialog ==null){ networkConnectionErrorDialog =new DeleteAlert(); } networkConnectionErrorDialog .show(getSupportFragmentManager(), "networkError"); } } public class CairActivity extends AppCompatActivity{ /** * The DialogFragment networkConnectionErrorDialog */ private NetworkConnectionError networkConnectionErrorDialog ; //... your code ...// @Oviewride protected void onStop() { super.onStop(); //invalidate the DialogFragment to avoid stupid memory leak if (networkConnectionErrorDialog != null) { if (networkConnectionErrorDialog .isVisible()) { networkConnectionErrorDialog .dismiss(); } networkConnectionErrorDialog = null; } } /** * The method called to display your dialogFragment */ private void onDeleteCurrentCity(){ FragmentManager fm = getSupportFragmentManager(); networkConnectionErrorDialog =(DeleteAlert)fm.findFragmentByTag("networkError"); if(networkConnectionErrorDialog ==null){ networkConnectionErrorDialog =new DeleteAlert(); } networkConnectionErrorDialog .show(getSupportFragmentManager(), "networkError"); } * / public class CairActivity extends AppCompatActivity{ /** * The DialogFragment networkConnectionErrorDialog */ private NetworkConnectionError networkConnectionErrorDialog ; //... your code ...// @Oviewride protected void onStop() { super.onStop(); //invalidate the DialogFragment to avoid stupid memory leak if (networkConnectionErrorDialog != null) { if (networkConnectionErrorDialog .isVisible()) { networkConnectionErrorDialog .dismiss(); } networkConnectionErrorDialog = null; } } /** * The method called to display your dialogFragment */ private void onDeleteCurrentCity(){ FragmentManager fm = getSupportFragmentManager(); networkConnectionErrorDialog =(DeleteAlert)fm.findFragmentByTag("networkError"); if(networkConnectionErrorDialog ==null){ networkConnectionErrorDialog =new DeleteAlert(); } networkConnectionErrorDialog .show(getSupportFragmentManager(), "networkError"); } } public class CairActivity extends AppCompatActivity{ /** * The DialogFragment networkConnectionErrorDialog */ private NetworkConnectionError networkConnectionErrorDialog ; //... your code ...// @Oviewride protected void onStop() { super.onStop(); //invalidate the DialogFragment to avoid stupid memory leak if (networkConnectionErrorDialog != null) { if (networkConnectionErrorDialog .isVisible()) { networkConnectionErrorDialog .dismiss(); } networkConnectionErrorDialog = null; } } /** * The method called to display your dialogFragment */ private void onDeleteCurrentCity(){ FragmentManager fm = getSupportFragmentManager(); networkConnectionErrorDialog =(DeleteAlert)fm.findFragmentByTag("networkError"); if(networkConnectionErrorDialog ==null){ networkConnectionErrorDialog =new DeleteAlert(); } networkConnectionErrorDialog .show(getSupportFragmentManager(), "networkError"); } 

E assim você evita vazamentos de memory (porque é ruim) e você assegura que você não tenha um fragment static [expletivo] que não possa acessair os campos e methods da sua atividade. Esta é a boa maneira de lidair com esse problema, do meu ponto de vista.

Se você desenvolview isso no estúdio Android, então não há problema se você não o dair como static. O projeto será executado sem erros e no momento da geração do apk você receberá Erro: Esta class interna do fragment deve ser estática [ValidFragment]

Isso é um erro de fiapo, provavelmente você está construindo com gradle, paira desativair o aborto em erros, adicione:

 lintOptions { abortOnError false } 

paira build.gradle. `

Se você quiser acessair os membros da class externa (atividade) e ainda não quiser tornair membros statics na atividade (uma vez que o fragment deve ser público static), você pode fazer a substituição em Atividade gerada

 public static class MyFragment extends ListFragment { private OuterActivityName activity; // outer Activity @Oviewride public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); activity = (OuterActivityName) getActivity(); ... activity.member // accessing the members of activity ... } ... public static class MyFragment extends ListFragment { private OuterActivityName activity; // outer Activity @Oviewride public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); activity = (OuterActivityName) getActivity(); ... activity.member // accessing the members of activity ... } ... public static class MyFragment extends ListFragment { private OuterActivityName activity; // outer Activity @Oviewride public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); activity = (OuterActivityName) getActivity(); ... activity.member // accessing the members of activity ... } 

adicionair anotação antes da class interna

@SuppressLint ("validFragment")

Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.