fragment error upon starting new activity - android

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;
}
}
}

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

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;
}
}

Android buttons' onClickLIstener() only works once when the Activity begins, not when returning from Fragment transaction

Any time the main Activity is started or restarted (i.e. by pressing the home button on my action bar), all of the buttons (which reside in a fragment in this main activity) work correctly. However, if I press one of the buttons, then press the back button, the home fragment and its buttons reappears but none of the button callbacks work. The button images continue to change appearance when pressed, as always.
The callbacks are assigned in the onCreateView method of the home fragment, as follows:
public class HomeFragment extends SherlockFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Button button = (Button) v.findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.d(TAG, "Button!");
mActivity.getSupportFragmentManager().beginTransaction()
.replace(R.id.contFragMain, new SomeFragment())
.addToBackStack(null)
.commit();
}
});
}
Here is the superclass of Fragments I launch with the buttons:
public abstract class RouteListFragment extends SherlockFragment {
private static final String TAG = RouteListFragment.class.getName();
private Activity mActivity; // parent activity
RouteListAdapter mAdapter;
ListView mListViewRouteList;
public RouteListFragment() {
super();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = getActivity();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle bndl = getArguments(); // data passed should contain search term
View v = inflater.inflate(R.layout.fragment_route_list, container, false);
mListViewRouteList = (ListView) v.findViewById(R.id.listViewRouteList);
addRoutesToList(); //Populates the list with data via an async DB call
return v;
}
You shouldn't be stashing the Activity reference; the Activity is probably getting recreated at some point (depends on how your flow is), but you should either reassign the reference in onAttach (Activity activity), or just call getActivity() to get the currently Activity you're attached to.
So mActivity.getSupportFragmentManager()...
should instead be
getActivity().getSupportFragmentManager()...
EDIT: Wait, maybe that's not right. What is v in onCreateView()? You're not creating any UI in this Fragment? You should be using onViewCreated() to get a reference to the views in the created hierarchy.

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;
}
}

onClick inside fragment called on Activity

i am encapsulating stuff into a fragment at the moment and run into a problem that is hard to google.
Inside my fragment are some buttons with onClick attributes but they are called on the Activity rather the fragment from the android system - this makes encapsulating a bit clumsy. Is there a way to have the reflection stuff from onClick to call on the fragment? The only solution to this I see at the moment is not to use onClick in the xml and set click-listeners inside the fragment via code.
I spoke to some googlers # #adl2011 - they recognize the problem and perhaps there will be a fix of that in the future. Until then - one should use .setOnClick in the Fragment.
The problem is that when layout's are inflated it is still the hosting Activity that is receiving the button clicks, not the individual Fragments.
I prefer using the following solution for handling onClick events. This works for Activity and Fragments as well.
public class StartFragment extends Fragment implements OnClickListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_start, container, false);
Button b = (Button) v.findViewById(R.id.StartButton);
b.setOnClickListener(this);
return v;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.StartButton:
...
break;
}
}
}
Then problem is gone.
It works for me
Add import:
import android.view.View.OnClickListener;
Fragment.java
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = (ViewGroup) inflater.inflate(R.layout.fragment, container, false);
Button mButton = (Button) rootView.findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
return rootView;
}
ISSUE:
1.In XML onClick attribute will call activity's public method.
2.Fragment's public method not called.
3.Fragment reusability.
SOLUTION:
In Fragment's layout add this to the View.
android:onClick="onFragmentViewClick"
In each activity the fragment may belong..
public void onFragmentViewClick(View v) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container);
if (fragment != null && fragment.isVisible()) {
if (fragment instanceof SomeFragment) {
((SomeFragment) fragment).onViewClicked(v);
}
}
}
In the Fragment include this method..
public void onViewClicked(View v) {
switch (v.getId()) {
case R.id.view_id:
Log.e("onViewClicked", "yehhh!!");
break;
}
}
You could have the listener that is being called in the activity forward the call onto a listener in the fragment. You should have a reference to the fragment inside of the FragmentActivity to pass the call on. You will have to cast to call the method or have your fragment implement an interface you define. I know that isn't the best solution but it will work. You could also use the tag of a button to specify the method name to call if you wanted. Hope this helps a bit.
Try this...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.activity_ipadditional_users, container, false);
FloatingActionButton button7 = (FloatingActionButton) rootView.findViewById(R.id.fab_ip_additional_user);
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), IPAdditionalUsersEdit.class);
startActivity(intent);
}
});
return rootView;

Categories

Resources