Android Studio - i-Programmer Tutorial - MainActivity implements View.onClickListener - Cannot resolve symbol error - onclicklistener

I'm following this tutorial: http://www.i-programmer.info/programming/android/10051-android-adventures-events.html?start=1
If you scroll down the header: Implement the interface in the activity
The picture shows an implement error, though I'm getting the 'Cannot resolve symbol Error'
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
// import android.view.View.OnClickListener
public class MainActivity extends AppCompatActivity implements View.onClickListener {
#Override
public void onClick(View v) {
//Handle event
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
}

You need to start with a capital letter on OnClickListener, change:
... implements View.onClickListener {
to
... implements View.OnClickListener {
Additionally, since you have imported:
import android.view.View.OnClickListener;
you can ommit View in View.OnClickListener, that is:
public class MainActivity extends AppCompatActivity implements OnClickListener {
Also, as a suggestion for future questions, please insert your code as text in the question, not as an image as it makes it possible for us to copy-paste rather than rewrite everything.

Related

JavaFX ChoiceBox doesn't show initial value

I have a factory method that gives me a ChoiceBox with a bidirectional binding to an enum property. The choice box works as expected, except that it doesn't show the initial value set in the property constructor. Instead the choice box shows up initially as blank, not showing any value. What's wrong?
Here's an MRE:
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ChoiceBoxMRE extends Application {
private ObjectProperty<Table> table = new SimpleObjectProperty<>(Table.BIG);
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
VBox vBox = new VBox();
vBox.getChildren().add(getChoiceBox(table));
primaryStage.setScene(new Scene(vBox));
primaryStage.show();
}
private <T extends Enum<T>> ChoiceBox<T> getChoiceBox(ObjectProperty<T> objectProperty) {
ChoiceBox<T> choiceBox = new ChoiceBox<>();
choiceBox.getItems()
.addAll(objectProperty.getValue().getDeclaringClass().getEnumConstants());
Bindings.bindBidirectional(objectProperty, choiceBox.valueProperty());
return choiceBox;
}
enum Table {
BIG, SMALL;
}
}
I am not sure if that is a bug or not, but there is a simple workaround: simply select the value prior to binding it:
choiceBox.getSelectionModel().select(objectProperty.get());

unable to update back4app database in android studio

So I've linked my android app to back4app parse server. According to their documentation when I run the following code a class is supposed to be created in the database. However Android studio shows the following errors:
cannot resolve symbol SaveCallback
Method does not override method from its superclass
package com.example.android.beastt;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.parse.ParseObject;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseUser;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ParseObject soccerPlayers = new ParseObject("SoccerPlayers");
// Store an object
soccerPlayers.put("playerName", "A. Wed");
soccerPlayers.put("yearOfBirth", 1997);
soccerPlayers.put("emailContact", "a.wed#email.io");
soccerPlayers.addAllUnique("attributes", Arrays.asList("fast", "good conditioning"));
// Saving object
soccerPlayers.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
// Success
} else {
// Error
}
}
});
}
}

Event handler for objects of the same Pane

