My program crashes and I don't know why - java

I'm learning how to program in Java for Android and my program keeps crashing. It's suppoesed to convert from Celsius to Fahrenheit.
package com.example.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener
{
public static final int MY_CODE=2;
EditText temp;
RadioButton converttoF,converttoC,selectedtype;
RadioGroup conversion;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conversion = (RadioGroup)findViewById(R.id.conversion);
conversion.setOnCheckedChangeListener(this);
converttoF = (RadioButton)findViewById(R.id.converttoF);
converttoC = (RadioButton)findViewById(R.id.converttoC);
}
#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);
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
selectedtype = (RadioButton) findViewById(checkedId);
Intent i = new Intent(this, Activity4Result.class);
i.putExtra("temper", temp.getText().toString());
if(selectedtype.equals(converttoC))
{
Toast.makeText(this,"C chosen", Toast.LENGTH_SHORT).show();
i.putExtra("type", 'c');
startActivityForResult(i, MY_CODE);
}
else
{
Toast.makeText(this,"F chosen", Toast.LENGTH_SHORT).show();
i.putExtra("type", 'f');
startActivityForResult(i, MY_CODE);
}
}
}
My guess is that the problem is happening in the "onCheckedChanged", because it crashes when I push any RadioButton.

I assume (It would be better if you post the crash log along with your question, so that we can help you better and stop assuming) you are getting a null pointer exception on the following code:
i.putExtra("temper", temp.getText().toString());
In your code you declared temp, but it is never initialised. You need to initialise temp before using it.

Related

In android studio cannot resolve method 'findViewById'

I am joining two activity through intent
package com.smartcodeone.newapp1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
public static final String STRING_VAR = "com.smartcodeone.newapp1.HELLO_WORLD";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnMsg = (Button) findViewById(R.id.btnMsg);
btnMsg.setOnClickListener(new View.OnClickListener(){
//when user click's this function will be called
public void onClick(View v){
Intent intentvar = new Intent(getApplicationContext(),Main2Activity.class);
intentvar.putExtra(STRING_VAR,"Hello World"); //this is used to pass data to next intent
startActivity(intentvar);
}
});
}
private int findViewId(int btnMsg) {
return 0;
}
#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);
}
#Override
public android.support.v4.app.FragmentManager getSupportFragmentManager() {
return null;
}
}
It might have to do with Android Studio. Try cleaning your project and then rebuilding. If that doesn't work go to File -> Invalidate Caches/Restart ...
I get those issues sometimes as well. Let me know if that works. I tried your code and it works fine.
The issue seems to be that you defined a method named: findViewById(int button) that always return 0.
Use the Activity method instead of your own:
this.findViewById(int resourceId)
Good luck!
It seems you are implementing your own findviewbyid(). I don't know if you intend to do so.
Try removing
private int findviewbyid(int btnMsg) {
}
The ActionBarActivity's findviewbyid should resolve your button from your layout file.

Error with ArrayAdapter context in another Activity

The thing is ... In an Activity I fill some fields to popularem one ListView to be loaded into another Acitivity , has tried to show it in the same Activity and works, the error happens when I try to show it in another Acitivity , I believe It has to do with the Context.
The Activity used to populate the parameters and that works if the list is shown it is called FiltrarImoveis where this step only in the Context of ArrayAdapter .
The Activity giving problem is called DetalhesImoveis , try to pass this , DetalhesImoveis.this and keeps giving error ... Here is the Activity section of code that is giving error:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> listaImoveis = new ArrayList<String>();
listaImoveis.add("foo");
listaImoveis.add("bar");
ArrayAdapter<String> adpImovel = new ArrayAdapter<String>(DetalhesImoveis.this,android.R.layout.simple_list_item_1, listaImoveis);
lvImoveis.setAdapter(adpImovel);
}
Full code DetalhesImoveis.class:
package com.example.dhjeferson.testemenu;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class DetalhesImoveis extends AppCompatActivity {
private ListView lvImoveis;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> listaImoveis = new ArrayList<String>();
/*setContentView(R.layout.activity_detalhes_imoveis);
TextView showParsedJSON = (TextView) findViewById(R.id.json);
Intent intent = getIntent();
String output = intent.getStringExtra("output");
showParsedJSON.setText(output);*/
/*FiltrarImoveis filtrarImoveis = new FiltrarImoveis();
listaImoveis = filtrarImoveis.retornaLista();*/
lvImoveis = (ListView)findViewById(R.id.lvImoveis);
Imovel imv = new Imovel();
imv.setGenero("Genero");
imv.setFinalidade("Finalidade");
imv.setTipo("Tipo");
imv.setValor("Valor");
listaImoveis.add("foo");
listaImoveis.add("bar");
try {
ArrayAdapter<String> adpImovel = new ArrayAdapter<String>(DetalhesImoveis.this, android.R.layout.simple_list_item_1, listaImoveis);
lvImoveis.setAdapter(adpImovel);
}catch(Exception e){
Log.v("logs","ERROR CAUSED BY THE EXCEPTION LIST: "+e.getStackTrace().toString());
}
}
#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_detalhes_imoveis, 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);
}
}

