Error with android.R - java

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

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.

My program crashes and I don't know why

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.

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.

Android Studio compiler error: cannot find a method listed in android studio tutorial

I am new to android studio and I am reading the tutorials on i-programmer located here (http://www.i-programmer.info/programming/android/5914-android-adventures-activity-and-ui.html?start=2)
The two objects in the environment are a button and a large text widget instanced
in android studio's xml designer.
The problem: A method for setting text referenced in the tutorial is showing this error message when I try to run the code: error: cannot find symbol method setText(String)
And this error shows up in the text editor: Cannot resolve method 'setText(java.lang.String)'
Provided Source Code:
package com.example.helloworld1.helloworld1;
import android.support.v7.app.ActionBarActivity;
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);
}
#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);
}
public void onButtonClick(View v){
Button button=(Button) v;
v.setText("I've Been Clicked!"); // This is where the error happens
}
}
I think you meant to use button instead of v.
button.setText("I've Been Clicked!");

Android SDK - Changing Properties in Array List

Can anyone please help me out. I'm having problems changing my array list properties. I am more familiar with changing properties via the XML file. Here is my code:
package com.example.examproject;
import java.util.Random;
import android.os.Bundle;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.content.Intent;
public class Menu_Lists extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] myAttractions = new String[2];
myAttractions[0] = "Grocery List";
myAttractions[1] = "Go Back";
setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, myAttractions));
}
protected void onListItemClick(ListView l, View v, int position, long id) {
switch(position) {
case 0: {
startActivity(new Intent(Menu_Lists.this, MainActivity.class));
break;
}
default: {
startActivity(new Intent(Menu_Lists.this, MainActivity.class));
}
}
}
#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;
}
}
What I would like to do is to change the background color. That's my main goal. I would also like each option to change color whenever the user hovers over a specific option.
Please go easy with the terminologies as well. I am still a beginner with Android SDK.
Thanks in advance.

Categories