App is not getting deployed - java

I've started coding in android. While running my app, I'm getting message "Unfortunately, myapp is stopped".
I've attached the code below.
Can you tell me what's wrong with this code?
The code is given below :
package in.developer.rjsharma.myfirstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText fno = (EditText) findViewById(R.id.fno);
String no1=fno.getText().toString();
int first=Integer.parseInt(no1);
EditText sno = (EditText) findViewById(R.id.sno);
String no2=fno.getText().toString();
int second=Integer.parseInt(no2);
final int r1=first+second;
final int r2=first-second;
final int r3=first*second;
final int r4=first/second;
Button add = (Button) findViewById(R.id.add);
Button subtract = (Button) findViewById(R.id.subtract);
Button multiply = (Button) findViewById(R.id.multiply);
Button divide = (Button) findViewById(R.id.divide);
final TextView solution = (TextView) findViewById(R.id.solution);
add.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(r1);
}
});
subtract.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(r2);
}
});
multiply.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(r3);
}
});
divide.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(r4);
}
});
}
}

You are not allowed to set Integer in Textview. You have to convert Integer to String
package in.developer.rjsharma.myfirstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText fno = (EditText) findViewById(R.id.fno);
String no1=fno.getText().toString();
int first=Integer.parseInt(no1);
EditText sno = (EditText) findViewById(R.id.sno);
String no2=fno.getText().toString();
int second=Integer.parseInt(no2);
final int r1=first+second;
final int r2=first-second;
final int r3=first*second;
final int r4=first/second;
Button add = (Button) findViewById(R.id.add);
Button subtract = (Button) findViewById(R.id.subtract);
Button multiply = (Button) findViewById(R.id.multiply);
Button divide = (Button) findViewById(R.id.divide);
final TextView solution = (TextView) findViewById(R.id.solution);
add.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(String.valueOf(r1));
}
});
subtract.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(String.valueOf(r2));
}
});
multiply.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(String.valueOf(r3));
}
});
divide.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(String.valueOf(r4));
}
});
}
}

You are making some mistakes in the code. You are reading the values of EditText fno in the onCreate(), it has just been created, their value would be an empty String. If you parse that to a value, I don't know if it will be 0 or null.
However, I would try it this way (not tested - likely to be errors with solution or other variables):
public class MainActivity extends AppCompatActivity {
private EditText fno, sno;
private TextView solution;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText fno = (EditText) findViewById(R.id.fno);
EditText sno = (EditText) findViewById(R.id.sno);
Button add = (Button) findViewById(R.id.add);
Button subtract = (Button) findViewById(R.id.subtract);
Button multiply = (Button) findViewById(R.id.multiply);
Button divide = (Button) findViewById(R.id.divide);
TextView solution = (TextView) findViewById(R.id.solution);
add.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
solution.setText(readIntegerFromEditText(fno)+readIntegerFromEditText(sno));
}
});
subtract.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
solution.setText(readIntegerFromEditText(fno)-readIntegerFromEditText(sno));
}
});
multiply.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
solution.setText(readIntegerFromEditText(fno)*readIntegerFromEditText(sno));
}
});
divide.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
solution.setText(readIntegerFromEditText(fno)/readIntegerFromEditText(sno));
}
});
}
private int readIntegerFromEditText (EditText editText){
int no = Integer.parseInt(editText.getText());
return no;
}
}
There are a lot of things to be done yet, like checking for 0 division. You will find out on the way, if you use the LogCat and Debugging messages.

Related

There is no error in the code but the button doesn't perform any action

