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().
Related
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
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);
}
};
}
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I seem to get at force close when i start the app on my Nexsus 4. Big possibility its just me that fools around but i can't find anything wrong with the code.
Android Studio gives no erros. The reset method that is commented out i supposed to reset the checkbox and the TextView.
Why can't i define items before onCreate ? And when i define them after i cannot use them in the "reset" method..
Could someone plese have a look ?
package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
final TextView txtTest = (TextView) findViewById(R.id.txtOut);
final CheckBox chkTest = (CheckBox) findViewById(R.id.chkTest);
final Button btnTest = (Button) findViewById(R.id.btnTest);
final EditText etTest = (EditText) findViewById(R.id.etTest);
String text = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chkTest.setChecked(true);
chkTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (chkTest.isChecked()) {
txtTest.setText("Checked :)");
} else txtTest.setText("Not Checked");
}
});
btnTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//etTest.getText().toString(text);
text = etTest.toString();
// Reset(text);
}
});
/*void Reset(String text) {
chkTest.setChecked(false);
txtTest.setText(text);
}*/
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
This is the logcat error:
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{[...].MyActivity}: java.lang.NullPointerException
You can not initialize the item in this way! The layout is not inflated yet when is executed the code final TextView txtTest = (TextView) findViewById(R.id.txtOut); so you have a null pointer exception. Also, define the reset() method outside the onCreate().
See the codes below:
TextView txtTest = null;
CheckBox chkTest = null;
Button btnTest = null;
EditText etTest = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtTest = (TextView) findViewById(R.id.txtOut);
chkTest = (CheckBox) findViewById(R.id.chkTest);
btnTest = (Button) findViewById(R.id.btnTest);
etTest = (EditText) findViewById(R.id.etTest);
....
}
void reset(String text) {
chkTest.setChecked(false);
txtTest.setText(text);
}
You can't find a view when is not created yet, try this:
package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView txtTest = null;
CheckBox chkTest = null;
Button btnTest = null;
EditText etTest = null;
String text = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtTest = (TextView) findViewById(R.id.txtOut);
chkTest = (CheckBox) findViewById(R.id.chkTest);
btnTest = (Button) findViewById(R.id.btnTest);
etTest = (EditText) findViewById(R.id.etTest);
chkTest.setChecked(true);
chkTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (chkTest.isChecked()) {
txtTest.setText("Checked :)");
} else txtTest.setText("Not Checked");
}
});
btnTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//etTest.getText().toString(text);
text = etTest.toString();
// Reset(text);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void Reset(String text) {
chkTest.setChecked(false);
txtTest.setText(text);
}
}
You cant cause the items your are initializing is on the activity_main.xml and you set the content view after the initialization. Do the initialization in the onCreate().
Firstly you must define the Reset() function outside the onCreate(). Thats not the correct way.
Secondly, I observe you have just started out this so I would suggest to define the views in the xml and then act upon them. You can set the checkbox true initially in the xml and the later set to false in the code.
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