Open a new Activity from within a Fragment

4

How do I exit the fragment screen ( extends Fragment ) and go to a normal Activity ( extends AppCompatActivity )? Either way, even using a button, I can not. It never works.

public class Teste extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teste);
    }
}
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}



@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    FragmentManager fragmentManager = getFragmentManager();

    //noinspection SimplifiableIfStatement
    if (id == R.id.botao_tela_status) {

        fragmentManager.beginTransaction().replace(R.id.conteudo_fragmento, new FragmentoStatusTela()).commit();

        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    FragmentManager fragmentManager = getFragmentManager();

    if (id == R.id.nav_quiz_tela) {

        fragmentManager.beginTransaction().replace(R.id.conteudo_fragmento, new FragmentoQuiz()).commit();

    } else if (id == R.id.desafio2) {

        Intent intent = new Intent(MainActivity.this, Teste.class);
        startActivity(intent);

    } else if (id == R.id.desafio3) {

    } else if (id == R.id.desafio4) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

12-18 17:32:17.776 3532-3532/com.example.alexandre.nossa_historia W/PathParser: Points are too far apart 4.000000596046461
12-18 17:32:17.790 3532-3532/com.example.alexandre.nossa_historia D/AndroidRuntime: Shutting down VM
12-18 17:32:17.791 3532-3532/com.example.alexandre.nossa_historia E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.example.alexandre.nossa_historia, PID: 3532
                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.alexandre.nossa_historia/com.example.alexandre.nossa_historia.Main2Activity}: java.lang.IllegalArgumentException: AppCompat does not support the current theme features: { windowActionBar: false, windowActionBarOverlay: false, android:windowIsFloating: false, windowActionModeOverlay: false, windowNoTitle: false }
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                        at android.os.Looper.loop(Looper.java:135)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                                     Caused by: java.lang.IllegalArgumentException: AppCompat does not support the current theme features: { windowActionBar: false, windowActionBarOverlay: false, android:windowIsFloating: false, windowActionModeOverlay: false, windowNoTitle: false }
                                                                                        at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:471)
                                                                                        at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:325)
                                                                                        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:286)
                                                                                        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                                                        at com.example.alexandre.nossa_historia.Main2Activity.onCreate(Main2Activity.java:11)
                                                                                        at android.app.Activity.performCreate(Activity.java:5990)
                                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                        at android.os.Looper.loop(Looper.java:135) 
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                        at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
12-18 17:32:20.471 3532-3532/com.example.alexandre.nossa_historia I/Process: Sending signal. PID: 3532 SIG: 9
    
asked by anonymous 18.12.2017 / 03:06

2 answers

2

By analyzing StackTrace, the problem lies with the application theme. You should add the following lines in the theme for this reaction to work:

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

Old version of answer:

Looking at the previous comments, the problem seems to be a NullException when the button is triggered.

  

I'm sorry, Pablo, but it still does not work because if I go in xml and put the onClick="click_here" and run the application already closes

As you are calling the event through XML, it should be resulting in a Button NullException because it is not being inflated correctly. Since you have not posted any code snippet, I can not be sure of that.

But anyway, try changing some things in your code, for example:

No XML: Remove the "onClick" button and add an ID to it.

No Fragment, please do:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment, container, false);

    Button btn = view.findViewById(R.id.id_btn);

     /*
     * Define a ação do Botão, é o mesmo que você está fazendo através do 
     * onClick no XML.
     */
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentActivity act = getActivity();

            if (act != null) {
                startActivity(new Intent(act, OutraActivity.class));
            }
        }
    });

    return view;
}

Please note that the "R.layout.fragment" section should be the name of your layout and "R.id.id_btn" should be the ID that you gave the button.

    
18.12.2017 / 15:54
1

Inside the event that will open the new screen (button, for example), you will put something like this:

Intent abrirOutraActivity = new Intent(getActivity(), OutraActivity.class);
startActivity(abrirOutraActivity);

The difference here in opening up an Activity is that you will use getActivity() instead of this or MinhaActivity.this , because this first constructor parameter of Intent must be a Context and Fragment is not a Context (but the Activity within which it is)

    
18.12.2017 / 04:14