app keeps crashing - java

hey I'm new to programming so I thought I would just make a simple c to f converter but it keeps crashing on the emulator before I open it can anyone see why? i know the codes probally all wrong but its the only way i could think of doing it
package com.jamie.convert;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ConvertorActivity extends Activity implements OnClickListener {
TextView textout1;
EditText textIn1;
Button convert;
int x=Integer.parseInt(textIn1.getText().toString());
double fahrenheit = 1.8*x;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
convert = (Button) findViewById(R.id.convert);
textIn1=(EditText) findViewById(R.id.input);
textout1=(TextView) findViewById(R.id.output);
convert.setOnClickListener((android.view.View.OnClickListener)this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.convert) {
textout1.setText(""+fahrenheit);
}
}
}

use this code instead of this you'll get the answer
TextView textout1;
EditText textIn1;
Button convert;
static int x;
//int x=Integer.parseInt(textIn1.getText().toString());
//double fahrenheit = 1.8*x;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
convert = (Button) findViewById(R.id.button1);
textIn1=(EditText) findViewById(R.id.editText1);
textout1=(TextView) findViewById(R.id.textView1);
convert.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
x=Integer.parseInt(textIn1.getText().toString());
double fahrenheit = 1.8*x;
textout1.setText(""+fahrenheit);
}
});
}
This would heppened due to your int x would not be getting any value from the edittext.

int x=Integer.parseInt(textIn1.getText().toString());
The above line will be executed immediately the Activity is started. At this point, textIn1 will be null.
Also this line shouldn't be there in this way...
double fahrenheit = 1.8*x;
Replace those lines with...
int x;
double fahrenheit;
...and then change the onClick() method to...
#Override
public void onClick(View v) {
if(v.getId() == R.id.convert) {
x = Integer.parseInt(textIn1.getText().toString());
fahrenheit = x * 1.8 + 32
textout1.setText(String.valueOf(fahrenheit));
}
}

try to String.valueOf(fahrenheit);
instead of fahrenheit in your onClick
also move your
int x=Integer.parseInt(textIn1.getText().toString());
to the onclick methode

Related

What to do if i get cannot find symbol class onclicklistener error?

I am writing another program but this time on Android Studio as an app in Java. Now I am getting following error; error: cannot find symbol class OnClickListener
How do I fix this? Here is my code:
package tegabyte.testprogramm;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText firstNumber;
EditText secondNumber;
TextView addResult;
Button btnAdd;
double num1,num2,sum;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstNumber = (EditText)findViewById(R.id.txtBase);
secondNumber = (EditText)findViewById(R.id.txtNikotin);
addResult = (TextView)findViewById(R.id.txtResult);
btnAdd = (Button)findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
num1 = Double.parseDouble(firstNumber.getText().toString());
num2 = Double.parseDouble(secondNumber.getText().toString());
sum = num1 / 20 * num2;
addResult.setText(Double.toString(sum));
}
});
}
}
Any help is appreciated. Thank you!
You haven't imported the OnClickListener. In addition, there are multiple OnClickListeners in different SDK packages. The one I assume you're looking for is the one in View. So instead of new OnClickListener(), write new View.OnClickListener()
OnClickListener need to identify of which OnClickListener to perform click so add View
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
num1 = Double.parseDouble(firstNumber.getText().toString());
num2 = Double.parseDouble(secondNumber.getText().toString());
sum = num1 / 20 * num2;
addResult.setText(Double.toString(sum));
}
});
or when you type btnAdd.setOnClickListener(new On
you get list of hints than select View.OnClickListener()
Try this instead.I think you have not imported the OnclickListener.Either import it or use like this.
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
num1 = Double.parseDouble(firstNumber.getText().toString());
num2 = Double.parseDouble(secondNumber.getText().toString());
sum = num1 / 20 * num2;
addResult.setText(Double.toString(sum));
}
});
Also add import statement at the top like this for the view class import android.view.View;

Simple onclick button not functioning in android