i'm studying the mechanism of the event handler on javaFX ,but i'm not sure that i have understand it , in Fact I have a little doubt :
if i have two object , they have all necessary code to handle the event (EventHandler interface ecc..),that they BELONG at the SAME stackPane ,the question is : is there a way for the 1st object to launch an event (ActionEvent for example ) that will be handled by the 2 object though they belong at the same Pane?
Because for what i have understand about the "event route",this is not possible ,at least directly.
In essence my little program have a splitpane that divided in two stackpane the screen,in the left panel i have put a gridpane with the button, each of them have the function that permit to draw a different shape ,in the right panel with a canvas.
My idea it was to launch an ActionEvent in setonaction of each button ,implements the EventHandlers on canvas to capture the event
with relative handle method ,and in the corp of he handle mode discriminate which button is Clicked for drawing the correct shape.
Can someone help me ? Thanks a lot anyway
package es1;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* #author DAVIDE
*/
public class Es1 extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
lanciaevento(this.getClass().toString());
}
});
//add a button on left panel and a textfiled on the right for test if the event launched
//on the click of the button is reached by the textfield
Textfield text = new Textfield();
StackPane panel1 = new StackPane();
panel1.getChildren().addAll(btn);
StackPane panel2 = new StackPane();
panel2.getChildren().addAll(text);
splitpane divisore = new splitpane();
divisore.addEventHandler(ActionEvent.ACTION, divisore);
divisore.getItems().addAll(panel1,panel2);
Scene scene = new Scene(divisore, 600, 450);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public void lanciaevento(String oggetto)
{
ActionEvent evento = new ActionEvent();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
package es1;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.SplitPane;
/**
*
* #author DAVIDE
*/
public class splitpane extends SplitPane implements EventHandler<ActionEvent>{
private String message_event;
public String get_message()
{
return(message_event);
}
public void set_message(String messaggio)
{
message_event = messaggio;
}
#Override
public void handle(ActionEvent event) {
System.out.println("mi ha mandato un messaggio "+event.getSource().toString());
}
}
/*
* To change this license header, choose License Headers in Project Properties.
package es1;
import javafx.event.ActionEvent;
javafx.event.EventHandler;
import javafx.scene.control.TextField;
/**
*
* #author DAVIDE
*/
public class Textfield extends TextField implements EventHandler<ActionEvent> {
#Override
public void handle(ActionEvent event) {
this.appendText(event.getSource().toString());
}
}

How to inject objects which are not annotated Spring classes

Lets say I've got a JavaFx class and an ViewState class which needs to have referance of stage created in start method. How am I able to autowire such dependency? I know that Stage.class is not annotated as #Component so Spring is unable to detect duch #Bean.
#SpringBootApplication
#ComponentScan({"controller","service","dao","javafx.stage.Stage"})
#EntityScan( basePackages = {"Model"})
#Import({ SpringConfig.class})
public class JavaFXandSpringBootTestApplication extends Application{
private ApplicationContext ctx;
public static void main(String[] args) {
launch();
}
#Override
public void start(Stage stage) throws Exception {
ViewState viewState = ctx.getBean(ViewState.class);
}
ViewState class:
#Componenet
public class ViewState {
#Autowired
private ApplicationContext ctx;
private Stage stage;
#Autowired
public ViewState(Stage stage)
{
this.stage = stage;
}
}
Compiler massage:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javafx.stage.Stage' available: expected at least 1
You probably don't want to do this at all: your ViewState class would seem to be a model class of some kind, so it should not have references to UI elements (such as Stages).
For completeness, though, here is an example that works, using ConfigurableApplicationContext.getBeanFactory().registerResolvableDependency(...). Note that, since you won't be able to create the View object until the stage is registered, you need to make the View bean #Lazy:
package example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
#Component
#Lazy
public class View {
#Autowired
public View(Stage stage) {
Button close = new Button("Close");
close.setOnAction(e -> stage.hide());
StackPane root = new StackPane(close);
Scene scene = new Scene(root, 400, 400);
stage.setScene(scene);
stage.show();
}
}
package example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import javafx.application.Application;
import javafx.stage.Stage;
#SpringBootApplication
public class Main extends Application {
private ConfigurableApplicationContext ctx ;
#Override
public void start(Stage primaryStage) {
ctx = SpringApplication.run(Main.class);
ctx.getBeanFactory().registerResolvableDependency(Stage.class, primaryStage);
ctx.getBean(View.class);
}
#Override
public void stop() {
ctx.close();
}
public static void main(String[] args) {
launch(args);
}
}

warning The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})

package com.example.andrappexp1;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
......;
public class MainActivity extends Activity {
private Button button;//set a variable
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //diaplay the Main_activity
button=(Button)findViewById(R.id.button1);//find the button1;
button.setOnClickListener(new OnClickListener() { ///<----here is warning.
#Override
public void onclick(View source) { ///and here. see bellow,
//System.out.println("show on the screen");
TextView show=(TextView)findViewById(R.id.button1);
show.setText("hit");
}
}
})
The method onclick(View) of type new OnClickListener(){} must override or implement a supertype method
Please try to properly format your question and make sure you are really asking a question (FAQ: How to ask). Additionally you should search for similar questions/problems before posting....
It looks like you imported the wrong OnClickListener. You need to import View.OnClickListener.
Either change your import or do it like that:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onclick(View source) {
//System.out.println("show on the screen");
TextView show=(TextView)findViewById(R.id.button1);
show.setText("hit");
}
}

Resources