I am new to android programming on eclipse and just doing it as a hobby...I have made and run 2 apps successfully and so their is no question of me not knowing how to run the app...i have restarted my pc but the problem sill persists..my 2 apps are still working on pressing the run button and i am using a device to run them(not an AVD) but the third app is not working...in fact their is no response..their are no errors in code but 3 lint warnings..the program is very simple(taking 2 numbers from user and displaying the sum)...Please help.MY code is as following:
package com.example.app3;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.*;
import android.view.View.*;
import android.widget.*;
public class MainActivityapp3 extends ActionBarActivity {
int num1,num2,sum;
Button button1;
EditText textf1,textf2;
TextView textv3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activityapp3);
textf1=(EditText)this.findViewById(R.id.editText1) ;
textf2=(EditText)this.findViewById(R.id.editText2);
textv3=(TextView)this.findViewById(R.id.textView3);
button1=(Button)this.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
num1 = Integer.parseInt(textf1.getText().toString());
num2 = Integer.parseInt(textf2.getText().toString());
sum = num1 + num2;
textv3.setText("sum" + "");
}
});
}
#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_activityapp3, 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);
}
}
Would really need to see the output of logcat. You can find it in the DDMS tabs of Eclipse.
Anyway, without more information I would ask: did you remember changing the activity name in AndroidManifest?
Related
I am just getting started towards android and i am having trouble with my hello world app, The problem is first it said R cannot resolved which i was able to overcome because there was no R.java files which i later copied from the internet
now there are three errors
1. activity_main cannot be resolved or is not a field
2. menu cannot be resolved or is not a field
3. action_settings cannot be resolved or is not a field
I have already tries control+shift+o,
already tried clean and build.
package my.hello;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#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);
}
}strong text
You should not create or modify R.java file manually as it's autogenerated by build system. Try to remove one copied from Internet and just rebuild your project with layout and menu files located in appropriated directories of the res folder
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.
I am a newbie android app developer, and I wanted to challenge myself by making an app where a user input is taken, converted into integer, and entered into the Fibonacci formula. The result should show all the fibonacci numbers, looping the formula a user-given number of times.
So far, I think I have isolated the problem to the ContentViewer, but then again any help would be greatly appreciated. BTW I'm using Eclipse Juno if that helps
MainActivity.java
package com.example.myfirstapp;
//importing the API for this app
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
//I based this on the first app I learnt (as I haven't done much app development so far)
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
//this method creates a default layout when a "new app" is started
#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);
}
//This method sends the information of the user input to a second activity screen,
//through the use of intents
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
The input goes into a second activity screen called DisplayMessageActivity.java
package com.example.myfirstapp;
//import all the relevant API for this app
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
//main class extending Activity superclass
public class DisplayMessageActivity extends Activity {
//This main method is where the fibonacci formula is worked out the result is shown
//For some reason, the ContentViewer is not working for me.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
int messages = Integer.parseInt(message);
int[] fib = new int[messages + 1];
fib[0] = 1;
fib[1] = 1;
for (int i=2; i <= messages; i++)
{
fib[i] = fib[i-1] + fib[i-2];
}
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(60);
StringBuilder builder = new StringBuilder();
for (int j : fib)
{
builder.append(j);
}
String text = builder.toString();
textView.setText(text);
}
// Set the text view as the activity layout
setContentView(textView);
#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 error message I'm getting is
DisplayMessageActivity.java
activity_display_message cannot be resolved or is not a field (Line 18)
Return type for this method is missing (Line 46)
Syntax error on token "textView", VariableDeclaratorId expected after this token (Line 46)
textView cannot be resolved to a type (Line 46)
Hope that's enough information. Funnily enough, I was following the developer.android.com tutorial on making the first app, with tweaks to make it do Fibonacci.
Many thanks.
Edit: Thanks for your quick response. I checked and there was no activity_display_message.xml so I just created that and copied everything from the activity_main.xml. I also moved the textviewer to inside the onCreate method. Now I'm just tidying up everything so thanks for relieving me from another sleepless night.
you have apparently not created activity_display_message (or another less likely possibility is you are referring to a file that is not in the layouts folder). You are setting the view to a file that does not exist. Also from the code, I can see that you are putting in setContentView() in some random place. It should be within the body of a method (preferably within onCreate() in your case).
the setContentView(textView); is out side the method i mean it is a global make sure it is with in onCreate method
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.
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!");