My Android-Emulator displays emulator error - java

I made an Android Application. Eclipse isn't reporting me what errors are there in my code.
But if I run my project then emulator displays:
emulator error http://s2.ipicture.ru/uploads/20121106/466GCZH9.png
My Java code is (MainActivity.java):
package ru.startandroid.develop.AppLog;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
TextView myText = (TextView) findViewById(R.id.myText);
Button myBtnCancel = (Button) findViewById(R.id.myBtnCancel);
Button myBtnOK = (Button) findViewById(R.id.myBtnOK);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.myBtnCancel:
myText.setText("Нажата кнопка Cancel");
break;
case R.id.myBtnOK:
myText.setText("Нажата кнопка ОК");
break;
}
}
}

You can only use findViewById() after calling setContentView():
public class MainActivity extends Activity implements OnClickListener{
TextView myText;
Button myBtnCancel;
Button myBtnOK;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView) findViewById(R.id.myText);
myBtnCancel = (Button) findViewById(R.id.myBtnCancel);
myBtnOK = (Button) findViewById(R.id.myBtnOK);
}

You need to bind the Widgets (TextView and Buttons) calling findById after the setContentView method, because depends of it:
TextView myText;
Button myBtnCancel;
Button myBtnOK;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView) findViewById(R.id.myText);
myBtnCancel = (Button) findViewById(R.id.myBtnCancel);
myBtnOK = (Button) findViewById(R.id.myBtnOK);
}

Change your code like below:
Declare variables at instance level and assign values inside onCreate()
public class MainActivity extends Activity implements OnClickListener{
TextView myText = null;
Button myBtnCancel = null;
Button myBtnOK = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView) findViewById(R.id.myText);
myBtnCancel = (Button) findViewById(R.id.myBtnCancel);
myBtnOK = (Button) findViewById(R.id.myBtnOK);
}

Related

How to increase value of a number on button press?