I am developing an login and register app. The app is supposed to check if the entered data is correct or not while login or register. The login validation works fine but register doesn't validate data. The register button is supposed to check validations after clicking but nothing happens when I click it. There is no error shown in android studio and the app runs fine.
package com.example.investas;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class RegisterActivity extends AppCompatActivity {
private EditText inputUsername,inputEmail,inputPassword,inputCpassword;
Button btnRegister;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
TextView btn=findViewById(R.id.alreadyHaveAccount);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView btn=findViewById(R.id.alreadyHaveAccount);
inputUsername=findViewById(R.id.inputUsername);
inputEmail=findViewById(R.id.inputEmail);
inputPassword=findViewById(R.id.inputPassword);
inputCpassword=findViewById(R.id.inputCpassword);
btnRegister=findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
validations();
}
});
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
}
});
}
});
}
private void validations() {
String username=inputUsername.getText().toString();
String email=inputEmail.getText().toString();
String password=inputPassword.getText().toString();
String cpassword=inputCpassword.getText().toString();
if(username.isEmpty() || username.length()<7)
{
showError(inputUsername,"Your username is not valid");
}
else if(email.isEmpty() || !email.contains("#"))
{
showError(inputEmail,"Email is not valid");
}
else if(password.isEmpty() || password.length()<7)
{
showError(inputPassword,"Password must be 7 characters");
}
else if(cpassword.isEmpty() || !cpassword.equals(password))
{
showError(inputCpassword,"Passwords are not matching");
}
else
{
Toast.makeText(getApplicationContext(),"Registration
Successful",Toast.LENGTH_SHORT).show();
}
}
private void showError(EditText input, String your_username_is_not_valid) {
input.setError(your_username_is_not_valid);
input.requestFocus();
}
}
Try to move the following lines out of the OnClickListener, they should be in the onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//...
inputUsername = findViewById(R.id.inputUsername);
inputEmail = findViewById(R.id.inputEmail);
inputPassword = findViewById(R.id.inputPassword);
inputCpassword = findViewById(R.id.inputCpassword);
btnRegister = findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
validations();
}
});
// ...
}
Doing it this way is necessary for it to be cleaner and to work.
private EditText inputUsername,inputEmail,inputPassword,inputCpassword;
Button btnRegister;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
TextView btn=findViewById(R.id.alreadyHaveAccount);
inputUsername=findViewById(R.id.inputUsername);
inputEmail=findViewById(R.id.inputEmail);
inputPassword=findViewById(R.id.inputPassword);
inputCpassword=findViewById(R.id.inputCpassword);
btnRegister=findViewById(R.id.btnRegister);
TextView btn=findViewById(R.id.alreadyHaveAccount);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
}
});
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
validations();
}
});
}

Android Webview doesn't load the Url from button listener

It loads the site but only when it's not in the Action Listener.
Even if I load the url way down in the onCreate method it works.
I already checked if the Webview is invisible, the button listener works also.
The problem began after a few hours of coding other things
WebView wbvCheckURL;
Button btnSearch;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wbvCheckURL = findViewById(R.id.wbvCheckURL);
btnSearch = findViewById(R.id.btnSearch);
wbvCheckURL.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
...
}
#Override
public void onReceivedTitle(WebView view, String title) {
...
}
});
wbvCheckURL.getSettings().setJavaScriptEnabled(true);
wbvCheckURL.setWebViewClient(new WebViewClient());
// When i do it like this it works:
// wbvCheckURL.loadUrl("http://www.google.de");
btnSearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
wbvCheckURL.loadUrl("http://www.google.de");
hideKeyboard(v);
LoadCode();
}
});
}
The Solution is to load it in a Thread
Create instance and start the thread:
LoadURL t = new LoadURL();
t.start();
Thread class:
class LoadURL extends Thread {
public void run() {
Classname.wbvCheckURL.post(new Runnable() {
public void run() {
Classname.wbvCheckURL.loadUrl("http://www.google.de");
}
});
}
you can try mine. Mine is another kind of webview with Drawer Layout. Hope you get the idea on how to insert the webview.
package com.example.appname;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.ShareActionProvider;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.MenuItemCompat;
public class Provokinc extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_provokinc);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://stackoverflow.com/questions/63438455/android-webview-doesnt-load-the-url-from-button-listener");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
ImageButton ImageButton = (ImageButton) findViewById(R.id.imageButtonA);
ImageButton ImageButton1 = (ImageButton) findViewById(R.id.imageButtonB);
ImageButton ImageButton2 = (ImageButton) findViewById(R.id.imageButtonC);
ImageButton ImageButton3 = (ImageButton) findViewById(R.id.imageButtonD);
ImageButton ImageButton4 = (ImageButton) findViewById(R.id.imageButtonE);
ImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent int1 = new Intent(Provokinc.this,Main3Activity.class);
startActivity(int1);
}
});
ImageButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent int2 = new Intent(Provokinc.this, Main2Activity.class);
startActivity(int2);
}
});
ImageButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent int3 = new Intent(Provokinc.this, Provokinc.class);
startActivity(int3);
}
});
ImageButton3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent int4 = new Intent(Provokinc.this, ActivityItemDetails.class);
startActivity(int4);
}
});
ImageButton4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent int5 = new Intent(Provokinc.this, MainActivity.class);
startActivity(int5);
}
});
}
#Override
public void onBackPressed(){
if(webView.canGoBack()){
webView.goBack();
} else{
super.onBackPressed();
}
}
}

