My App Crashes When I press A Button In Android Studio - java

I am creating my first app which is when I press a button I simply move to the next page, but every time I press the button my app crashes (Ignore the username and password texts they don't do anything yet). I am trying to move from login to home page.
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient"
tools:context=".Login">
<ImageView
android:id="#+id/imageView"
android:layout_width="120dp"
android:layout_height="150dp"
android:layout_marginTop="59dp"
app:srcCompat="#drawable/logo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:id="#+id/username"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:background="#11000000"
android:drawableLeft="#drawable/ic_action_user"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName"
android:textSize="16dp" />
<EditText
android:id="#+id/password"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_below="#id/username"
android:layout_alignStart="#id/username"
android:layout_alignLeft="#id/username"
android:layout_marginTop="38dp"
android:background="#11000000"
android:drawableLeft="#drawable/ic_action_pass"
android:ems="10"
android:hint="Password"
android:inputType="textPersonName"
android:textSize="16sp" />
<Button
android:id="#+id/login"
android:layout_width="321dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="207dp"
android:background="#color/colorAccent"
android:onClick="loginButton"
android:text="Login" />
<TextView
android:id="#+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="139dp"
android:text="Register Here"
android:textColor="#fff"
android:textSize="14sp" />
<TextView
android:id="#+id/attempts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="74dp"
android:text="Incorrect Attempts: "
android:textColor="#fff"
android:textSize="14sp" />
</RelativeLayout>
Login.java
package com.example.chorehelpers;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class Login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void onClick (View v){
switch (v.getId()){
case R.id.login:
loginButton(v);
break;
}
}
public void loginButton(View v) {
Intent i = new Intent (Login.this, HomePage.class);
startActivity(i);
}
}
activity_home_page.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient"
tools:context=".HomePage">
</RelativeLayout>
HomePage.java
package com.example.chorehelpers;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class HomePage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
}
}
Please Help.

why are u using this method
public void onClick (View v){
switch (v.getId()){
case R.id.login:
loginButton(v);
break;
}
remove it and rerun the app , Hope it woks

**Please try to update Login.java class like this,**
package com.example.chorehelpers;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class Login extends AppCompatActivity {
private Button loginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginButton = (Button) findViewById(R.id.login);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent x = new Intent(LoginActivity.this, HomePageActivity.class);
startActivity(x);
}
});
}
}

Related

Why I can't run the two activities together? the two activities and the image shows that I reached MainActivity directly without clicking homebutton

knowing that there is no errors, only MainActivity shows without homebutton................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
MainActivity.java
package com.example.AppCalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText etfirstvalue,etsecondvalue;
Button btnadd,btnsubs,btnmultiply,btndivide;
Double num1,num2;
TextView tvresult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etfirstvalue=findViewById(R.id.etfirstvalue);
etsecondvalue=findViewById(R.id.etsecondvalue);
btnadd=findViewById(R.id.btnadd);
btndivide=findViewById(R.id.btndivision);
btnmultiply=findViewById(R.id.btnmultiply);
btnsubs=findViewById(R.id.btnsubs);
tvresult=findViewById(R.id.tvresult);
Clicklistener();
}
public void Clicklistener(){
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num1=Double.parseDouble(etfirstvalue.getText().toString());
num2=Double.parseDouble(etsecondvalue.getText().toString());
Double result=num1+num2;
tvresult.setText(String.valueOf(result));
}
});
btnsubs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num1=Double.parseDouble(etfirstvalue.getText().toString());
num2=Double.parseDouble(etsecondvalue.getText().toString());
Double result=num1-num2;
tvresult.setText(String.valueOf(result));
}
});
btnmultiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
num1=Double.parseDouble(etfirstvalue.getText().toString());
num2=Double.parseDouble(etsecondvalue.getText().toString());
Double result=num1*num2;
tvresult.setText(String.valueOf(result));
}
});
btndivide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
num1=Double.parseDouble(etfirstvalue.getText().toString());
num2=Double.parseDouble(etsecondvalue.getText().toString());
Double result=num1/num2;
tvresult.setText(String.valueOf(result));
}
});
}
}
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:backgroundTint="#FFFFFF"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="Simple Calculator"
android:textSize="25sp" />
<EditText
android:id="#+id/etfirstvalue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter First Value"
android:inputType="number" />
<EditText
android:id="#+id/etsecondvalue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter Second Value"
android:inputType="number" />
<TextView
android:id="#+id/tvresult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="Result"
android:textColor="#color/purple_500"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginHorizontal="10dp"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/btnadd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="ADD" />
<Button
android:id="#+id/btnsubs"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="Subs" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginHorizontal="10dp"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/btnmultiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="Multiply" />
<Button
android:id="#+id/btndivision"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="Divide" />
</LinearLayout>
</LinearLayout>
homebutton.java
package com.example.AppCalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class homebutton extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homebutton);
Button btncalc=findViewById(R.id.btncalc);
btncalc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(homebutton.this, MainActivity.class);
startActivity(intent);
}
});
}
}
homebutton.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".homebutton"
tools:ignore="ExtraText">
<Button
android:id="#+id/btncalc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="Calculator" />
</LinearLayout>
You need to clarify your question more. However I believe you meant homebutton should start first and after you click calculate it should take you to the second one.
If that's the case, then you need to define your start activity from the AndroidManifest because currently your app is starting on MainActivity.java