I am trying to create an app to increase the value of a number on screen to the next number when the button is pressed. It is not working. I have given my Java and XML code below.
XML code:
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:text="+" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:paddingLeft="200dp"
android:textSize="25sp"
android:text="0" />
Java code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btn;
TextView txt;
protected int a = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
txt = (TextView) findViewById(R.id.textView);
}
public void display(final int n) {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt.setText(n);
}
});
}
public void increment(View view) {
a = a + 1;
display(a);
}
}
The button is unreactive.
When you click the button, increase the value of a. Then pass the a to display method. Finally display the value to textView.
public class MainActivity extends AppCompatActivity {
Button btn;
TextView txt;
protected int a = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
txt = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a++;
display(a);
}
});
}
public void display(int n) {
txt.setText("" + n);
//txt.setText(n);
}
}
You have to increment your counter and set new text
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btn;
TextView txt;
protected int a = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
txt = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt.setText(String.valueOf(++a));
}
});
}
}
if I am not mistake, we can not call `onClickListener in some method.
In any case, change the code in your button to the next one and it should work.
int a = 0;
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a = a + 1;
txt.setText(a);
}
});
in this case, that code helps. becose it increases value a on 1, all time when we click on the button.
When you click the on the button, increase the value of a and display to the textView
public class MainActivity extends AppCompatActivity {
Button btn;
TextView txt;
protected int a = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
txt = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a++;
txt.setText(a + "");
}
});
}
}

Button's onClick property does not show my action

I was trying to program a simple calculator in Android Studio, only with plus, minus, times and divide functions. I am half-code in programming of "plus" function, which is triggered by clicking a button. Here is my code in MainActivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class Arithmetic {
public void plus(View view) {
EditText editText = (EditText) findViewById(R.id.editText);
EditText editText2 = (EditText) findViewById(R.id.editText2);
TextView textView = (TextView) findViewById(R.id.textView);
String numberOne = editText.getText().toString();
String numberTwo = editText2.getText().toString();
String result = numberOne + numberTwo;
}
}}
Now I have two questions. Firstly, how do I make it to show result in textView? Secondly, public void plus is not showing in button's box onClick. Why? And how do I make it to show there?
I will be really thankful for every useful tip.
1.Firstly, how do I make it to show result in textView
int sum = (Integer.parseInt(numberOne)) + (Integer.parseInt(numberTwo));
String result = sum + "";
textView.setText(result);
//or
textView.setText(sum + "");
2.Why public void plus is not showing in button's box onClick
<button
----
android:onClick = "plus"/>
and
public class MainActivity extends AppCompatActivity{
onCreate().....
public void plus(View view){
//your button code here
}
No need to create a nested class for that!
This is what you are looking for, but you should learn some Android and Java basics before starting a project:
Remove the onClick property of your button in your xml file, it's better to use a OnClickListener (particularly if you are planning to use multiple buttons)
package com.example.arnaudpradier.calculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button calculBtn;
EditText inputFieldOne;
EditText inputFieldTwo;
TextView showResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calculBtn = (Button) findViewById(R.id.calculateBtn);
inputFieldOne = (EditText) findViewById(R.id.inputFieldOne);
inputFieldTwo = (EditText) findViewById(R.id.inputFieldTwo);
showResult = (TextView) findViewById(R.id.resultText);
calculBtn.setOnClickListener(this);
}
private void makeAddition() {
int firstValue = (Integer.parseInt(inputFieldOne.getText().toString()));
int secondValue = (Integer.parseInt(inputFieldTwo.getText().toString()));
String result = (firstValue + secondValue) + "";
showResult.setText(result);
}
#Override
public void onClick(View view) {
int i = view.getId();
if (i == R.id.calculateBtn) {
makeAddition();
}
}
}
public class MainActivity extends AppCompatActivity {
EditText editText;
EditText editText2;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
textView = (TextView) findViewById(R.id.textView);
Arithmetic a = new Arithmetic();
Arithmetic.plus();
}
public class Arithmetic {
public void plus() {
String numberOne = editText.getText().toString();
String numberTwo = editText2.getText().toString();
String result = numberOne + numberTwo;
textView.setText(result);
}
}}

Android - Trying to give links to open web in two button

I'm new android development, i tried to give links to open webpage for two buttons and i get the error "} expected at line xx" can some one just check any whether i got problem with my java? and fix the closing tag issue too.
package com.domain.app;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public class MyAndroidAppActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button_Red);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yahoo.com"));
startActivity(browserIntent);
}
});
button = (Button) findViewById(R.id.button_Blue);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
}
});
}
}
Try these:
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
//Try putting one here } OR (see bottom)
public class MyAndroidAppActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button_Red);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yahoo.com"));
startActivity(browserIntent);
}
});
button = (Button) findViewById(R.id.button_Blue);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
}
});
}
}
//Or here }

How to stay in same activity after clicking button in android?

I am trying to develop simple feedback application. When user enters invalid data it should show error. After error is detected all fields should be clear and I should stay on same activity What should I do?Here's my code:
package com.example.feedback;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
public class Feedback extends Activity {
String s;
boolean fill=true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feedback);
final Button bt1 = (Button) findViewById(R.id.bt1);
final EditText tv2 = (EditText) findViewById(R.id.tv2);
final EditText tv1 = (EditText) findViewById(R.id.tv1);
final EditText tv3 = (EditText) findViewById(R.id.tv3);
final EditText tv4 = (EditText) findViewById(R.id.tv4);
final RadioGroup rg = (RadioGroup) findViewById(R.id.rg1);
bt1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
s = tv1.getText().toString();
check();
s = tv2.getText().toString();
check();
s = tv3.getText().toString();
check();
s = tv4.getText().toString();
check();
if (rg.getCheckedRadioButtonId() == -1){
Toast.makeText(Feedback.this,"Error",Toast.LENGTH_LONG).show();
}
Toast.makeText(Feedback.this,"Press again to Submit",Toast.LENGTH_LONG).show();
bt1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
startActivity(new Intent(Feedback.this,Feedback2.class));
// TODO Auto-generated method stub
}
});
// TODO Auto-generated method stub
}
private void check() {
if(s.matches("")){
Toast.makeText(Feedback.this,"Error",Toast.LENGTH_LONG).show();
}
// 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.feedback, menu);
return true;
}
}
this is the line:
startActivity(new Intent(Feedback.this,Feedback2.class));
after the click, change activity.
I suggest you to check login success in onActivityResult method and if it's true than start new activity

android java setOnClickListener error

I am trying to perform a routine when the btnExecute is pressed. I get an error at each onClick btnxxx.setOnClickListener stating "The type new View.OnClickListener(){} must implement the inherited abstract method View.OnClickListener.onClick(View). Please help me understand what's wrong.
package com.androidbook.triviaquiz8;
import android.os.Bundle;
import android.os.Environment;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
public class QuizHelpActivity extends QuizActivity {
private EditText m1_sqs1;
private EditText m1_rs1;
private EditText m1_rs2;
private EditText m1_cd;
private EditText m1_els1;
private EditText m1_els2;
private EditText m1_sa_in;
private EditText m1_sa_ft;
private EditText m1_sa_mm;
private EditText m1_sa_m;
private EditText m1_ed_in;
private EditText m1_ed_ft;
private EditText m1_ed_mm;
private EditText m1_ed_m;
private Spinner m1_sqs1_spinner;
private Spinner m1_rs1_unit;
private Spinner m1_rs2_unit;
private Spinner m1_cd_unit;
private Spinner m1_els1_unit;
private Spinner m1_els2_unit;
private QuizHelpActivity mContext;
private Button btnSave;
private Button btnClear;
private Button btnExecute;
//
// onCreate - let the fun begin!
//
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.help);
m1_sqs1 = (EditText) findViewById(R.id.m1_sqs1);
m1_rs1 = (EditText) findViewById(R.id.m1_rs1);
m1_rs2 = (EditText) findViewById(R.id.m1_rs2);
m1_cd = (EditText) findViewById(R.id.m1_cd);
m1_els1 = (EditText) findViewById(R.id.m1_els1);
m1_els2 = (EditText) findViewById(R.id.m1_els2);
m1_sa_in = (EditText) findViewById(R.id.m1_sa_in);
m1_sa_ft = (EditText) findViewById(R.id.m1_sa_ft);
m1_sa_mm = (EditText) findViewById(R.id.m1_sa_mm);
m1_sa_m = (EditText) findViewById(R.id.m1_sa_m);
m1_ed_in = (EditText) findViewById(R.id.m1_ed_in);
m1_ed_ft = (EditText) findViewById(R.id.m1_ed_ft);
m1_ed_mm = (EditText) findViewById(R.id.m1_ed_mm);
m1_ed_m = (EditText) findViewById(R.id.m1_ed_m);
// Create an OnClick Event in each button.
Button btnExecute = (Button) findViewById(R.id.btnExecute);
Button btnSave = (Button) findViewById(R.id.btnSave);
Button btnClear = (Button) findViewById(R.id.btnClear);
btnSave.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
}
});
btnClear.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
}
});
btnExecute.setOnClickListener(new OnClickListener() {
private AlertDialog show;
public void onClick(View arg0) {
if ((m1_sqs1.getText().length() == 0)
|| (m1_sqs1.getText().toString() == " ")) {
show = new AlertDialog.Builder(mContext).setTitle("Error")
.setMessage("The Square Side length is empty")
.setPositiveButton("OK", null).show();
} else if (operator.getText().equals("")) {
show = new AlertDialog.Builder(mContext).setTitle("Error")
.setMessage(" is null").setPositiveButton(
"OK", null).show();
}
{
double m1_sa_in = new Double(m1_sqs1.getText().toString())
* new Double(m1_sqs1.getText().toString());
m1_sa_in.setText(Double.toString(m1_sa_in));
}
}
}
Where you have
btnSave.setOnClickListener(new OnClickListener() {
Change that for
btnSave.setOnClickListener(new View.OnClickListener() {
It's just the newer way, so if you are following an older tutorial or blog post, that's why it would be outdated.
Edit:
import android.view.View.OnClickListener;
View view = findViewById(android.R.id.content);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// do something
}
});
Your imports list doesn't include android.view.View, just android.view.View.OnClickListener. Without knowledge of the View object that is a parameter of onClick(), the compiler can't resolve that the implementation is complete. Personally, I would fix it by:
Change (don't add) the import from android.view.View.OnClickListener to android.view.View
Update your declaration as citizen conn mentioned.
I believe you can also do this:
public class QuizHelpActivity extends QuizActivity implements OnClickListener {
adding implements OnClickListener will let you use this. For example, in your onCreate, you can have:
Button btnExecute = (Button) findViewById(R.id.btnExecute);
Button btnSave = (Button) findViewById(R.id.btnSave);
Button btnClear = (Button) findViewById(R.id.btnClear);
btnSave.setOnClickListener(this);
btnClear.setOnClickListener(this);
btnExecute.setOnClickListener(this);
Then you can implement the onClick function after onCreate, like so:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// the rest of your onCreate, including buttons
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.btnExecute:
// reference a function that would house your current onClick behavior, such as:
execute();
break;
case R.id.btnSave:
break;
case R.id.btnClear:
break;
}
}
I think this is a cleaner approach with fewer nested functions.
try this
btnSave.setOnClickListener(new OnClickListener() {
public void View.OnclickListener.onClick(View arg0) {
}
});
In this case it's maybe a better way to set an OnClick in your XML-file.
Select your 3 buttons, which have an onClickListener, in your XML-layout file. Right click on it > Properties > OnClick and for the 3 buttons give the OnClick a name (ex. onClickButtons). So, in your java code you can do like this:
public void onClickButtons(View v) {
switch (v.getId()) {
case R.id.R.id.btnExecute:
...
break;
case R.id.btnSave:
...
break;
case R.id.btnClear:
...
break;
}
}
And let your class implement the OnClickListener.
For your AlerDialog, this is a better way to handle this:
new AlertDialog.Builder(this)
.setTitle("Error")
.setMessage("is null")
.setCancelable(false)
.setPositiveButton(
"OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
Greets and have nice development!
First import the following package:
import android.view.View.OnClickListener;
Then change the following like this:
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
You can put seem This
public class MainActivity2 extends Activity implements View.OnClickListener{
Button B2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
B2=(Button) findViewById(R.id.BM2);
B2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// Do anything
}
Use this:
button1.setOnClickListener((OnClickListener) this);
Do not use this:
button1.setOnClickListener(this);

Categories

Resources