I have a following snippet of codes. What I want to do is that when I click button1 just show the text. But eclipse suggest me to add onClick(DialogInterface dialog, int which) but then at btnOk.setOnClickListener(oclBtnOk); it gives me this error:
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (DialogInterface.OnClickListener)
Here is my code:
TextView tvOut;
Button btnOk;
Button btnCancel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
tvOut = (TextView) findViewById(R.id.textView1);
btnOk = (Button) findViewById(R.id.button1);
btnCancel = (Button) findViewById(R.id.button2);
// create click listener
OnClickListener oclBtnOk = new OnClickListener() {
public void onClick(View v) {
// change text of the TextView (tvOut)
tvOut.setText("Button OK clicked");
}
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
};
// assign click listener to the OK button (btnOK)
btnOk.setOnClickListener(oclBtnOk);
}
You are importing the wrong OnClickListener class.
View.setOnClickListener() Takes a View.OnClickListener, not a DialogInterface.OnClickListener, which is what you have imported.
If you don't use the DialogInterface.OnClickListener elsewhere in this class, simply change your import statement to import android.view.View.OnClickListener.
If you do also use the DialogInterface.OnClickListener interface in your class, you will need to further qualify the class name here, like so:
View.OnClickListener oclBtnOk = new View.OnClickListener() {
public void onClick(View v) {
// change text of the TextView (tvOut)
tvOut.setText("Button OK clicked");
}
}
You should also remove the onClick(DialogInterface dialog, int which) method, as that is only defined for DialogInterface.OnClickListener.
Same problem :
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (DialogInterface.OnClickListener)
calButton.setOnClickListener(addClick);
Source:
import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#SuppressWarnings("deprecation")
public class MainActivity extends ActionBarActivity {
LinearLayout layout1;
EditText no1Text,no2Text;
Button calButton;
TextView answerText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout1=new LinearLayout(this);
no1Text=new EditText(this);
no2Text=new EditText(this);
calButton=new Button(this);
answerText=new TextView(this);
answerText.setText("0");
calButton.setText("Calculate");
layout1.setOrientation(LinearLayout.VERTICAL);
calButton.setOnClickListener(addClick);
layout1.addView(no1Text);
layout1.addView(no2Text);
layout1.addView(calButton);
layout1.addView(answerText);
setContentView(layout1);
}
private OnClickListener addClick=new OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
String firstStr=no1Text.getText().toString();
String secondStr=no2Text.getText().toString();
double firstNo=Double.parseDouble(firstStr);
double secondNo=Double.parseDouble(secondStr);
double sumNo=firstNo+secondNo;
String sumStr=String.valueOf(sumNo);
answerText.setText(sumStr);
}
};
}

Incompatible Java method

I'm trying to learn Java through a tutorial on Aide and have entered what I believed was the correct code:
package com.aide.trainer.myapp;
import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set main.xml as user interface layout
setContentView(R.layout.main);
final TextView textView = (TextView) findViewById(R.id.mainTextView1);
Button button = (Button) findViewById(R.id.mainButton1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View p1)
{
// TODO: Implement this method
TextView textView = (TextView) findViewById(R.id.mainTextView1);
textView.setText("Clicked");
}
});
button.setOnLongClickListener(new OnLongClickListener()
{
#Override
public void onLongClick(View p1)
{
}
});
}
}
But at public void onLongClick(View p1), there is an error around void that says:
The type of this method is incompatible with the type of the overridden method 'android.view.View.OnLongClickListener.onLongClick(android.view.View)'
I can't figure out what I did wrong. Does anyone know?
In an OnLongClickListener, onLongClick(…) must return boolean, not void.

Android Application Force Close error

I wrote the following code and compiled it but when I run the application, I get the error android the application has stopped unexpectedly force close eclipse. I thought this is because I didn't initialize the Button and TextView objects but when I initialize them, I get missing token ";" error. What is the reason for this error.
package com.umer.first.project;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class StartingPoint extends Activity {
int counter;
TextView display;
Button add, sub;
//add= new Button(this);
//sub=new Button(this);
//display=new TextView();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
add= (Button) findViewById(R.id.aButton);
sub = (Button) findViewById(R.id.sButton);
display= (Button) findViewById(R.id.tvButton);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter++;
display.setText("The total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter--;
display.setText("You counter is " + counter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_starting_point, menu);
return true;
}
}
Display is a TextView, you can't cast it to a button.
display= (Button) findViewById(R.id.tvButton);
2. You have declared display as TextView,but initialized as Button, thats a Casting Exception .
display= (Button) findViewById(R.id.tvButton); ///// Wrong.
Must be.
display= (TextView) findViewById(R.id.tvButton);
1. Do the declaration of the Views as instance variables before onCreate() Method, then initialize them in the onCreate().

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