how do I add a reset button for my two TextViews in Android Studio

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="#+id/away"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="#string/away"
android:gravity="center"
android:textSize="20sp"/>
<TextView
android:id="#+id/score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:gravity="center"
android:text="#string/_0"
android:textSize="20sp" />
<Button
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="50dp"
android:onClick="score1"
android:text="#string/score" />
<TextView
android:id="#+id/home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:text="#string/home"
android:textSize="20sp"
android:gravity="center"/>
<TextView
android:id="#+id/score2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="50dp"
android:text="#string/_0"
android:textSize="20sp" />
<Button
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="score2"
android:text="#string/score" />
<Button
android:id="#+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/reset"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:textColor="#ff1100"/>
</LinearLayout>
I searched it on multiple websites but I still can't the answer. I also tried using the__setText("")it
didn't work as well as the the if() method. I have also tried the intent method also doesn't seem to
work, and many other codes , please help me because I am stuck at this bit , I know that it is simple but
I just can't find it. Thank you
The following codes will set the TextView score to blank when Button one is clicked
First approach
To use the android:onClick="score1" in your XML
Add this to your activity
TextView mTvScore1 = (TextView) findViewById(R.id.score)
public void score1(View v) {
mTvScore1.setText("");
}
Second approach
Button mBtn = (Button) findViewById(R.id.one)
TextView mTvScore1 = (TextView) findViewById(R.id.score)
mBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTvScore1.setText("");
}
});
Activity code
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView mTvScore1 = (TextView) findViewById(R.id.score);
Button mBtn = (Button) findViewById(R.id.one);
mBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
score1(v);
}
});
}
public void score1(View v) {
mTvScore1.setText("");
}
}

My App Closes on a button click

