estou criando um app e gostaria de saber onde estou errando, ta funcionando, mas na activity do fragment da erro.
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
getActionBar
, porque já que está usando a biblioteca de suporte (support-library-v4
), você deve usar os métodos de suporte. No seu caso seriagetSupportActionBar
, nos demais casos também:getSupportFragmentManager
,getSupportLoaderManager
e sempre que existir algo desse tipo.