java


Android app button not working


I have used this snippet of code in a few other projects, but I cannot understand why it will not work here. It is a simple button that changes activities. The code is giving the error message: cannot resolve the symbol "setOnClickListener"
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public static String value2;
public static String value3;
public static String value4;
public static String value5;
public static String value6;
public static String value7;
public static String value8;
public Spinner spinnerMath;
public Spinner spinnerEnglish;
public Spinner spinnerHistory;
public Spinner spinnerScience;
public Spinner spinnerLanguage;
public Spinner spinnerReligion;
public Spinner spinnerSeventh;
public Button button = (Button) findViewById(R.id.button);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getBaseContext(), Main2Activity.class);
startActivity(intent);
}
});
}
Any help is greatly appreciated.
Without seeing more code it's hard to tell, but I'm suspicious of what 'this' is:
Intent intent = new Intent(this, Main2Activity.class);
try
Intent intent = new Intent(getBaseContext(), Main2Activity.class);
or something similar, unless you've already taken that into account. Otherwise, your ClickListener code looks good to me.
Make sure your view id is the same with the one you want to click. replace this to YOUR_CLASS.this. If intent give you an error says something about manifest, then check you manifest if Main2Activity.class is declare as activity
Button button = (Button)findViewById(R.id.button_name);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.i(TAG,"CLICKED");
Intent intent = new Intent(YOUR_CLASS.this, Main2Activity.class);
startActivity(intent);
}
});

Related Links

Tar a directory preserving structure with Apache in java
Persisting java objects in oracle blob
Multipart File Upload Using Spring Rest Template + Spring Web MVC
com.ibm.websphere.management.exception.AdminException:
Can't disable quartz-scheduler logging using SLF4J
working with the command prompt in Java
Changing 2 images at mousePressed
No javadoc for Java API in Netbeans
JBoss Wildfly with Jersey Web Services Deployment Error JBAS011859: Naming context is read-only
Different scrolling speeds on a JComponent using a single JScrollBar
Generating XML Data from Java Code to store in column of XMLTYPE
Which SSL Ciphers Should I Remove in Tomcat for the Poodle (SSL3) Vulnerability
Two questions about JsonProperty
nesting collectors in Java 8
map a json object of string arrays to a java class
Multiple zookeeper nodes causing out of memory issue?

Categories

HOME
ionic-framework
fluentd
google-play
smarty
json.net
hashmap
relayjs
fme
xmpp
jsp-tags
google-docs
filtering
spring-cloud-stream
serverspec
amazon-cloudformation
maude-system
apache-metamodel
connection-string
pythonanywhere
code-review
contextmenu
plunker
reverse-proxy
orleans
functional-testing
google-search-api
realex-payments-api
intel-pin
social-media
elasticsearch-ruby
tasklet
nameservers
twilio-api
xquery-3.0
titanium-mobile
contact-form
html5-fullscreen
nssegmentedcontrol
stacked
jquery-bootgrid
automake
hilbert-curve
reportbuilder
temporary-files
ws-security
email-templates
ensembles
pdf-reactor
reactive-cocoa-5
parentheses
upstart
libusb-win32
blacklist
mediaelement
watchconnectivity
skobbler-maps
paypal-express
python-stackless
sdf
pickadate
oauth2client
asp.net5
system.management
jms2
google-places
computer-algebra-systems
sankey-diagram
sniffer
pretty-print
android-nested-fragment
mdt
client-side-templating
bitcoinj
ceil
contenttype
html5-notifications
ruby-datamapper
django-nonrel
cassini-dev
angularjs-controller
jquery-ui-layout
punbb
adk
gwt-ext
nsobject
heartbeat
winbugs14
android-sdk-2.1
fluent-interface
asp.net-mvc-areas
paster
mozilla-prism
rescale
kdbg
xetex
yagni
thread-local-storage
msdev

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App