android - Start new activity from fragment, apk built but crashes on device - android

I'm trying to start new activity from fragment and here is my code:
public class HomePage extends Fragment {
View rootview;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.homepage_layout, container, false);
Button newPage = (Button)rootview.findViewById(R.id.view_our_products);
newPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ProductsOverview.class);
startActivity(intent);
}
});
return rootview;
}
}
There are no errors reflected by Android studio. But after I build the apk and click on this button it crashes and says "Unfortunately Sample App has stopped".

Your ProductsOverview.class is an fragment not an Activity so make that as a Activity by ProductOverview extends AppCompatActivity.
Hope this will help you

Related

How to implement button function in fragments in Android studio

FeedFragment.java
public class FeedFragment extends Fragment {
Button bt_scan;
private Object Button;
public FeedFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_feed, container, false);
bt_scan = (Button) v.findViewById(R.id.bt_scan);
bt_scan.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent MainIntent = new Intent(getActivity(), MainActivityQR.class);
startActivity(MainIntent);
/* if you want to finish the first activity then just call
finish(); */
}
});
return v;
}
}
I already tried but the camera for scanning QR Code wont come out
The use of the camera require a permission request and a declaration in the AndroidManifest file

Trouble adding Logout Button to Tab Fragment Class?

I recently made a simple app that allows users to register information, login, and logout. I want to increase the complexity of this app by adding a tabbed activity that the user sees when they login, and having the third tab contain a TextView that will log them out.
Here is what I did previously to log out in my Main Activity before adding tabs:
public class MainActivity extends AppCompatActivity{
private Button bLogout;
private Session session;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
session = new Session(this);
if(!session.loggedin()){
logout();
}
bLogout = (Button) findViewById(R.id.bLogout);
bLogout.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
logout();
}
});
}
private void logout(){
session.setLoggedin(false);
finish();
startActivity(new Intent(MainActivity.this, LoginActivity.class));
}
}
Now, I want to transfer this same concept to my Tab3 Fragment Class, but I keep getting errors. Here is the Tab3 class without errors:
public class Tab3User extends Fragment{
private TextView tvLogout;
private Session session;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab3User, container, false);
tvLogout = (TextView) rootView.findViewById(R.id.tvLogout);
return rootView;
}
}
The errors happen when I try to create a new session using this as Context, as well as in the 'startActivity' method in my logout function when I try to use 'Tab3User.this'. The onClickListener seems to be working, but I am very new to android dev so I'm sure I'm just making a mistake. Here is my attempt to add in everything:
public class Tab3User extends Fragment{
private TextView tvLogout;
private Session session;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab3User, container, false);
tvLogout = (TextView) rootView.findViewById(R.id.tvLogout);
return rootView;
//error here under the "this"
session = new Session(this);
if(!session.loggedin()){
logout();
}
tvLogout.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
logout();
}
});
}
private void logout(){
session.setLoggedin(false);
finish();
startActivity(new Intent(Tab3User.this, LoginActivity.class));
}
}
Thanks for any and all help. I've been looking online but many answers to questions like this are very ambiguous so I decided to post.
Inside a fragment you need to use getContext() / getActivity().
session = new Session(getContext());

fragment error upon starting new activity

