Silly errors (beginner) - android

I have a first activity ("Home"), with two buttons: one is called About and leads to activity About and the second is named List and leads to the activity List.
Manifest.xml should be fine, but I get a load of tiny petty errors I can't fix up by myself, regrettably.
Home.class is the following
Public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button AboutButton = (Button)findViewById(R.id.About);
AboutButton.setOnClickListener(new View.OnClickListener()){
#Override
public void onClick(View view); {
Intent openAbout = new Intent(Home.this, About.class);
startActivity(openAbout);
}
}
Button ListButton = (Button)findViewById(R.id.List);
ListButton.setOnClickListener(new View.onClickListener());{
#Override
public void onClick(View view); {
Intent openList = new Intent(Home.this, List.class);
startActivity(openList);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
while About.class is like this
public class About extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
Button ReturnButton = (Button)findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
public void onClick(View view); {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_about, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and List is like this:
public class List extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Button ReturnButton = (Button)findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener()){
#Override
public void onClick(View view) {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_list, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I get lot of red light bulbs, saying, for instance that ")" or ";" is expected or (worse) onClickListener cannot be resolved
Last but not least: I copied this code online and I was wondering why after "View" there is a "view"; what does it mean?

I copied your code and I see fails everywhere... let me explain you what's going on ...
HOME CLASS
1.- You have to remove the ")"
2.- You don't have to ";" when you call onClick()
3.- When you are don on your onClick() NOW you have to close it, you missed the ");"
AboutButton.setOnClickListener(new View.OnClickListener()){ //<-- Just remove one
#Override
public void onClick(View view); { //<-- Remove this ";"
Intent openAbout = new Intent(Home.this, About.class);
startActivity(openAbout);
}
}//Here goes ");"
4.- The ListButton has the same issues so just fix it as you will fix the first one.
ABOUT CLASS
1.-On this case you have the setOnClickListener() ok, BUT why you have two onClick(View view)? It's not necessary just remove one of them.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
Button ReturnButton = (Button) findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
});
}
LIST CLASS
1.-Well in your List class you have made the same error as the first one... Your onClickListener() it's wrong.
2.-Once again you included an unnecessary ")" on new View.OnClickListener() just remove it,
3.-Another fail that I'm seeing is that you are trying to make an Intent but you are refering that you are on About.this and you are NOT. You are on List class so you have ot put List.this because the first parameter refers :
A Context as its first parameter (this is used because the Activity class is a subclass of Context)
More information about Intents
4.- You need to close again the setOnClickListener()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Button ReturnButton = (Button)findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener()){ //<-- remove one ")"
#Override
public void onClick(View view) {
Intent returnhome = new Intent(About.this, Home.class); //<-- Remove About.this and put List.this
startActivity(returnhome);
}
}//<-- Close the setOnClickListener() with ");"
}
It's okay guy, this is your first question and I'll answer it, but NOW as I've made the favor to take my time and explain to you what was wrong on your code take your time to :
How do I as a question on StackOverflow
Learn some Android basics
And the most IMPORTANT THING
DO NOT COPY PASTE AN INTERNET CODE if you don't know the basics, I mean you can copy paste the code, but you'll face with this problem every time you do this, so first of all read the tutorial, make an examples, and you'll improve every day.

Related

Returning back to a previous activity?

I have an main activity that opens a child activity:
startActivity(new Intent(this, ChildActivity.class));
From within the child activity, I press the Back button to return back to the main activity. I also have this code in the main activity:
#Override
protected void onResume() {
super.onResume();
dosomething();
}
However, onResume is never reached.
Am I missing something?
This way, it's calling onResume() when you go back from child Activity to parent: Activity:
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView mTextView = (TextView)findViewById(R.id.mTextView);
mTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,ChildActivity.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
protected void onResume() {
super.onResume();
Toast.makeText(this,"Yes calling",Toast.LENGTH_LONG).show();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ChildActivity.java
public class ChildActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
Toolbar mtooToolbar= (Toolbar)findViewById(R.id.mtoolBar);
setSupportActionBar(mtooToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_child, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if(id==android.R.id.home){
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
onResume() is called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.
Always followed by onPause().
Check this link to understand the relationship:
Activity Lifecycle
you can use finish() method to finish your activity.

Post to wall without a shareDialog() SDK4.0

I can post to Facebook using their sharDialog() pop-up. However, if I want to post a set message on a button click, without having the dialog pop up, how do I go about doing that?
public class MainActivity extends AppCompatActivity {
private ShareDialog share;
private ShareLinkContent content;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
content = new ShareLinkContent.Builder().setContentTitle("CURRENT LOCATION").build();
share = new ShareDialog(this);
ImageButton fbBut = (ImageButton) findViewById(R.id.facebook_button);
fbBut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
share.show(content);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
//Opens the settings preferences
Intent settings = new Intent(MainActivity.this, SettingsActivity.class);
MainActivity.this.startActivity(settings);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onResume() {
super.onResume();
// Logs 'install' and 'app activate' App Events.
AppEventsLogger.activateApp(this);
}
#Override
protected void onPause() {
super.onPause();
// Logs 'app deactivate' App Event.
AppEventsLogger.deactivateApp(this);
}
}
This is the code that I have so far.

How Can I Make my ImageButton go to another activity in Android Studio?

I want to know how I can make my ImageButton to go to another activity in Android Studio 1.2.2?
I tried to make it using the way that was made for buttons.
This is my Java code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_01);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_menu_01, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
You first need to create a ImageButton on the layout.xml of your current activity:
<ImageButton
android:id="#+id/your_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Than, in your current activity, you need to create a variable ImageButton:
ImageButton myButton = (ImageButton) findViewById(R.id.the_button_you_created_on_the_layout.xml_file);
Then, you need to set a click listener to this button:
*The click listener will "wait" for the click in your button.
myButton.setOnClickListener(new View.OnClickListner(){
// When the button is pressed/clicked, it will run the code below
#Override
public void onClick(){
// Intent is what you use to start another activity
Intent intent = new Intent(this, YourActivity.class);
startActivity(intent);
}
});
After that, your app should start the other activity with no problem.
Here is some sample code for you to work with. The idea is to set an OnClickListener for your ImageButton, and in that OnClickListener create an Intent to go to your new Activity, then call startActivity(intent).
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_01);
ImageButton button = (ImageButton) findViewById(R.id.my_button);
final Context context = this;
button.setOnClickListener(new View.OnClickListner(){
#Override
public void onClick(View v){
Intent intent = new Intent(context, NewActivity.class);
startActivity(intent);
}
});
}

