I'm learning android developpement, and I wrote a short and easy code, but it doesn't work.
I can't start another activity,despite many try ! Here is the code of the main activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pageaccueil);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.pageaccueil, menu);
return true;
}
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pageaccueil);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Pageaccueil.this, Devise.class);
startActivity(intent);
}
});
}
}
And the button part of the XML Layout from the first/main activity :
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="38dp"
android:text="convertisseur de devises"
/>
The second activity is "devise", and here is its code :
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Devise extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_devise);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.devise, menu);
return true;
}
}
Does anyone know How can I make the second activity to launch ? I tried many times without any success .
Thank you in advance !!
use Following in your onCreate() instead of onCreate1()
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Pageaccueil.this, Devise.class);
startActivity(intent);
}
});
}
also study the Life Cycle of Activity http://developer.android.com/training/basics/activity-lifecycle/index.html
Related
When I try to run my app on the emulator I have a tab message saying
"unfortunately,GeoQuiz has stopped working" .......
GeoQuiz is my app's name .......
here is my activity class in the QuizActivity.java file
public class QuizActivity extends ActionBarActivity {
private Button mTrueButton;
private Button mFalseButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
mTrueButton=(Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT).show();
}
});
mFalseButton=(Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
}
});
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
}
#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_quiz, 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 have no images at all in my app
and the min sdk version for this app is API 16:android 4.1 (jelly bean)
and the targeted version is API 21:android 5.0 (Lollipop)
you dont call setContentView at the right place:
you call it at the end of the onCreate method! You have to move these two lines to the beginning of the onCreate Method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
because of that you get a NullPointerException here:
mTrueButton=(Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() { // NPE
This is my First Question & I'm a new android learner,
Suddenly in my ellipse 2nd layout button is not working;
after pressing button of my first layout it working & going to second layout.
But in 2nd layout my Button is not working, I tested it very simple code. Please check
public class MainActivity extends Activity {
Button b1,b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
//setContentView(R.layout.activity_main_activity2); Need to remove
startActivity(new Intent(MainActivity.this,MainActivity2.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;
}
}
This is my 2nd activity code
public class MainActivity2 extends Activity {
EditText ed;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
ed = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button21);
button.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Working", Toast.LENGTH_SHORT).show();
}
});
}
#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_activity2, menu);
return true;
}
}
*You are simply changing the layout on first activity using setContentView(). You are not navigating from one activity to another.
Either you can register the button of layout 2 by onClicklistener after setting it on activity.
b1.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
setContentView(R.layout.activity_main_activity2);
button = (Button) findViewById(R.id.button21);
button.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),"Working",Toast.LENGTH_SHORT).show();
}
});
}
});
or you can goto your other activity
b1.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// setContentView(R.layout.activity_main_activity2); remove
startActivity(new Intent(MainActivity.this,MainActivity2.class));
}
});
i just started to use eclipse, and im trying to do my first android application. But apparently something is wrong with my syntax error , and i dont know how can i fix it or where did i do wrongly. so i hope someone here can help me. Thanks :) pardon me for my bad english.
Those underlined in red are errors but i dont know how can i fix them.
starts below here
Here is my .java
public class Category extends Activity { <---(**this sign is underlined in red**)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
Button switchButton = (Button) findViewById(R.id.button1);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cake.class);
startActivity(intent);
Button switchButton = (Button) findViewById(R.id.button2);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cookie.class);
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.category, 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 sign is underlined in red**)
};
Unfortunately you're trying to initialize your button listener in another button listener. That's wrong.
Corrected:
Button switchButton = (Button) findViewById(R.id.button1);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cake.class);
startActivity(intent);
};
});
Button switchButton2 = (Button) findViewById(R.id.button2);
switchButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cookie.class);
startActivity(intent);
};
});
I'm trying to make an ImageView open another activity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gtacheats);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.gtacheats, menu);
return true;
ImageView Image1 = (ImageView) findViewById(R.id.Image1);
Image1.setClickable(true);
Image1.setOnClickListener(new View.OnClickListener() {
public void onClick(View V) {
Intent intent = new Intent(V.getContext(), Activity2.class);
startActivityForResult(intent, 0);
}
});
}
}
Where i'm doing wrong? It say "Unreachable code"
move return true as last statement of onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.gtacheats, menu);
ImageView Image1 = (ImageView) findViewById(R.id.Image1);
Image1.setClickable(true);
Image1.setOnClickListener(new View.OnClickListener() {
public void onClick(View V) {
Intent intent = new Intent(V.getContext(), Activity2.class);
startActivityForResult(intent, 0);
}
});
return true;
}
I am a newbie.. I am trying to get text on another activity which I have created, from an activity created apriori. I could do this using onClickListener, but I am unable to get it done using onClick and setText(). The code runs well until the second activity, but when I type and send button, the code crashes. Help me!
Here's the XML file:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="#+id/tv1"
android:text="#string/hello_world" />
Here's the Java file:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.bt1);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
DisplayMessageActivity.class);
EditText et = (EditText) findViewById(R.id.et1);
String message = et.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
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.main, menu);
return true;
}
}
This code can help you
MainActivity.java
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText et=(EditText) findViewById(R.id.editText2);
Button bt=(Button) findViewById(R.id.button2);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(Main.this,DisplayMessageActivity.class);
intent.putExtra("theText", et.getText().toString());
startActivity(intent);
}
});
}
DisplayMessageActivity.java
public class DisplayMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.theme.R.layout.DisplayActivity);
TextView tv=(TextView) findViewById(com.example.theme.R.id.textView2);
tv.setText(getIntent().getExtras().getString("theText"));
}
Replace
android:layout="#+id/tv1"
with
android:id="#+id/tv1"