I have trouble in starting new activity within the fragment class. Everytime, I click the specified image button it will have an error stating that it is undefined from another class. This class is the holder of the said fragment activity.
Here is the code
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
rootView = inflater.inflate(R.layout.activity_seat_plan, container, false);
button1 = (ImageButton)rootView.findViewById(R.id.ImageButton1);
return rootView;
}
Upon clicking the button
public void AssignAddress(View view){
Intent i = new Intent(getActivity(), AssignSeat.class);
startActivity(i);
}
Here's the logcat
03-15 23:27:56.842: W/dalvikvm(23000): threadid=1: thread exiting with uncaught exception (group=0x40bdc438)03-15 23:27:56.852: E/AndroidRuntime(23000): FATAL EXCEPTION: main03-15 23:27:56.852: E/AndroidRuntime(23000): java.lang.IllegalStateException: Could not find a method AssignAddress(View) in the activity class com.example.mcr.InstructorMenu for onClick handler on view class android.widget.ImageButton with id 'ImageButton1'
First, show more code; secondly, the code is complaining about the non-existent method. Here is what might help you:
In your fragment, you need to implement the click listener for your button (which MUST be in the fragment layout as well).
Inside the click method, you should then start the new activity - you don't even need a method for that.
Again, more code of your activity structure might help and the rest of your fragment.
I think you are trying to hook the AssignAddress(View view) method from the xml like this android:onClick="AssignAddress"
which will not work because after the user clicks this button the compiler will try to find the method in the activity that the fragment resides in but not in the fragment where you have declared the method. So You must remove this from the xml and change to the following
public class YourFragment extends Fragment implements
OnClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
rootView = inflater.inflate(R.layout.activity_seat_plan, container, false);
button1 = (ImageButton)rootView.findViewById(R.id.ImageButton1);
button1.setOnClickListener(this);
return rootView;
}
////your other codes
///////
//the OnClickListener
#Override
public void onClick(View v) {
Intent i=null;
switch (v.getId()) {
case R.id.ImageButton1:
i= new Intent(getActivity(), AssignSeat.class);
startActivity(i);
break;
default:
break;
}
}
}

Open a new activity with a button from a static fragment?

I have an Activity (main) with four fragments. I want to add one button to the third fragment that opens another Activity (secondary) with three fragments. When I press back I want to return to the main Activity.
I have searched for an answer, but I can't find one that works. My fragments' codes are inside the main activity, and the fragments are static because otherwise the app Force Closes when rotating to landscape mode.
I took the button code from here:
http://developer.android.com/reference/android/widget/Button.html
public class fragmentFour extends Fragment {
Intent intent = new Intent(getActivity(), musikteori_ackord.class);
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_layout1, container, false);
}
final Button button = (Button) findViewById(R.id.buttonAckord);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
}
Errors:
- Cannot resolve symbol 'setOnClickListener'
- Cannot resolve symbol 'v'
So just add a Button in the third fragment and onClick of that button, start an Intent that opens up the new desired Activity containing the other three fragments.
[EDIT]
Try this:
public class fragmentFour extends Fragment {
Intent intent;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_layout1, container, false);
intent = new Intent(getActivity(), musikteori_ackord.class);
final Button button = (Button) root.findViewById(R.id.buttonAckord);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
return root;
}
}

Unable to switch from Fragment to Activity / Activity to Fragment in Android Programming

Currently, I am mainly using Fragments to connect to Facebook.
However, for the other codes, I am using normal activites (no Fragments).
My issue now is that I wish to have a button to link from my "Home Page" to the Fragment, and from the Fragment back to my "Home Page"
I am unable to do so.
I tried to use the same code to switch between activities for this but it does not work.
Is there a way to Link Normal Activities to Fragments and Vice Versa ? Or can they only link to each other ?
This is my Code:
public class SplashFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.splash, container, false);
// return view;
Button btnNextScreen = (Button) view.findViewById(R.id.btnNextScreen);
// Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
//Listening to button event
btnNextScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Intent nextScreen = new Intent(**getApplicationContext()**, SecondScreenActivity.class);
startActivity(nextScreen);
}
});
return view;
}
}
I am getting an error at getApplicationContext().
If I change it to getActivity(), they will prompt with another error that they are expecting to switch to a Fragment, not an activity.
Thank you for your help !
Regards,
AndroidStudent
use this getActivity() if u want to navigate from fragment to activity.
Intent nextScreen = new Intent(getActivity(), SecondScreenActivity.class);
startActivity(nextScreen);
do like this:
Intent i = new Intent(getActivity(), activityname.class);
i.putExtra("key", value);
getActivity().startActivity(i);
In my case, I am also unable to find the class from fragment then I write this code and it works;
public class userprofile extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_userprofile, container, false);
final ImageView iconoptions = view.findViewById(R.id.iconoptions);
iconoptions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),com.example.miniproject. options.class));
}
});
return view;
}
}

Categories

Resources