Cannot resolve method ParseDouble

I'm working with java in Android Studio and trying to convert a string from a TextView into a double so it can be used to calculate some figures on a person's income.
The method ParseDouble will not allow me to run it in this way. I'm wondering why it will not compile. It simply says cannot resolve method. According to the documentation, it accepts a string as a parameter.
Here is my code currently:
package ericleeconklin.costoflivingcalculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.lang.Object;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.lang.Double;
public class EnterIncome extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_income);
}
#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_enter_income, 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);
}
public void getIncome(View income){
View myIncomeString;
myIncomeString = (TextView)findViewById(R.id.enterIncome);
Double myIncome;
myIncome = Double.ParseDouble(myIncomeString);
}
}
myIncomeString is a TextView you have to get first the text ( which is EditText) and then get toString()
Change to be :
public void getIncome(View income){
TextView myIncomeString;
myIncomeString = (TextView)findViewById(R.id.enterIncome);
Double myIncome;
myIncome = Double.parseDouble(myIncomeString.getText().toString());
}
Method is Double.parseDouble(String) not Double.ParseDouble(String).

Error with android.R

Hi I am developing an android application, nothing fancy just something small for college. Where I use R I recieve an error on the final bit. example R.id.mediAware
Any help would be greatly appreciated.
Here is my code if that helps:
package com.example.medicalapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.medicalapp.R;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img1 = (ImageView) findViewById(R.id.mediAware);
ImageView splash_image2 = (ImageView) findViewById(R.id.mediAware2);
Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fadein);
Animation fade2 = AnimationUtils.loadAnimation(this, R.anim.fadein2);
fade2.setAnimationListener(new AnimationListener() {
public void onAnimationEnd (Animation animation) {
Intent intent = new Intent(new Intent(MainActivity.this, MenuActivity.class));
startActivity(intent);
}
public void onAnimationStart(Animation animation){
}
public void onAnimationRepeat(Animation animation){
}
});
splash_image.startAnimation(fade1);
splash_image2.startAnimation(fade2);
}
#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);
}
}
I figured out my problem. I was missing the R.java file. To add the file i simply created a folder in src called gen and then cleaned the project, this created the R.java file and removed my errors, appreciate the help that was given

Multiple buttons with different layouts

I have one activity with 5 buttons. Each button should show up one activity (total 5 different activities). I tried many methods for multiple buttons but my app does not work. The first button of the activity starts its linked activity without any problem (btEtiologia and the activity is Etiology), but the second button still won't show up, and returns to the Main Activity (that is home layout). When I restart the app, I try with the second button, and third buttons and works, but the fourth button won´t show up and returns to the main activity. I have dowloaded the app on a samsung galaxy S2 and is the same than emulator. ¿Is anything wrong with my code?
Here is the java code of Inicio:
package com.example.protesis;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Inicio extends ActionBarActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inicio);
Button btInformacion = (Button) findViewById(R.id.btInformacion);
btInformacion.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(getBaseContext(), "Esta aplicación esta basada en el manejo de las infecciones de prótesis articulares de la IDSA", Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.btEtiologia).setOnClickListener(this);
findViewById(R.id.btAlgoritmo).setOnClickListener(this);
findViewById(R.id.btAntibioiv).setOnClickListener(this);
findViewById(R.id.btAntibioral).setOnClickListener(this);
findViewById(R.id.btRetirada).setOnClickListener(this);
}
public void onClick(View arg0){
// create a general intent
Intent intent = null;
// define an intent for all cases
switch(arg0.getId()){
case R.id.btEtiologia:
// Setting intent for first button
intent = new Intent(this,Etiology.class);
break;
case R.id.btAlgoritmo:
// Setting intent for second button
intent = new Intent(this,Algoritmo.class);
break;
case R.id.btAntibioiv:
// Setting intent for third button
intent = new Intent(this,Antibioiv.class);
break;
case R.id.btAntibioral:
// Setting intent for fourth button
intent = new Intent(this,Antibioral.class);
break;
case R.id.btRetirada:
// Setting intent for fourth button
intent = new Intent(this,Retirada.class);
break;
}
this.startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.inicio, 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);
}
}
The main activity code is:
package com.example.protesis;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btInicio = (Button) findViewById(R.id.btInicio);
btInicio.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Inicio.class);
startActivityForResult(intent, 0);
}
});
}
#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);
}
}
As per you code, your images are dragging the code to OutOfMemory exception. Though the images which you are using of 120 dpi, their size is double to 640*480. please change image sizes then the problem will be solved. I have changed the all the 5 image sizes and now i am able to navigate to all the screens by clicking the buttons. Make the images to around 640*480 in ldpi folder.

Categories