Using Fragment, Activity Error

Asked

Viewed 129 times

2

I am creating an app and would like to know where I am missing, ta working, but in error Fragment Activity.

package com.example.ronysueliton.patospizzas;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;


public class Nome_Pizzaria extends FragmentActivity {

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

    ActionBar ab = getActionBar();
    ab.setDisplayHomeAsUpEnabled(true);
    ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.logo));

    //TABS

    ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab tab1 = ab.newTab();
    tab1.setText("Pizzas");
    tab1.setTabListener(new NavegacaoTabs(new Fragment1()));
    ab.addTab(tab1);

    ActionBar.Tab tab2 = ab.newTab();
    tab2.setText("Favoritos");
    tab2.setTabListener(new NavegacaoTabs(new Fragment2()));
    ab.addTab(tab2);

}

private class NavegacaoTabs implements ActionBar.TabListener{

    private Fragment frag;

    public NavegacaoTabs(Fragment frag){
        this.frag=frag;
    }


    @Override
    public void onTabReselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
        Log.i("Script", "onTabReSelected(0");

    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
        FragmentTransaction fts = getSupportFragmentManager().beginTransaction();
        fts.replace(R.id.fragmentContainer, frag);
        fts.commit();

    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
        FragmentTransaction fts = getSupportFragmentManager().beginTransaction();
        fts.remove(frag);
        fts.commit();
    }
}

}

LOGCAT

04-11 12:39:04.186  16835-16835/com.example.ronysueliton.patospizzas D/AndroidRuntime﹕ Shutting down VM
04-11 12:39:04.205  16835-16835/com.example.ronysueliton.patospizzas E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.ronysueliton.patospizzas, PID: 16835
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ronysueliton.patospizzas/com.example.ronysueliton.patospizzas.Nome_Pizzaria}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
            at android.app.ActivityThread.access$800(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            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:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
            at com.example.ronysueliton.patospizzas.Nome_Pizzaria.onCreate(Nome_Pizzaria.java:22)
            at android.app.Activity.performCreate(Activity.java:5953)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
            at android.app.ActivityThread.access$800(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            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:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

Sem ActionBar

Sem Actionbar

  • What error is shown in Logcat?

  • @Wakim put the question above, because there was no room here.

  • I don’t think you should be calling the method getActionBar, because it is already using the support library (support-library-v4), you should use the support methods. In your case it would be getSupportActionBar, in other cases also: getSupportFragmentManager, getSupportLoaderManager and whenever there is such a thing.

  • I am following a tutorial, being that I implemented the way I need, he creates the same way that is there, but in mine it went wrong. I’m trying to change what you said.

2 answers

3

I suggest that your Activity inherits from Actionbaractivity and that the theme (Styles/manifest) applied to it use as Parent a Theme.AppCompat.Light.Darkactionbar, for example. If you want only tabs to appear later, use getSupportActionBar().hide(); on the onCreate of your Activity or on the onCreateView of your Fragments.

  • It’s already worked out. I found by studying and searching this, even so thank you very much for the tip, if I had not gotten your example was the same that I used and would work. Thank you.

0

The answer the user Wakim already gave in the comments, I think you should call getSupportActionbar() instead of getActionBar() by using Actionbar via support library. I also think your Activity should inherit Actionbaractivity, but I’m not sure.

  • I’ve already made the changes that you (@Wakim and @Informatheus) talked about, but it features new bugs, so I can implement new methods.

  • Expose here the logcat and all possible information to help you.

  • 04-11 14:30:43.516 28967-28967/com.example.ronysueliton.patospizzas E/AndroidRuntime﹕ FATAL EXCEPTION: main
 Process: com.example.ronysueliton.patospizzas, PID: 28967
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ronysueliton.patospizzas/com.example.ronysueliton.patospizzas.Nome_Pizzaria}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference

  • Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
 at com.example.ronysueliton.patospizzas.Nome_Pizzaria.onCreate(Nome_Pizzaria.java:23)

  • I think I’m making a mistake. Since I saw some people saying that it is not possible to use TAB without the action bar, is that what I’m making a mistake? 'Cause as I said above my Activity has no Action Bar but an Imageview at the top and below it the TAB.

  • According to your logcat, you are trying to call the setDisplayHomeAsUpEnabled(Boolean) method on an object that is null on line 22. About tabs, I advise you to give a good read on this page: http://developer.android.com/training/implementing-navigation/lateral.html

  • I found that this error happened because I did not put it here in android manifest: <activity android:name=".MainActivity"&#xA; android:label="Pizzaria Napolitana"&#xA; android:theme="@android:style/Theme.Holo.Light">&#xA; </activity>

  • Now it’s all right then?

  • Yes. I put solved? in the title of the question?

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.