所以我有这个小代码应该打开标签布局:
public class FriendsList extends ActionBarActivity {
EditText search;
ActionBar actionBar = getSupportActionBar();
Activity activity=this;
CallbackManager callbackManager=null;
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// show the given tab
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// hide the given tab
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// probably ignore this event
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friends_list);
FacebookSdk.sdkInitialize(this);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Add 3 tabs, specifying the tab's text and TabListener
for (int i = 0; i < 3; i++) {
actionBar.addTab(
actionBar.newTab()
.setText("Tab " + (i + 1))
.setTabListener(tabListener));
}
问题是第二个我启动应用程序,它崩溃了。但是当我删除actionBar引用时,它完美地工作。崩溃时没有logcat,因为这是我的默认应用程序。如有必要,我可以分享我的清单和其余代码。
先谢谢
更新: logcat的;
ss: com.inc.nicky.messengersayit, PID: 14114
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.inc.nicky.messengersayit/com.inc.nicky.messengersayit.FriendsList}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2452)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5586)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:70)
at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:141)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:29)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:93)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:77)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:429)
at android.support.v7.app.AppCompatActivity.getSupportActionBar(AppCompatActivity.java:77)
at com.inc.nicky.messengersayit.FriendsList.<init>(FriendsList.java:58)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2452)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5586)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invo
答案 0 :(得分:1)
永远不要从数据成员初始化程序中调用从组件基类继承的方法(例如,AppCompatActivity
)。等到onCreate()
,特别是在super.onCreate()
之后。
IOW,你不能在getSupportActionBar()
打电话给你。在super.onCreate()
返回之前不要打电话。