I'm new in coding, and I was trying to make a simple app that make a subtraction from 2 variable, but when I click the button to calculate, the app crashes... Can someone help me finding the problem in the code? (Android Studio)
Here's the code:
package apps.ste.resto;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import static apps.ste.resto.R.id.risultato;
public class MainActivity extends AppCompatActivity {
TextView importo;
TextView conto;
TextView risultato;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
TextView risultato = (TextView) findViewById(R.id.textView);
TextView importo = (TextView) findViewById(R.id.editText);
TextView conto = (TextView) findViewById(R.id.editText2);
Button bottone = (Button) findViewById(R.id.button);
bottone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result();
}
});
}
public void result(){
float result = Float.parseFloat(importo.getText().toString()) - Float.parseFloat(conto.getText().toString());
risultato.setText(Float.toString(result));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
I think you are getting null value for getText()..
SO you can try this :
public void result(){
boolean digitsOnly1 = TextUtils.isDigitsOnly(importo.getText()+"");
boolean digitsOnly2 = TextUtils.isDigitsOnly(conto.getText()+"");
if(digitsOnly1 && digitsOnly2){
float result = Float.parseFloat(importo.getText().toString()) - Float.parseFloat(conto.getText().toString());
risultato.setText(result+""));
}else{
risultato.setText("Please provide inputs");
}
}
Related
Starting with line 34 I am getting error: class, interface, enum expected. This is a simple percent calculator app as my introduction to android studio. I have tried fix all all the formatting suggestion I read for other people getting the same error but I had no luck. I would appreciate any suggestions or ideas on how to fix that or also if you have any basic app ideas I would love to hear them.
package com.example.bheue.percenttutorial;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView totalTextView;
EditText percentageText;
EditText numberText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
};
TextView totalTextView = (TextView) findViewById(R.id.totalText);
percentageText = (EditText) findViewById(R.id.percentageText);
numberText = (EditText) findViewById(R.id.numberText);
Button calcButton = (Button) findViewById(R.id.percentText);
calcButton.setOnClickListener(new view.OnClickListener())
#Override
public void onClick(view view){
float percentage = Float.parseFloat(percentageText.getText().toString());
float dec = percentage / 100;
float total = dec * Float.parseFloat(numberText.getText().toString());
totalTextView.setText(Float.toString(total));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
This is what your code should be:
package com.example.bheue.percenttutorial;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.uplication.hamza.helpinghands.R;
public class MainActivity extends AppCompatActivity {
TextView totalTextView;
EditText percentageText;
EditText numberText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView totalTextView = (TextView) findViewById(R.id.totalText);
percentageText = (EditText) findViewById(R.id.percentageText);
numberText = (EditText) findViewById(R.id.numberText);
Button calcButton = (Button) findViewById(R.id.percentText);
calcButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
float percentage = Float.parseFloat(percentageText.getText().toString());
float dec = percentage / 100;
float total = dec * Float.parseFloat(numberText.getText().toString());
totalTextView.setText(Float.toString(total));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Did you copy this code from somewhere and had problems in pasting?
I am creating an Android application. Here I need to get the IP address of the Android device. Below is my MainActivity code,
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteOrder;
public class MainActivity extends AppCompatActivity {
Button button;
TextView txtView;
public static Context context = MyApplication.getAppContext();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button_ip);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
ipAddress = Integer.reverseBytes(ipAddress);
}
byte[] ipByteArray = BigInteger.valueOf(ipAddress).toByteArray();
String ipAddressString;
try {
ipAddressString = InetAddress.getByAddress(ipByteArray)
.getHostAddress();
} catch (UnknownHostException ex) {
Log.e("WI-Resource", "Unable to get host address.");
ipAddressString = null;
}
System.out.println(ipAddress);
//txtView.setText(ipAddress);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
After creating the WiFi hotspot in my device, I started the application, then it gives following exception,
Unable to get host address
So please anyone can help me to solve this problem?
try this Android: How to Know an IP Address is a Wifi IP Address?
o can read more here: https://developer.android.com/reference/java/net/NetworkInterface.html
I had added 2 spinners to my cricket scoring app and there are no compiler errors however my app keeps crashing on start. It was working fine when I added the first spinner but the moment I added the second spinner the app started to crash. Please help.
package com.example.visha.cricketcounter;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private Spinner spinner;
private Spinner spinner2;
EditText text = (EditText)findViewById(R.id.editText);
String value = text.getText().toString();
EditText text2 = (EditText)findViewById(R.id.editText2);
String value2 = text2.getText().toString();
private static final String[]paths = {"15", "20", "30"};
private final String[]paths1 = {value, value2};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,paths);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
spinner2 = (Spinner)findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,paths1);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
switch (position) {
case 0:
// Whatever you want to happen when the first item gets selected
break;
case 1:
// Whatever you want to happen when the second item gets selected
break;
case 2:
// Whatever you want to happen when the thrid item gets selected
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
You should implement OnItemSelectedListener to access the onItemSelected method.
you are passing this in method like setOnItemSelectedListener(this) means you are passing reference of listener as class but you don't implemented listener, impalement OnItemSelectedListener like following and try,
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener
Your activity must implement the interface onItemSelectedListener and override the method OnNothingSelected
Example:
public class MainActivity extends AppCompatActivity implements onItemSelectedListener{
....
Then with the Mouse Cursor in the Main Activity let your IDE auto insert all the missing method implementations
I m a beginner to android so i made app to display just simple x and y coordinates.
package company.com.bucky;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public static int X;
public static int Y;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView firsttextview = (TextView)findViewById(R.id.textView);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
firsttextview.setText("X : " + X + "\nY : " + Y);
}
public boolean onTouch(View v, MotionEvent event) {
this.Y = (int) event.getX();
this.Y = (int) event.getY();
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
SO when i run the app i just shows the value of x and y just 0..and no change in value when i touch the screen.Thx in advance for helping
Your onTouch handler only sets the static variables, but does not update the text view.
Put firsttextview.setText("X : " + X + "\nY : " + Y);
into the onTouch handler.
I'm trying to declare buttons and EditTexts in the OnCreate() function, calling the function findViewById(). But when I try to use the button in the emulator the application fails. When I use the findViewById() in the function of the treatment of the event (I used android:onClick because the treatment in java wasn't working either) the aplication worked normally. Why I can't only use the findViewById() one time, inside the onCreate?
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends Activity implements OnClickListener{
String[] names;
EditText status;
EditText par1;
EditText par2;
EditText par3;
Button b1;
Button b2;
Button b3;
Button b4;
Spinner spin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (EditText) findViewById(R.id.text);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
status.setText("HA");
}
});
Button b2 = (Button) findViewById(R.id.b2);
b2.setActivated(false);
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Display any data about the persons
}
});
Button b3 = (Button) findViewById(R.id.b3);
b3.setActivated(false);
b3.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Start the sensors lecture
}
});
Button b4 = (Button) findViewById(R.id.b4);
b4.setActivated(false);
b4.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Execute the music
}
});
}
public void treatment_button(View v){
status = (EditText) findViewById(R.id.text);
status.setText("Connect");
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
Inside oncreate() you have again define Button b1 like:
Button b1 = (Button) findViewById(R.id.b1);
Change it to:
b1 = (Button) findViewById(R.id.b1);
As you are creating the Button variable inside onCreate(),it will not work outside that method.So use the global reference variable of the Button that you have created.
try this hope it's worked:
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends Activity {
String[] names;
EditText status;
EditText par1;
EditText par2;
EditText par3;
Button b1;
Button b2;
Button b3;
Button b4;
Spinner spin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (EditText) findViewById(R.id.text);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
status.setText("HA");
}
});
b2 = (Button) findViewById(R.id.b2);
b2.setActivated(false);
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Display any data about the persons
}
});
b3 = (Button) findViewById(R.id.b3);
b3.setActivated(false);
b3.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Start the sensors lecture
}
});
b4 = (Button) findViewById(R.id.b4);
b4.setActivated(false);
b4.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Execute the music
}
});
}
public void treatment_button(View v){
status = (EditText) findViewById(R.id.text);
status.setText("Connect");
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}