Reputation: 159
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);
}
}
Upvotes: 0
Views: 1425
Reputation: 14918
when i was directly using getSupportActionBar() method my app was crashed instead of that i am using ActionBar actionbar=getSupportActionBar(); then you can do and perform any operation on action bar like actionbar.show(),actionbar.hide() and other operations.
Upvotes: -1
Reputation: 114
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.
Upvotes: 2