1

My fragment class is

public class GetFragmentManager extends FragmentActivity {
    public FragmentManager getSupportFragmentMethod(){
        FragmentManager s = getSupportFragmentManager();
        return s;
    }
}

I needed the getSupportFragmentManager method(which i can get from the FragmentActivity class) so i made this class which extends the FragmentActivity class.

My Activity code(this extends the Activity class) is

public void showFileChooser(View v){
    DialogFragment a =new FireMissilesDialogFragment();
    a.show(getSupportFragmentManager1(), "missiles");

}

private android.support.v4.app.FragmentManager getSupportFragmentManager1() {
     android.support.v4.app.FragmentManager ab = new GetFragmentManager().getSupportFragmentMethod();
    return ab;
}

The error statement coming is Activity is being destroyed.

Please can anyone find what is going wrong in here.I have spend many hours on this.Thanks everyone for your time.

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
Debojyoti
  • 169
  • 1
  • 14
  • Please post more code and plus the logcat error message. – Andrei T Jul 30 '16 at 08:21
  • However, on a second look, you are basically using Activity as a manager. This is wrong. – Andrei T Jul 30 '16 at 08:23
  • @AndreiT i looked upon that and yes the code is wrong.But then how will i get the getSupportFragmentManager method?Is there a way to get it as well as extending the Activity class? – Debojyoti Jul 30 '16 at 08:26
  • Fragment has method `getFragmentManager()`. Support fragment returns support fragment manager. Don't overthink it. – Eugen Pechanec Jul 30 '16 at 08:52

4 Answers4

2

Sorry for wasting all of your time.
It seems that Activity class has a method getFragmentManager(), which I knew at the time, but wasn't running correctly, as I was wrong in referencing a class (specifically the fragment class) in a code line. Also at every import on fragments I imported not the app.v4 support version but the main version.

The main activity class extends the Activity class

public class Profile extends Activity implements View.OnTouchListener{

And on the same class the open fragment method is written

public void showFileChooser(View v){

    a =new FireMissilesDialogFragment();
    a.show(getFragmentManager(),"text");

}

and the FireMissilesDialogFragment is as follow.

public class FireMissilesDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("dialog_fire_missiles")
            .setPositiveButton("fire", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // FIRE ZE MISSILES!
                }
            })
            .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });
    // Create the AlertDialog object and return it
    return builder.create();
}
}

The things which changed are that for every app.v4 support version import i didn't import the v4 version but the regular version. Even if the dialog works the manifest file will show the error "java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity"

Anyways thanks everyone for your time. I will upvote the two answers as they have been useful in coming at this point.

Andrei T
  • 2,985
  • 3
  • 21
  • 28
Debojyoti
  • 169
  • 1
  • 14
1

you can not instantiate an Activity like you instantiated new GetFragmentManager() , pass an already instantiated activity to the method.

For example your main activity (the activity currently on screen) extends fragment manager, then inside that you call this.getSupportFragmentManager()

dumb_terminal
  • 1,815
  • 1
  • 16
  • 27
  • But i cant as the activity which is already instantiated extends the Activity class and not the Fragment Activity class.I want the getSupportFragmentManager method. – Debojyoti Jul 30 '16 at 08:24
  • is there any reason why you aren't extending FragmentActivity ? FragmentActivity also inherits Activity which means it gets all features of activity and more from FragmentActivity. – dumb_terminal Jul 30 '16 at 08:25
  • Is it true ? let me try and get you on this on a while – Debojyoti Jul 30 '16 at 08:27
  • now the error coming on the log is java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity – Debojyoti Jul 30 '16 at 08:30
  • check http://stackoverflow.com/questions/30180052/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity-chan – dumb_terminal Jul 30 '16 at 08:50
  • also the source code for the activity wont hurt, what is the package name of your FragmentActivity ? – dumb_terminal Jul 30 '16 at 08:50
1

First of all, if you want to use activity for starting a fragment you first need to pass the onCreate threshold, then you create the fragment:

public class MyActivity  extends AppCompatActivity {
  @Override
  public void onCreate(Bundle saveInstanceState){
      super.oncCreate(saveInstanceState);
      //create your file chooser, etc.
      DialogFragment a =new FireMissilesDialogFragment();
      a.show(getSupportFragmentManager(), "missiles"); 
       //getSupportManager exists in the activity
  }
}

You can also create it in onResume, onStart, whenever you feel like.

Edit

For appcompat you can look at the following to understand the issue:
Update your style resources
For relevant stack posts:
You need to use a Theme.AppCompat theme (or descendant) with this activity
As you can understand you need to define the theme in your activity or in your application:

android:theme="@style/Theme.AppCompat" >
Community
  • 1
  • 1
Andrei T
  • 2,985
  • 3
  • 21
  • 28
  • thanks for your reply.Let me check that and come over to you – Debojyoti Jul 30 '16 at 08:31
  • how do i call this activity?Let me remind you that i want the fragment to show from Myactivity which extends activity Class – Debojyoti Jul 30 '16 at 08:37
  • @Debojyoti MyActivity.this or this, it depends if it is some inner class or not. – Andrei T Jul 30 '16 at 08:41
  • I initialized the dialog fragment on oncreate method and executed the show method on the showFileChooser method(All on the main activity which extends AppCompat Activity).The error showing on the log now is java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. – Debojyoti Jul 30 '16 at 08:42
0

just extend AppCompatActivity instead of Activity and then you can use

FireMissilesDialogFragment a =new FireMissilesDialogFragment();
    a.show(getSupportFragmentManager1(), "missiles");
Daniel Nyamasyo
  • 2,152
  • 1
  • 24
  • 23