Syntax error setOnClickListener? - java

ok if you didnt see my previous question I asked how 2 Command button to import text to textview from edittext using Scanner? Here is what I have done:
I keep geting this error
"Syntax error on token
"setOnClickListener",
VariableDeclaratorId expected after
this token"
what am I missing or doing wrong?
package test.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
import java.util.Scanner;
import android.R.layout;
public class test extends Activity {
Scanner what = (new Scanner(System.in));
private int addbtn;
Button btn = (Button) findViewById(addbtn);
btn.setOnClickListener = (new OnClickListener() {
public void onClick(View v) {
int txtbox;
EditText txt = (EditText) findViewById(txtbox);
int tv1;
TextView txt1 = (TextView) findViewById(tv1);
txt.setText( txt.getText().toString() );}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

You're missing a closing brace } on the new OnClickListener block.
Also you shouldn't be attempting Button btn = (Button) findViewById(addbtn); before the onCreate(...) method has called setContentView(...).
On top of that, addbtn isn't a valid resource id.
Use findViewById() method before setContentView(...).

On top of which your closing brackets look messed up. What's matching the ( before the new onClickListener?

Related

Android Programming with Java, how to use R.id

studio to create an android app that adds a string from an editText with an id of input and displays it into textView with an id of output. But
EditText input = EditText(findViewById(R.id.input));
and
TextView output = TextView(findViewById(R.id.output));
doesnt work as it says Method call expected. Any kind of help would be great, thankyou.
package com.example.toshb.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
private Model model;
public MainActivity() {model = new Model();}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void processInput(View view)
{
EditText input = EditText(findViewById(R.id.input));
TextView output = TextView(findViewById(R.id.output));
model.addString(editText.getText().toString());
output.setText(model.getList());
input.setText("");
}
}
You need to add proper declaration of EditText And TextView . Before write a code please have a look here:
https://developer.android.com/index.html
EditText input = (EditText)findViewById(R.id.input);
TextView output =(TextView)findViewById(R.id.output);
model.addString(input.getText().toString());

Trying to make a Dialog on click

I'm trying to make an help Dialog which provides some helpful tips to the users of my app. The tips should be in an #string resource to handle language issues. The dialog should pop up on click and the text in it should be scrollable. My current implementation fails to meet such requirements. Here is the code:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import ch.OptiLab.visuscontroll.R;
public class MainActivity extends Activity implements OnClickListener {
TextView textView;
Button buttonende;
Button tipps;
Button btn1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
tipps = (Button) findViewById(R.id.tipps);
btn1 = (Button) findViewById(R.id.buttonSTART);
buttonende = (Button) findViewById(R.id.buttonende);
btn1.setOnClickListener(this);
tipps.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder (MainActivity.this.getActivity());
// 2. Chain together various setter methods to set the dialog characteristics
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);
// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();
}
});
Change your code to
(...)
AlertDialog dialog = builder.create();
dialog.show(); //Do not forget this line
Also, make sure to correctly initialize builder:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

setOnClickListener Error: No Idea Why?

I am new to Android Development, and I can't for the life of me, figure out why this is giving me an error. The error is occurring on this line: 'bu.setOnClickListener(new OnClickListener() {'
I am following the Android Development Fundamental tutorials on Lynda.com; and my code is identical to that of the teachers; or so I think.
package com.lynda.lyndaproject;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Main extends Activity {
Button bu = (Button) findViewById(R.id.button1);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bu.setOnClickListener(new OnClickListener() {
public void onClick(View v){
}
});
}
}
By the way, Eclipse doesn't seem to be allowing me to import anything; and I do believe importing 'android.view.View' should have imported everything necessary.
First, you need to fix your import. You are using the wrong OnClickListener. Change the line
import android.content.DialogInterface.OnClickListener;
to
import android.view.View.OnClickListener;
Next, You need to move the line
Button bu = (Button) findViewById(R.id.button1);
To after the call to setContentView(...). So your onCreate method would look like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bu = (Button) findViewById(R.id.button1);
bu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
}
});
}
This is because findViewById(...) looks for a View in the current layout. Since this has not been set by setContentView, it will always return null.

My app will only output "false", not the code I want

I used samples from similar code to make this, unfortunately I'm not to sure what I did wrong.
The purpose of this app is to output text entered in a field to a TextView, where I changed the color, when a button is pressed.
package edu.wmich.project3
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class Main extends Activity {
String txtResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button text =(Button) findViewById(R.id.btnColor0);
final TextView result = ((TextView) findViewById(R.id.txtResult));
text.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
txtResult= getText(R.id.txtField0).toString();
result.setText(txtResult);
}
});
}
}
txtResult = (findViewById(R.id.txtField0)).toString();
will solve...
the problem is that you're using the method getText() from the Activity which has nothing to do with the TextField you're dealing with.
...what is the type of view of R.id.txtField0? I guess with this you can take it from here.

java android handler calling

I am new in the android programming. I see many ways to do the event handling, but when I try to do it by calling the handler class it give error on handling class name:
package com.example.test;
import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//attach an instance of HandleClick to the Button
findViewById(R.id.button1).setOnClickListener(new HandleClick());
}
private class HandleClick implements OnClickListener{
public void onClick(View arg0) {
Button btn = (Button)arg0; //cast view to a button
// get a reference to the TextView
TextView tv = (TextView) findViewById(R.id.textview1);
// update the TextView text
tv.setText("You pressed " + btn.getText());
}
}
}
"HandleClick" error come on this it say class should be abstract type?
I do not understand why it is giving this error can any one help me?
That's the wrong OnClickListener class. You have
import android.content.DialogInterface.OnClickListener;
You need:
import android.view.View.OnClickListener;
For future reference, the error you get is "The type must implement the inherited abstract method...". This is because you need to implement the DialogInterface's onClick, which should have led you to notice that it was the wrong import (since you have onClick(View))
you imported the wrong OnClickListener, you should import the one from android.view.View
Make it simple & use this,
b1 = (Button) findViewById(R.id.button1);
TextView tv = (TextView) findViewById(R.id.textview1);
b1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast msg = Toast.makeText(getBaseContext(),
"You have clicked Button 1", Toast.LENGTH_LONG);
msg.show();
tv.setText("You pressed " + btn.getText());
}
});

Categories