Cannot Resolve Symbol "OnClickListener" - java

I am getting the error Cannot Resolve Symbol "OnClickListener" and "Cannot Resolve Symbol "V""
I am starting to get into Java so I am not very good at this and not so familiar with the language
My code:
package com.emiliogaines.counter.counter;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button mButton = (Button) findViewById(R.id.More);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final TextView mTextView = (TextView) findViewById(R.id.Antal);
mTextView.setText("Some Text");
}
});
#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
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'm not sure why you did that but please move the initializing and listeners to onCreate():
Simply cut these lines of code:
Button mButton = (Button) findViewById(R.id.More);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final TextView mTextView = (TextView) findViewById(R.id.Antal);
mTextView.setText("Some Text");
}
});
and paste them in onCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mButton = (Button) findViewById(R.id.More);
final TextView mTextView = (TextView)findViewById(R.id.Antal);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mTextView.setText("Some Text");
}
});
}
And then go read the basis of coding and it's standards.

U must store this into onCreate method :
#Override
protected void onCreate(Bundle savedInstanceState) {
Button mButton = (Button) findViewById(R.id.More);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final TextView mTextView = (TextView)findViewById(R.id.Antal);
mTextView.setText("Some Text");
}
});
}

Related

Android Studio multiple onCreate methods

I've been trying to make two buttong on my main layout. The buttons are supposed to open a second layout, called barcode_scanner.xml and vragen.xml
However, only the first button opens the scanner. The second button does not do anything.
This is my current code from MainActivity.java
package com.kvprasad.zbarbarcodescanner;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button scannerButton;
//#Override
//protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
// scannerButton = (Button) findViewById(R.id.scannerButton);
//scannerButton.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View v) {
// Intent intent = new Intent(v.getContext(), BarcodeScanner.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
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);
}
#Override
//Barcodescanner knop
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.scannerButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), BarcodeScanner.class);
startActivityForResult(myIntent, 0);
}
});
}
//bovenbouw knop
public class AutoBodyActivity extends MainActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.bovenbouw);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Vragen.class);
startActivityForResult(myIntent, 0);
}
});
}};
}
This is the specific part with the onCreate methods:
#Override
//Barcodescanner knop
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.scannerButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), BarcodeScanner.class);
startActivityForResult(myIntent, 0);
}
});
}
//bovenbouw knop
public class AutoBodyActivity extends MainActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.bovenbouw);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Vragen.class);
startActivityForResult(myIntent, 0);
}
});
}};
}
The button with id: 'scannerButton' works fine, but the button with id: 'bovenbouw'
I don't see any mistakes in the code. What is possibly wrong?
You should declare both buttons in the MainActivity.onCreate(..) method
Button b1 = (Button) findViewById(R.id.b1);
Button b2 = (Button) findViewById(R.id.b2);
and then register your onClick listeners to open a new Activity.
For your second activity you'd have an own class SecondActivity with its own onCreate method to take care of stuff.
To open the second Activity you just open a new Intent like so
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent)
hope this helps :)

Android Studio Multiple onCreate methods in MainActivity.java

