I'm a noob in java android but I get this error when I hover onCreate method onCreate(android.os.Bundle) is never used. Should I worry about this error?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.addTab(actionbar.newTab().setText("Feed").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Browse").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Settings").setTabListener(this));
}
Thanks
thats not an error its a warning. the bundle is used for putting information in when the app changes state ie. orientation so you can continue where the user left off.
there is no harm in not using it however the activity will be shown just as it would if you were to launch it from the launcher.
Got it! I just changed extends FragmentActivity into extends Activity and imported android.app.Activity.
Related
I get a massage says "app is keep stopping" when I run the project on my mobile.
the error at on Create(MainActivity.java:16)
at the line 16 set Content View method
help me
The method setContentView() sets the View that your MainActivity should display.
The View is usually declared in layout/activity_main.xml.
Some error seems to appear when trying to call this method in your application.
A common reason why android applications crash there is calling some methods in the overridden method onCreate(Bundle savedInstanceState) in a wrong order.
Check if your code follows this pattern:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView call
// other things
}
I cannot help you further now, please copy and paste the stacktrace (crash information) into your question. When your application just crashed on an emulator, you can see it in the Run-section (usually red text).
I am developing an app for Android where I use the front camera as a mirror and have controls on top to do various functions. Now I was wondering how I can get the MainActivity to stay locked as 'portrait' and have another activity on top (which has all the buttons and has a transparent background) to have freedom to rotate?
Thanks
So you have two activites, one for which orientation is fixed and another one is free to rotate.
For the activity for which orientation is to be fixed, add this line in its activity tag in Manifest.xml :
<activity
android:name=".MyPreferencesActivity"
android:screenOrientation="landscape"/>
And by default the other activity is free to rotate.Mark the answer as verified if it works.
The above way is static (defined in xml).Ii it doesn't work, try this programmatical(can be changed during runtime) method also:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
public void onButtonPressed(Uri uri) {getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);}
I am new to Android development. My app is compiling perfectly fine (no errors, no warnings), but it crashes on launch. I have diagnosed the cause of the crash to be a NullPointerException, and I discovered that if I remove TextView output = (TextView) findViewById(R.id.outputbox); as well as all the lines depending on this declaration, the app launches fine. I did research (both on and off Stack Exchange, and about 5 pages of Google results) and none of the solutions I have found worked, which is why I am asking this question. I have ran setContentView(R.layout.activity_main); before TextView output = (TextView) findViewById(R.id.outputbox); and outputbox is declared in activity_main.xml. This is not a duplicate of the existing questions because the answers to the other ones did not solve my problem.
most probably your declaration and initialization of the variable output is in the class. This is not going to work. The method findViewById will work only after the setContentView method has been invoked. That is why you have to declare your variable in the class, but initialize it in the method onCreate immediately after the setContentView method:
public class MainActivity extends AppCompatActivity {
TextView output;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output = (TextView) findViewById(R.id.outputbox)
}
}
The method findViewById searches for your component in the xml. For this to work, you have to point out what xml file will this Activity be using. This is done in the setContentView(R.layout.activity_main); command. It is a common mistake, so no worries :)
I want to set the Home Up Button in my PreferenceScreen so i tried with this code
if (android.os.Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB){
getActionBar().setDisplayHomeAsUpEnabled(true);
}
My App require minSdkVersion10 (GB) so i check the android version with Build.VERSION.SDK_INT. The problem is that eclipse give me an error on getActionBarMethod() because "Call requires API level 11 (current min is 10): android.preference.PreferenceActivity#getActionBar" How can i solve?
add this to the your method above where you use this code,
In this case i used onCreate method.
#SuppressLint("NewApi") or #TargetApi(HONEYCOMB)
public void onCreate(Bundle savedInstance){
if (android.os.Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB){
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
For you to use actionbar on gingerbread your best solution would be to use ActionBarSherlock library by Jake Wharton
Being new to android development, i have created a project using tabs using tutourials.
on Gingerbread it looks sort of like :
While on ICS(emulator) it looks like:
Because of the sort of "Holo" type teme i have going i cant have the tab on the gingerbread style. I need help please it is killing me. Please dont tell me i have to figure out how to design it myself for Gingerbread i'll cry.
here is what i did:
public class MainActivity extends FragmentActivity
{
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabframelayout);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab 1"),
FragmentTab.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab 2"),
FragmentTab.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab 3"),
FragmentTab.class, null);
}
It is greatly suggested that you use Action Bar Tabs rather than using TabHost. While the Action Bar was added in Honeycomb, you can use compatibility libraries like ActionBarSherlock (which would give you consistent tabs across all devices and my personal recommendation) or follow the Creating Backward-Compatible UIs training (which is very similar to what you are doing now).