Help Me Please to find the error
Please when i click the button my application close
emp.XML
**XML file*
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/lawa"
tools:context="com.example.johnwalls.projet.Main2Activity">
<EditText
android:id="#+id/username"
android:layout_width="150dp"
android:layout_height="30dp"
android:ems="10"
android:inputType="textPersonName"
android:textSize="10dp"
android:background="#ffffffff"
android:textColor="#000"
android:textColorHighlight="#ffffffff"
android:phoneNumber="false"
android:text=""
style="#android:style/TextAppearance.Large"
android:textStyle="bold"
android:typeface="sans"
android:layout_marginBottom="90dp"
android:layout_alignBottom="#+id/pass"
android:layout_alignLeft="#+id/pass"
android:layout_alignStart="#+id/pass" />
android:text="" />
<EditText
android:id="#+id/pass"
android:layout_width="150dp"
android:layout_height="30dp"
android:ems="10"
android:inputType="textPassword"
android:textSize="10dp"
android:background="#ffffffff"
android:textColor="#000"
android:textColorHighlight="#ffffffff"
android:phoneNumber="false"
android:text=""
style="#android:style/TextAppearance.Large"
android:textStyle="bold"
android:typeface="sans"
android:layout_marginLeft="11dp"
android:layout_marginStart="11dp"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/log"
android:layout_alignStart="#+id/log" />
<Button
android:text=""
android:layout_width="105.0dp"
android:layout_height="40.0dp"
android:id="#+id/log"
android:background="#drawable/log"
android:layout_marginLeft="10dp"
android:layout_marginTop="340dp"
android:textAllCaps="false"
android:onClick=""
/>
<Button
android:text=""
android:layout_width="105.0dp"
android:layout_height="40.0dp"
android:background="#drawable/ins"
android:layout_marginLeft="120dp"
android:layout_marginTop="340dp"
android:textAllCaps="false"
android:onClick="goButtonClicked"
android:id="#+id/goButton"
/>
</RelativeLayout>`
Main2Activity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emp);
}
public void goButtonClicked(View v) {
Intent i=new Intent(Main2Activity.this,SaveadminActivity.class);
startActivity(i);
}
}
SaveadminActivity
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class SaveadminActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.enregistreradmin);
}
}
[enter image description here][1]
What are you trying to achieve
what are you expecting to get out
What did you get out (include error messages)
What else have you tried?
What do you think is causing it?
Why do you need to make a new question for it? Why is your problem different to other, similar questions on here?
This is how you can solve the issue
emp.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/lawa"
tools:context="com.example.johnwalls.projet.Main2Activity">
<EditText
android:id="#+id/username"
android:layout_width="150dp"
android:layout_height="30dp"
android:ems="10"
android:inputType="textPersonName"
android:textSize="10dp"
android:background="#ffffffff"
android:textColor="#000"
android:textColorHighlight="#ffffffff"
android:phoneNumber="false"
android:text=""
style="#android:style/TextAppearance.Large"
android:textStyle="bold"
android:typeface="sans"
android:layout_marginBottom="90dp"
android:layout_alignBottom="#+id/pass"
android:layout_alignLeft="#+id/pass"
android:layout_alignStart="#+id/pass" />
android:text="" />
<EditText
android:id="#+id/pass"
android:layout_width="150dp"
android:layout_height="30dp"
android:ems="10"
android:inputType="textPassword"
android:textSize="10dp"
android:background="#ffffffff"
android:textColor="#000"
android:textColorHighlight="#ffffffff"
android:phoneNumber="false"
android:text=""
style="#android:style/TextAppearance.Large"
android:textStyle="bold"
android:typeface="sans"
android:layout_marginLeft="11dp"
android:layout_marginStart="11dp"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/log"
android:layout_alignStart="#+id/log" />
<Button
android:text=""
android:layout_width="105.0dp"
android:layout_height="40.0dp"
android:id="#+id/log"
android:background="#drawable/log"
android:layout_marginLeft="10dp"
android:layout_marginTop="340dp"
android:textAllCaps="false"
/>
<Button
android:text=""
android:layout_width="105.0dp"
android:layout_height="40.0dp"
android:background="#drawable/ins"
android:layout_marginLeft="120dp"
android:layout_marginTop="340dp"
android:textAllCaps="false"
android:id="#+id/goButton"
/>
</RelativeLayout>
Main2Activity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class Main2Activity extends AppCompatActivity {
Button goButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emp);
goButton=(Button)findViewById(R.id.goButton);
goButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Main2Activity.this,SaveadminActivity.class);
startActivity(i);
}
});
}
}
In Main2Activity delete goButton method and add to onCreate this
goButton=(Button)findViewById(R.id.goButton);
goButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Main2Activity.this,SaveadminActivity.class);
startActivity(i);
}
});
Main2Activity will looks like this
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class Main2Activity extends AppCompatActivity {
Button goButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emp);
goButton=(Button)findViewById(R.id.goButton);
goButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Main2Activity.this,SaveadminActivity.class);
startActivity(i);
}
});
}

Unfortunately, your app stopped working

I honestly do not know what is wrong with my code. I have searched for answers here and have done everything and i simply cannot tell what is wrong, Please help?
This is my MainActivity.java,
package com.example.myname.easternmusic;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
TimerTask task=new TimerTask() {
#Override
public void run() {
finish();
startActivity(new Intent(MainActivity.this, Main.class));
}
};
Timer opening=new Timer();
opening.schedule(task,5000);
}
}
My splash.xml which is the layout for the MainActivity.java
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sounds of the East"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:textSize="20dp"
android:textColor="#000000"
android:textStyle="bold"
android:gravity="center_horizontal"
android:background="#drawable/bell"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
My Main.java which is the extra class I created for the main_activity.xml file
package com.example.myname.easternmusic;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Main extends Activity {
Button btBamboo, btPalace;
MediaPlayer mpBamboo, mpPalace;
int playing;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView=(ImageView)findViewById(R.id.imageView);
ImageView imageView1=(ImageView)findViewById(R.id.imageView2);
btBamboo=(Button)findViewById(R.id.button1);
btPalace=(Button)findViewById(R.id.button2);
btBamboo.setOnClickListener(bBamboo);
btPalace.setOnClickListener(bPalace);
mpBamboo=new MediaPlayer();
mpBamboo=MediaPlayer.create(this,R.raw.bamboo);
mpPalace=new MediaPlayer();
mpPalace=MediaPlayer.create(this,R.raw.palace);
playing=0;
}
Button.OnClickListener bBamboo=new Button.OnClickListener(){
#Override
public void onClick(View v) {
switch (playing){
case 0:
mpBamboo.start();
playing=1;
btBamboo.setText("Pause Bamboo Song");
btPalace.setVisibility(View.INVISIBLE);
break;
case 1:
mpBamboo.pause();
playing=0;
btBamboo.setText("Play Bamboo Song");
btPalace.setVisibility(View.VISIBLE);
break;
}
}
};
Button.OnClickListener bPalace=new Button.OnClickListener(){
#Override
public void onClick(View v) {
switch (playing){
case 0:
mpPalace.start();
playing=1;
btPalace.setText("Pause Palace Song");
btBamboo.setVisibility(View.INVISIBLE);
break;
case 1:
mpPalace.pause();
playing=0;
btPalace.setText("Play Palace Song");
btBamboo.setVisibility(View.VISIBLE);
break;
}
}
};
}
and finally, the activity_main.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".Splash">
<ImageView
android:layout_width="320dp"
android:layout_height="150dp"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:src="#drawable/band" />
<Button
android:layout_width="320dp"
android:layout_height="wrap_content"
android:text="Play Bamboo Song"
android:id="#+id/button1"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:textSize="22dp"
android:layout_marginBottom="10dp" />
<ImageView
android:layout_width="320dp"
android:layout_height="150dp"
android:id="#+id/imageView2"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:src="#drawable/drums" />
<Button
android:layout_width="320dp"
android:layout_height="wrap_content"
android:text="Play Palace Song"
android:id="#+id/button2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textSize="22dp"
android:layout_marginBottom="10dp"
android:layout_alignParentStart="false" />
When the app is run, it should pull up a splash screen with the splash layout for five seconds, and then display the activity_main layout, but it stops right after it switches screens. Please help.
write
startActivity(new Intent(MainActivity.this, Main.class));
then write
finish();
here You first finish your activity and after that you are try to call that activity. That is not possible.

How to call object from other class to another class and add it?

I'm new for android java coding. I'm try to do menu list where just have tick box, and once tick the items, n press next, it should go to view layout and show items and the total of the item selected, then press next it should open details page where user must put their details n press send button to send via email. I don't know how to call the items from menu to Cart and to confirmation class.
This is menu.java .
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MenuActivity extends Activity {
Button btnorder;
Button btnback;
Button btnlinkcart;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
btnlinkcart = (Button) findViewById(R.id.button1);
btnback = (Button) findViewById(R.id.button2);
// back button click event
btnback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MenuActivity.this, MainActivity.class);
startActivity(intent);
}
});
// Link to Cart Screen
btnlinkcart.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
ViewActivity.class);
startActivity(i);
finish();
}
});
}
}
This is Cart.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ViewActivity extends Activity {
Button btnconfirm;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cart);
btnconfirm = (Button) findViewById(R.id.button1);
// Link to Cart Screen
btnconfirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
ConfirmActivity.class);
startActivity(i);
finish();
}
});
}
}
This is cart.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/wallpaper" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Confirm" />
</RelativeLayout>
This is menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/wallpaper"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Next" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Back" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="28dp"
android:text="Pizza (Large) RM30.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox1"
android:layout_marginTop="20dp"
android:text="Pizza (Mediume) RM20.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox2"
android:layout_marginTop="20dp"
android:text="Pizza (Personal) RM10.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox3"
android:layout_marginTop="20dp"
android:text="Chicken Wings RM12.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox4"
android:layout_marginTop="19dp"
android:text="Garlic Bread RM6.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox5"
android:layout_marginTop="20dp"
android:text="Soft Drink (Large) RM5.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox6"
android:layout_marginTop="19dp"
android:text="Soft Drink (Medium) RM4.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Make the variable you want to get in the menu class static, then in the cart class you can get them like this:
menu.variableFromMenuClass
public static ArrayList<YourObject> selectItemList= new ArrayList<YourObject>();
on selectItem(){
//each Item you add here
selectedList.add(selectedObject);
}
on disselectItem(){
//remove if exist in list
selectedList.remove(disselectedOject);
}
sendemail(){
send your public static selecteditem list.
}
This is the algorithm you have to write simple.

Categories