For a school project I am creating a simple app. However, I run into one problem to finalize the app.
I've been trying to make two buttons on my main layout. The buttons are supposed to open a second layout, one called barcode_scanner.xml and another called vragen.xml
However, only the first button opens the scanner. The second button does not do anything.
This is my current code from MainActivity.java
package com.kvprasad.zbarbarcodescanner;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button scannerButton;
#Override
//Barcodescanner knop
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.scannerButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), BarcodeScanner.class);
startActivityForResult(myIntent, 0);
}
});
}
#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
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);
}
public class bovenbouw extends MainActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.bovenbutton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Vragen.class);
startActivityForResult(myIntent, 0);}});}}}
I don't see any problems in the code. What am I possibly doing wrong?
Thank you.
Do not make multiple onCreate methods for the same activity. Handle all your button clicks in the same onCreate method. What you can do is this:
package com.kvprasad.zbarbarcodescanner;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button scannerButton;
#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
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);
}
#Override
//Barcodescanner knop
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next1 = (Button) findViewById(R.id.scannerButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), BarcodeScanner.class);
startActivityForResult(myIntent, 0);
}
});
Button next2 = (Button) findViewById(R.id.bovenbutton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Vragen.class);
startActivityForResult(myIntent, 0);}
});
}
}
You don't need 2 onCreate() methods for two buttons. As both of them are in the same layout, you can set onClickListener on both of them in one method only.
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.scannerButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(this, BarcodeScanner.class);
startActivityForResult(myIntent, 0);
}
});
Button next1 = (Button) findViewById(R.id.bovenbutton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(this, Vragen.class);
startActivityForResult(myIntent, 1);
//Make sure the second parameter is not 0, so that you can differentiate between them in onActivityReult method.
}
});
}
}
You don't have to create another class for vragen.class after bovenbutton is pressed.
just create another button for bovenbutton in the main class oncreate method besides the scanner_button
#Override
//Barcodescanner knop
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.scannerButton);
Button vragen = (Button) findViewById(R.id.bovenbutton);//button for vragen
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), BarcodeScanner.class);
startActivityForResult(myIntent, 0);
}
});
//here goes the onclicklistener for vragen button
vragen.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Vragen.class);
startActivityForResult(myIntent, 0);
}
});
}
You're not supposed to register two Activities in single .java file. You must create separate Activity.
package com.example.photosapp
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

java.lang.runtimeexception unable to start activity componentinfo

i am having this error in this code
java.lang.runtimeexception unable to start activity componentinfo
package estimatewall.example.com.estimatewall;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
EditText inputTxt;
EditText inputTxt1;
TextView setText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputTxt = (EditText) findViewById(R.id.editText);
// Store EditText in Variable
int val = Integer.parseInt(inputTxt.getText().toString());
inputTxt1 = (EditText) findViewById(R.id.editText2);
// Store EditText in Variable
int val1 = Integer.parseInt(inputTxt1.getText().toString());
float block1,block2;
final float sum;
block1=(val*12)/7;
block2=(val1*12)/10;
sum=block1*block2;
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
try {
TextView textView = (TextView) findViewById(R.id.textView3);
textView.setText("" + sum);
}catch (Exception e)
{
e.printStackTrace();
}
}
});
}
#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
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);
}
}
Probably getting NumberFormatException because here:
int val = Integer.parseInt(inputTxt.getText().toString());
trying to convert null to int.
Use inputTxt.getText() inside onClick method of Button on click listener to get values input by user in Edittext or set some default values in xml for both EditText's as android:text="0"

Class won't execute properly inside main activity? [duplicate]

This question already has answers here:
How to use java classes within one activity?
(4 answers)
Closed 7 years ago.
How can you use a java class within one activity, by that I mean is having different components of that activity spread out in a bunch of java classes. I'm a little new to android and this is what I have tried so far:
MainActivity.java
package com.example.alex.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Something(this);
}
#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
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);
}
}
Something.java
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.app.Activity;
public class Something {
private Activity activity;
private Button add,subtract,multiply,devide;
private EditText editA, editB, editC;
private double doubleA,doubleB,doubleC;
public Something(Activity a){
activity=a;
click();
}
public void click(){
editA = (EditText) activity.findViewById(R.id.editText);
editB = (EditText) activity.findViewById(R.id.editText2);
editC = (EditText) activity.findViewById(R.id.editText3);
doubleA =Double.parseDouble(editA.getText().toString());
doubleB =Double.parseDouble(editB.getText().toString());
add = (Button) activity.findViewById(R.id.add);
subtract = (Button) activity.findViewById(R.id.subtract);
multiply = (Button) activity.findViewById(R.id.multiply);
devide = (Button) activity.findViewById(R.id.devide);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
doubleC = doubleA+doubleB;
String s = "" + doubleC;
editC.setText(s);
}
});
subtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
doubleC = doubleA-doubleB;
String s = "" + doubleC;
editC.setText(s);
}
});
multiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
doubleC = doubleA*doubleB;
String s = "" + doubleC;
editC.setText(s);
}
});
devide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
doubleC = doubleA/doubleB;
String s = "" + doubleC;
editC.setText(s);
}
});
}
}
So I wasn't sure why my listeners weren't working on my buttons so I tried passing the activity to the class that has the listeners added to the buttons but that didn't work in fact now my application won't even start in the emulator. All I wanted to do was have "MainActivity" handle the "Gui" and have the "Something" class handle the listeners but no matter what I do I can't seem to make them communicate with one another to form one Activity.
Someone suggested to do a pass in main activity Something s = new Something(MainActivity.this); and call it from within main activity like this s.click(); but that didn't work.
in android every class that extends activity just have a one xml Layout to view and handle this XML file just in it's Activities,and you can make a lot of java classes that send alot of parameter but when you want to handle XML behavioral you should do in own class that Extends Activity,so you can do this:
public class MainActivity extends ActionBarActivity {
Button myBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Something(this);
myBtn=(Button)findViewById(R.id.Btn);
myBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
#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
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);
}

