How to open another activity from an activity? - java

My problem is the following.
my app has a 1 welcome screen where the user ckick the "continue" button and it goes to next screen. The next one contains a menu with several buttons.
my problem is that I can not open another activity on the second screen (on the first screen it opens normal)
more or less this scheme below
(| activity1> button continue | >> | activity2> button continue2 |> does not respond)
to compliment and test apk on a galaxy grand duos 4.2.2
code below
code 1 screen (welcome).
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button button7 = (Button) findViewById(R.id.button7);
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_main4);
}
});}}
code 2 tela
public class Main4Activity extends AppCompatActivity {
private Button prova;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
prova = (Button) findViewById(R.id.button5);
prova.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent( Main4Activity.this, Main3Activity.class);
startActivity(intent);
}
});
}}
2 tela code xml button
<Button
android:id="#+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button4"
android:layout_marginTop="11dp"
android:text="tela 2"/>

First, I want to make sure that you understand what you are writing.
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_main4);
}
});
In your onclick, you only set your view of Main2Activity to screen of activity_main4.xml. But you are still standing in Main2Activity (It means you are in Main2Activity with view activity_main4.xml).
In this case, Main4Activity hasn't initialized and the button prova hasn't been initialized too. So when you press prova button, it won't do anything.
Second, to solve your problem, make Main4Activity be initialized, you must start it. So, instead of using:
setContentView(R.layout.activity_main4);
in Main2Activity, which only change the view, not the Activity. You should use
Intent intent = new Intent(Main2Activity.this, Main4Activity.class);
startActivity(intent);
Hope you can understand this!

Your problem is that you can't use setContentView(R.layout.activity_main4); to open another activity .You can use startActivity method to open another activity .
You can try this .
1.remove the code in your Main2Activity
setContentView(R.layout.activity_main4);
2.add change to this
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i=new Intent(Main2Activity.this,Main4Activity.this);
startActivity(i);
}
});}}

Related

setOnClickListener start another activity

I'm trying to start another activity by pressing on the cardview which has a friend finder id. But when I write home.java it gives me problems in the setOnClickListener. At homeActivity it tells me Cannot resolve method 'homeActivity' in 'HomeActivity'. because?
public class HomeActivity extends AppCompatActivity {
private CardView btn_home;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_home = findViewById(androidx.appcompat.R.id.home);
btn_home.setOnClickListener(v -> homeActivity(new Intent(HomeActivity.this, TrovamicoActivity.class)));
}
btn_home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(HomeActivity.this, TrovamicoActivity.class);
startActivity(intent);
}
});
If there is no code in the manifest, write it
<activity android:name=".TrovamicoActivity" />

How to open new activity that contain RecyclerView and CardView?

I created a project that use RecyclerView and CardView (for PointOfInterest). These 5 activities are relate to each other :
PointOfInterest.java
PlacesAdapter.java
Places.java
layout_poi.xml
activity_point_of_interest.xml
Meanwhile in activity_main.xml I design the Main Menu together with some buttons. One of the button named Rapid Penang (id: rapid_btn). I call an activity of Rapid Penang (from MainActivity.java) like below:
public class MainActivity extends AppCompatActivity {
private Button button_for_rapid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to call Rapid Penang class
button_for_rapid = (Button) findViewById(R.id.rapid_btn);
button_for_rapid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openRapid();
}
});
}
public void openRapid()
{
Intent intent_rapid = new Intent(this, RapidPenang.class);
startActivity(intent_rapid);
}
}
RapidPenang consist of only one activity and it is success. But when I try to do exactly the same to PointOfInterest activites (as mention above), suddenly the app were crashed.
This is how I try to open PointOfInterest activites from a button in MainMenu called Point Of Interest:
private Button button_for_poi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to call Point Of Interest class
button_for_poi = (Button) findViewById(R.id.poi_btn);
button_for_poi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openPOI();
}
});
}
public void openPOI()
{
Intent intent_poi = new Intent(this, PointOfInterest.class);
Intent intent_poi2 = new Intent(this, PlacesAdapter.class);
Intent intent_poi3 = new Intent(this, Places.class);
startActivity(intent_poi);
}
Firstly check your activity is define in Android manifest file and then call
StartActivity(new Intent(getApplicationcontext(),RapidPenang.class));
That's it

Incorrect transition between activities

Probably something I do not understand.
In the application programs (from google play), if we move from the main activity to the second and then back to the main, then after pressing the "back" button on the phone, the application closes.
I tried to make my own applications with two activities, but it did not work as it should. When the main activity goes to the second and then I go back to the main, then after pressing the "back" button on the phone, the application instead of closing, it goes back to the second activity, then back to the main and it just closes.
What am I doing wrong ?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Act2Butt = (Button) findViewById(R.id.Act2Butt);
Act2Butt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
});
}
.
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button Act1Butt = (Button) findViewById(R.id.Act1Butt);
Act1Butt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main2Activity.this, MainActivity.class );
startActivity(intent);
}
});
}
}
To go back to MainActivity from Main2Activity, you need to call onBackPressed() or finish(), instead of startActivity.

Buttons interfering with each other on Android Studio

I am trying to make a button on my homepage of an app that will lead to a search page, that will have a handful more buttons leading to other pages. However, I used the same code from my activity main for the button in my second page (seachpage) and now when I run the code, my first button on my main page, when clicked it just shuts the app down. I don't even know how to approach this properly because I copied the same code, just changed the "findViewById" and the "startActivity" accordingly with their new labels. Any recommendation or help would be massively appreciated!
Activity main Java code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button yourButton = (Button) findViewById(R.id.TranslateButton);
if (yourButton == null) throw new AssertionError();
yourButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SearchPage.class));
}
});
}
}
Activity main xml for the button:
Secondary page (searchpage) Java code:
public class SearchPage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_page);
Button accommodationButton = (Button) findViewById(R.id.accommodationButton);
if (accommodationButton == null) throw new AssertionError();
accommodationButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(SearchPage.this, Accommodation.class));
}
});
}
}
Secondary page xml for the button:
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="#string/accommodation"
android:id="#+id/accommodationButton"
android:layout_below="#+id/search_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_weight="1"/>
Thank you again for taking the time and consideration to read and/or respond to my question~!

Why unable to start new activity when click on the rate design button

When I click on the rate design button it stops, I doubt is my manifest because I have check it already below are my codes. I have also enter the error log. Can someone help me instead of rating down for this question
Your forget to add
setContentView(R.layout.yourlayout);
before Button initialization in onCreate(....)
Correct:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
Button btnRating = (Button) findViewById(R.id.rd);
btnRating.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(NearablesDemoActivity.this, MainActivityRating.class);
startActivity(intent);
}
});
}

Categories