Error: setOnClickListener cannot be resolved

I encountered an issue and couldn't resolve in Android Studio. The setOnClickListener remains red and doesn't work unless I get rid of my "loseStarter1" button name.
Note: Starter1 is a button, I'm trying to make it disappear when clicked by the user. My real code starts when I introduce the loseStarter1 button.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
}
Button loseStarter1;
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
})
}
Much appreciated.
You're missing a semicolon to end the new View.OnClickListener() { ... statement as well as that block not being inside of a method.
Not only move this code into the onCreate method, make sure you end it with a semicolon.
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
}); // Add the semicolon here
It should look like this:
public class game1 extends AppCompatActivity {
Button loseStarter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
}); //added semicolon
} // ends onCreate method
} // ends class
Your Button variable declaration and OnClickListener initialization is outside of the onCreate() method. Use the following code:
package com.cutting_edge_tech.mentalenhancementapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity {
private Button loseStarter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
});
}
}
Move below code inside onCreate()
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
});
This is how i do it.
1) first make the class implement the interface View.OnClickListener, this let you handle the button clic event:
public class game1 extends AppCompatActivity implements View.OnClickListener;
2) second create the interface method to handle event.
#Override
public void onClick(View view)
{
if(view.getId()==R.id.Starter1)
{
view.setVisibility(View.GONE);
}
}
3) OnCreate methods is the best way to find objects and set properties.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1= (Button) findViewById(R.id.Starter1);
if(loseStarter1!=null){
loseStarter1.setOnClickListener(this);
}
}
all code :
package com.cutting_edge_tech.mentalenhancementapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity implements View.OnClickListener {
private Button loseStarter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
if(loseStarter1!=null){
loseStarter1.setOnClickListener(this);
}
}
#Override
public void onClick(View view)
{
if(view.getId()==R.id.Starter1)
{
view.setVisibility(View.GONE);
}
}
}
Advice:
Rename class to Game1.

Intents issue in eclipse android

I have an issue with intents. I use it in main activity and call another activity but on click its call again main activity. Here is my code
Main activity.java
package com.umair.facebook;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
ImageView img1;
TextView txt, txt3;
Button like, share, update;
EditText et1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img1 = (ImageView) findViewById(R.id.img);
txt = (TextView) findViewById(R.id.name);
txt3 = (TextView) findViewById(R.id.status);
like = (Button) findViewById(R.id.Like);
share = (Button) findViewById(R.id.Share);
update = (Button) findViewById(R.id.Comment);
et1 = (EditText) findViewById(R.id.editText1);
like.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
// TODO Auto-generated method stub
like.setText("Liked");
}
});
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
share.setText("Shared");
}
});
update.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub startGame();
}
});
}
private void startGame() {
// TODO Auto-generated method stub
Intent lanuchGame = new Intent(MainActivity.this, Result.class);
startActivity(lanuchGame);
}
}
Result.java (Second Activity)
package com.umair.facebook;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class Result extends MainActivity {
ImageView img;
TextView txt1, txt2;
Button btn1, btn2,btn3;
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setContentView(R.layout.result_1);
super.onCreate(savedInstanceState);
img =(ImageView)findViewById(R.id.img1);
txt1= (TextView)findViewById(R.id.nam);
txt2= (TextView)findViewById(R.id.stats);
btn1=(Button)findViewById(R.id.Liked);
btn2=(Button)findViewById(R.id.Shared);
et=(EditText)findViewById(R.id.editText); } }
Call super.onCreate(savedInstanceState) before setContentView() in your second Activity.

Android: Why isn't my button playing its sound effect?

The first button works fine, but when I click to the second, nothing happens. I'm not getting any errors, I can't see what's wrong.
Sounds.java
package com.andrew.finnandjake;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Sounds extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
}
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);
Button b2 = (Button)findViewById(R.id.Button2);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
}
}
You can't randomly create a method like you're doing with onCreate1, it's never executed by you or by your Activity.
This is all you need:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(this);
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);
Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
Button b2 = (Button)findViewById(R.id.Button2);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
}
You aren't assigning the onclick listener inside the actual OnCreate. This should work:
package com.andrew.finnandjake;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Sounds extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);
Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
Button b2 = (Button)findViewById(R.id.Button2);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
}

Categories