Android - How to re-start MainActivity after clicking a button in another class?

so I'm fairly new to Android. Lemme just explain first:
I'm trying to make a small portable music app, just for my phone and to test my skills/review what I've learned. I have the mainactivity setup to pick a song with 1 of 4 buttons, and it'll start another activity with buttons to pause, resume, and go back to the song selection screen (MainActivity). I'm trying to get the back button to both release the player and finish the activity, and I've tried many different things but nothing seems to work; the app closes due to some exception (a common one being NullPointerException).
So here's MainActivity:
package me.lemmy.portablemusic.app;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button wreckingBall, happy, lig, ligMulti;
Intent songPickedActivity = new Intent("me.lemmy.portablemusic.app.SONGPICKED");
public static MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set buttons
wreckingBall = (Button) findViewById(R.id.song_wreck);
happy = (Button) findViewById(R.id.song_happy);
lig = (Button) findViewById(R.id.song_lig);
ligMulti = (Button) findViewById(R.id.song_lig_multi);
//listeners
wreckingBall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(songPickedActivity);
player = MediaPlayer.create(MainActivity.this, R.raw.wrecking_ball);
player.start();
}
});
happy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(songPickedActivity);
player = MediaPlayer.create(MainActivity.this, R.raw.happy);
player.start();
}
});
lig.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(songPickedActivity);
player = MediaPlayer.create(MainActivity.this, R.raw.let_it_go);
player.start();
}
});
ligMulti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(songPickedActivity);
player = MediaPlayer.create(MainActivity.this, R.raw.let_it_go_multi);
player.start();
}
});
}
#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);
}
public static MediaPlayer getPlayer(){
return player;
}
}
and here's my SongPicked activity:
package me.lemmy.portablemusic.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.media.MediaPlayer;
import android.widget.TextView;
/**
* Created by Lemuel on 6/20/14.
*/
public class SongPicked extends Activity {
TextView text;
Button pause, resume, back;
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_songpicked);
text = (TextView) findViewById(R.id.tvPlaying);
pause = (Button) findViewById(R.id.buttonPause);
resume = (Button) findViewById(R.id.buttonResume);
back = (Button) findViewById(R.id.buttonBack);
player = MainActivity.getPlayer();
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
player.pause();
}
});
resume.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
player.start();
}
});
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view){
player.release();
}
});
}
#Override
protected void onPause(){
super.onPause();
finish();
}
}
Any help is appreciated, thanks!
P.S. I know I can't use copyrighted music in my apps, this is just a test.
I suggest to first make sure the activity is in the AndroidManifest file. Second is to change your intent from this:
Intent songPickedActivity = new Intent("me.lemmy.portablemusic.app.SONGPICKED");
into:
Intent songPickedActivity = new Intent(this, SongPicked.class);
Do it as well on the songPickedActivity when going back to MainActivity. You can use putExtra to send data to next intent. For more information click this link.

Categories

Resources