Why are my buttons in Android not working?

I have created a menu activity with 3 buttons (main,history,logout).
I do not know why all of buttons are not working and there's logout function.
Anyway i have no idea about logout function, is it right function or wrong function?
I am new to Android, any help will be appreciated.
Here is my code:
public class MenuActivity extends ActionBarActivity implements View.OnClickListener {
Button mainBtn,historyBtn,logoutBtn ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
mainBtn = (Button)findViewById(R.id.button1);
historyBtn = (Button)findViewById(R.id.button2);
logoutBtn = (Button)findViewById(R.id.button3);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
Intent main = new Intent (this, MainActivity2.class);
startActivity(main);
break;
case R.id.button2:
Intent history = new Intent(this, HistoryActivity.class);
startActivity(history);
break;
case R.id.button3:
Intent logout = new Intent(this, LoginActivity.class);
keluar.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(logout);
this.finish();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Have you done this?
mainBtn.setOnClickListener(this);
historyBtn.setOnClickListener(this);
logoutBtn.setOnClickListener(this);
i.e. Register the onClickListener for the buttons, either in code or within the xml file.
Set listner to all buttons like..
mainBtn.setOnClickListener(this);
You will need to register the buttons with the click listener. Replace your code with the following..
public class MenuActivity extends ActionBarActivity implements View.OnClickListener {
Button mainBtn,historyBtn,logoutBtn ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
mainBtn = (Button)findViewById(R.id.button1);
historyBtn = (Button)findViewById(R.id.button2);
logoutBtn = (Button)findViewById(R.id.button3);
// Register your buttons with the click listener
mainBtn.setOnClickListener(this);
historyBtn.setOnClickListener(this);
logoutBtn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
Intent main = new Intent (this, MainActivity2.class);
startActivity(main);
break;
case R.id.button2:
Intent history = new Intent(this, HistoryActivity.class);
startActivity(history);
break;
case R.id.button3:
Intent logout = new Intent(this, LoginActivity.class);
keluar.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(logout);
this.finish();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
You didn't set a listener to your buttons.
mainBtn = (Button)findViewById(R.id.button1);
historyBtn = (Button)findViewById(R.id.button2);
logoutBtn = (Button)findViewById(R.id.button3);
mainBtn.setOnClickListener(this);
historyBtn.setOnClickListener(this);
logoutBtn.setOnClickListener(this);
Good Luck With your work. :)
mainBtn.setOnClickListener(this);
historyBtn.setOnClickListener(this);
logoutBtn.setOnClickListener(this);

passing value from mainactivity to a listview

Hi everyone I am currently developing an android app, I am having some confusions in the code, I have a main activity in which I store some array values.
I have two xml files(one mainactivity.xml and other the other one is listview.xml)
In main activity.xml there are four switches, when I click any particular switch it should take me to the listview.xml, with a corresponding array displayed on list view by list view adapter. The code is as follows
public class MainActivity extends ActionBarActivity {
ListView l;
Button chem = (Button) findViewById(R.id.button4);
public String[] contentc = {
"Abundance",
"Anxiety",
"Bruxism",
"Discipline",
"Drug Addiction"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l= (ListView) findViewById(R.id.listview);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_element, contentp);
chem.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
//DO SOMETHING! {RUN SOME FUNCTION ... DO CHECKS... ETC}
setContentView(R.layout.list);
l.setAdapter(adapter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Intent intent = new Intent(activity2.this, activity1.class);
intent.putExtra("message", message);
startActivity(intent);
In activity1, in onCreate(), you can get the String message by retrieving a Bundle (which contains all the messages sent by the calling activity) and call getString() on it :
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message")

Categories

Resources