Sam M Sam M - 2 months ago 19
Android Question

getSupportActionBar crashes my app

I'm trying to use actionbar in my application, however getSupportActionBar crashes my app.

This was working, but I don't know what I've done to make it crash (I've remove everything and tried again, it keeps crashing ).

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

public class MainMenu extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionbar = getSupportActionBar();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main_menu);
LinearLayout v = (LinearLayout) findViewById(R.id.layout);
v.setBackgroundColor(0xffECECEC);


}

/*
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.actionbar, menu);
return true;
}
*/
public void play(View view){
Intent intent = new Intent(MainMenu.this, Levels.class);
startActivity(intent);
}


}

Ram Ram
Answer

If you only want actionbar then you can adjust your code like below.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);
    ActionBar actionbar = getSupportActionBar();
    LinearLayout v = (LinearLayout) findViewById(R.id.layout);
    v.setBackgroundColor(0xffECECEC);


}

We should not write the actionbar code before setContentView() method.

And if you want full screen then there is no use of actionBar in your activity.

Comments