I got a very odd problem. I need to create a three tabs in my activiy. Eveything seems fine, but I got the only ine mistake on the same line everytime no matter is it a part if the code either a comment. Here is the code
package com.example.i.actionbar4;
import android.app.ActionBar;
import android.graphics.drawable.Drawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
public class MainActivity extends FragmentActivity {
// start
private FragmentTabHost mTabHost;
// end
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// start
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
// Error always there mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("Tab 1", null),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("Tab 2", null),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab3").setIndicator("Tab 3", null),
FragmentTab.class, null);
}
//end
Related
I'm just trying to screw around in Android Studio and at the moment, I'm trying to figure out how to get the selected text of a spinner.
Here is my relevant code
package dgameman1.com.emojifixer;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Spinner emojiSpinner = (Spinner)findViewById(R.id.emojiSpinner);
final String selectedSpinnerText = emojiSpinner.getSelectedItem().toString();
Button installNewEMojis = (Button) findViewById(R.id.installEmojisButton);
installNewEMojis.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, selectedSpinnerText, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
The issue I have right now is with this
Spinner emojiSpinner = (Spinner)findViewById(R.id.emojiSpinner);
String selectedSpinnerText = emojiSpinner.getSelectedItem().toString();
'Spinner' is a red color and when I over my mouse over it, I get the error
Cannot resolve symbol 'Spinner'
Cannot resolve symbol 'Spinner'
This means that the class has not been imported properly .
At First import R
import dgameman1.com.emojifixer.R
Then add this in your import section
import android.widget.Spinner;
Then Clean-Rebuild-Restart Your Project .
Let me know if that works. If it doesn't, there are other possible solutions I can add.
you need to import the android.widget.Spinner
change the code if you don't want to import anything.
android.widget.Spinner emojiSpinner = (android.widget.Spinner)findViewById(R.id.emojiSpinner);
I am trying to code an action bar for my android app and i defined my object to set the logo of the action bar to the default andorid icon before i update. When i run just that code, i get an error on the marked line.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
public class myabDemo extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myab_demo);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.drawable.ic_launcher); // ***Error appears here***
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
}
It's not R.drawable.icon_launcher but R.drawable.ic_launcher (At least i'm pretty sure you followed the tutorial that puts ic_launcher and not icon_launcher)
I guess you received a nullpointer exception, since actionBar is null.
Add the code below:
requestWindowFeature(Window.FEATURE_NO_TITLE);
I wanted to navigate to a new page when clicking a button. Below are my both activities I have created. I am using eclipse and I am getting an error saying The method onClick(View) of type new View.OnClickListener(){} must override a superclass method on the main activity.
This is my main activity.
package com.example.grammer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button grammerButton = (Button)findViewById(R.id.grammar);
grammerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Grammer.class);
startActivity(intent);
}
});
}
}
This is my second activity.
package com.example.grammer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.os.Build;
public class Grammer extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grammer);
Button grammerButton = (Button) findViewById(R.id.grammar);
grammerButton.setOnClickListener(new View.OnClickListener() {
// #Override
public void onClick(View view) {
finish();
}
});
}
}
Removing the #override will remove the error, but then app is not working as intended.
Check this
OnClickListener() must override a superclass method?
Uncheck "Enable project specific settings", click "Configure Workspace Settings..." and change "Compiler Compliance Level" to 1.6 or above
Have this import statement
import android.view.View.OnClickListener;
There is no need to remove #Override Annotation.
Also calling finish() is not necessary. The hardware back button does the job.
When you press back button in Grammar Activity your current activity is popped from the back stack and the previous activity in the back stack takes focus. So there is no need to call finish() on button click.
http://developer.android.com/guide/components/tasks-and-back-stack.html
Also if you have a Button with id grammar in activity_grammer.xml it is ok.
Make sure you have a button with id grammar in activity_grammer.xml
You can read the topic id
http://developer.android.com/guide/topics/ui/declaring-layout.html
An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching.
Change "Compiler Compliance Level" to 1.6. of java from project properties.
Place this code::
Just replace then name of layout and button id by your
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alayout);
Button grammerButton = (Button)findViewById(R.id.aId);
grammerButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Grammer.class);
startActivity(intent);
}
});
}
}
Just replace then name of layout and button id by your
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Grammer extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blayout);
Button grammerButton = (Button) findViewById(R.id.aId);
grammerButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
}
You are suppose to import the ViewClickListener namespace in your code.
Just Press ctrl + shift + O and it will add the relevant and missing namespaces in your project.
All you need is to import the library for OnClickListener. Just press ctrl + Shift + O in your eclipse and it will import the import android.view.View.OnClickListener file for you.
Try this..
First, You havn't import OnClickListener
import android.view.View.OnClickListener;
and second one
Button grammerButton = (Button) findViewById(R.id.grammar);
you are giving same name for both Button Ids in different layouts. Make sure you have a button with id grammar in activity_grammer.xml present are not.
Hoping to get into android app development so I'm doing some basic tutorials just now.
Just trying to get comfortable with the basics at the moment, one of which is using the Typeface class.
package org.me.myandroidstuff;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class HelloWorldActivity extends Activity implements OnClickListener
{
private View mainView;
private TextView tbox1;
private Button exitButton;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainView=(View)findViewById(R.id.mainView);
mainView.setBackgroundColor(getResources().getColor(R.color.silver));
tbox1 = (TextView)findViewById(R.id.textBox1);
tbox1.setTypeface(Typeface.MONOSPACE);
}
}
The line
tbox1 = (TextView)findViewById(R.id.textBox1);
Has a red cross beside it (I'm using eclipse) with the error
tbox1 cannot be resolved
Its been a while since i have used java, but as i aware the following code
create a new TextView object called tbox1
Assigns the tbox1 object the id specified in the xml for the TextView tag in an external main.xml
Then tbox1 executes the setTypeFace() method on itself?
Obviously I'm going wrong somewhere, any ideas? Something really simple no doubt...
You can't inform us about one error and neglect the others. Look at your code.
Besides what user370305 said, you have other problems. Namely, your Activity, according to the contract, implements OnClickListener but does not override the necessary onClick(View v) method. You must add it for the contract to be met.
So your code should look like:
package org.me.myandroidstuff;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class HelloWorldActivity extends Activity implements OnClickListener {
private View mainView;
private TextView tbox1;
private Button exitButton;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainView=(View)findViewById(R.id.mainView);
mainView.setBackgroundColor(getResources().getColor(R.color.silver));
tbox1 = (TextView)findViewById(R.id.textBox1);
tbox1.setTypeface(Typeface.MONOSPACE);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Remember, you can't talk about errors until you fix every other that might cause other errors to be falsely reported.
First try to set setContentView(R.layout.yourlayoutfilename); in onCreate().
1.) Delete line super.onCreate(savedInstanceState);
2.) Retype super.onCreate(savedInstanceState);
3.) Clean the Project
4.) Build the Project
Android crashes whenever this activity is started. here is the code.
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SettingsActivity extends Activity {
EditText vname, vphone, vemail, vaddress;
TextView textDeviceID;
Button settings_submit;
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.settingmenu);
vname = (EditText) findViewById(R.id.get_name);
vphone = (EditText) findViewById(R.id.get_phone);
vemail = (EditText) findViewById(R.id.get_email);
vaddress = (EditText) findViewById(R.id.get_address);
settings_submit = (Button) findViewById(R.id.settings_submit);
settings_submit.setOnClickListener(settings_submitOnClickListener);
TextView textDeviceID = (TextView) findViewById(R.id.deviceid);
super.onCreate(savedInstanceState);
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(
you should tell us more details. But I see that everything looks fine, except that
super.onCreate(savedInstanceState);
should go before setContentView() and all other stuff. By the way, check your xml for bad config controls too.