Why this program is not working in Android Virtual Device? - android

WelcomeActivity:
package com.emdad.androidapp;
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.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
public class WelcomeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
findViewById(R.id.androidButton).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(WelcomeActivity.this, GameActivity.class);
startActivity(intent);
}
});
setContentView(R.layout.activity_welcome);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.welcome, 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);
}
}
GameActivity:
package com.emdad.androidapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class GameActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game, 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);
}
}
When it was run in Android Virtual Device, it replied "Unfortunately, it has stopped." What is the problem?

You should move
setContentView(R.layout.activity_welcome);
before
findViewById(R.id.androidButton).setOnClickListener(new OnClickListener() {
Corrected:
setContentView(R.layout.activity_welcome);
findViewById(R.id.androidButton).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(WelcomeActivity.this, GameActivity.class);
startActivity(intent);
}
});

Related

app chrashing on imageview rotation

Hi guys i am new on android developing and i am trying to rotate an imageview.
The problem comes when (obviously) i push the button and the app close itself
Code java:
package comad.exampleaaaa.vittoriodaadntico.bottiglia3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity6 extends AppCompatActivity {
public ImageView bottiglia;
public Button gira;
#Override
protected void onCreate(Bundle savedInstanceState) {
gira=(Button)findViewById(R.id.btn_roll);
bottiglia=(ImageView)findViewById(R.id.bottiglia);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
}
public void rollOnClick(View v){
RotateAnimation anim=new RotateAnimation(0,360,20,20,20,20);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);
bottiglia.startAnimation(anim);
}
#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_activity6, 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);
}
}
error log :
http://imgur.com/H2Phsvf
You call methods in wrong order:
#Override
protected void onCreate(Bundle savedInstanceState) {
gira=(Button)findViewById(R.id.btn_roll); // This
bottiglia=(ImageView)findViewById(R.id.bottiglia); // And this
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6); // Should come after this
}
Here you search for views before you set content view. You should set your view, and only then search for widgets. Otherwise there is no view in which your widgets could be found, so both gira and bottiglia are nulls:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
gira=(Button)findViewById(R.id.btn_roll);
bottiglia=(ImageView)findViewById(R.id.bottiglia);
}

how to slide down the action bar in android to display something like coversation

i want to display the conversation between me and admin when i dragged the action bar. since i was new to android programming i dont know how to slide the action bar also. the following code is a decoy(dummy) code, coz its not letting me to post the question without sample code. please help me to drag or slide down the action bar to display conversation. thank you.
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt=(Button)findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
{
}
}
#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);
}
}

error in android studio package android.veiw does not exist

1 it showing a error package android.veiw does not exist in my android studio
2 when i add a buuton and use onClick
package com.example.batman.buttoncheck;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.veiw.Veiw;
import android.view.MenuItem;
public class Button_MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button__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.menu_button__main, menu);
return true;
}
public void buttononClick(Veiw v){
//do nothing
}
#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);
}
}
Typo.
Change
import android.veiw.Veiw
to
import android.view.View
And similarly
public void buttononClick(Veiw v)
to
public void buttononClick(View v)

Create a button than send the user to the main page

I am new with Android Studio world. I made a two activity pages. The first button in the first page send the user to the second page which it works fine
public void change(View v){
setContentView(R.layout.activity_main_activity2);
but I made the same button in the second page to send the user the main page but no luck. Please see the code and let me know why I got this error.
public void HomePage(View v){
setContentView(R.layout.activity_main_activity);
This is the first activity page
package com.example.lenovo;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends ActionBarActivity {
private Menu menu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.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);
}
public void change(View v){
setContentView(R.layout.activity_main_activity2);
}
}
and this is the second activity
package com.example.lenovo;
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;
import static com.chapter2.example.lenovo.chapter2.R.layout;
public class MainActivity2 extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main_activity2);
Button mButton = (Button) findViewById(R.id.button2);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent mIntent = new Intent(MainActivity2.this,
MainActivity.class);
startActivity(mIntent);
}
});
}
#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_activity2, 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 HomePage (View v){
setContentView(R.layout.activity_main);
}
}
Currently you are switching layouts of Activity instead of changing Activity. this not right way to switch between Activities. use startActivity method with intent to start next Activity on Button click:
Start MainActivity2 from MainActivity:
public void change(View v){
Intent intent=new Intent(v.getContext(),MainActivity2.class);
startActivity(intent);
}
Start MainActivity2 from MainActivity:
public void HomePage(View v){
Intent intent=new Intent(v.getContext(),MainActivity.class);
startActivity(intent);
}
and make sure to have added both Activities in AndroidManifest.xml
If you want use inbuilt device back button then simply put
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
}
and if you want to go back by button click put this line
bbsubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
onBackPressed();
}
});
}
In the first activity you are not launching the mainactivity2.class when you click the button, instead you are just changing the content view.
and the R.id.button2 is not registered in the first activity.
So it is not working
You have to send user first page to second page using Intent
You need to add onClick tag in that button in activity_main layout like this
android:onClick="change"
in activity_main_activity2 add onClick tag in that button like this
android:onClick="HomePage"
In MainActivity2 in setContentView change like this
setContentView(R.layout.activity_main_activity2);
In first activity do like this
public void change(View v){
Intent mIntent = new Intent(MainActivity.this,MainActivity2.class);
startActivity(mIntent);
}
In second activity do like this
public void HomePage(View v){
Intent mIntent = new Intent(MainActivity2.this,MainActivity.class);
startActivity(mIntent);
}
Don't forget to declare those Activities in manifest

How can i switch activity using Button?

How can i switch activity using Button? this is my problem:
package net.example.finals;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
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.AdapterView;
import android.widget.Button;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#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 implements OnClickListener {
Button bq;
Button bw;
Button be;
Button br;
Button bt;
Button by;
Button bu;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_second,
container, false);
bq=(Button)rootView.findViewById(R.id.btn1);
bq.setOnClickListener(this);
bw=(Button)rootView.findViewById(R.id.btn2);
bw.setOnClickListener(this);
be=(Button)rootView.findViewById(R.id.btn3);
be.setOnClickListener(this);
br=(Button)rootView.findViewById(R.id.btn4);
br.setOnClickListener(this);
bt=(Button)rootView.findViewById(R.id.btn5);
bt.setOnClickListener(this);
by=(Button)rootView.findViewById(R.id.btn6);
by.setOnClickListener(this);
bu=(Button)rootView.findViewById(R.id.btn7);
bu.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//i dont know what to put here
case R.id.btn1:
if(bq.callOnClick()==true){
Intent myOwnIntent = new Intent(getActivity(), Second.class);
startActivity(myOwnIntent);
}
break;
}
}
}
Why did you use "if(bq.callOnClick()==true)" this?
Android use a lots of java code, first you need to learn java properly.
AND Google too
#Override
public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.btn1:
Intent myOwnIntent = new Intent(YourActivityName.this, Second.class);
startActivity(myOwnIntent);
break;
}
}
Try this one.
How to start new activity on button click
Start another activity by clicking a button
Android eclipse how to start a new activity on button click

Categories

Resources