Make an onclicklistener in SherlockFragment - java

Hello I want to make a click listener in a sherlockfragment but every time I click it, it displays "view.class source not found" it doesn't matter what action I do I want to use it to start an intent to change an activity
here is my code:
public class SafanTab extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.safantab, container, false);
}
public void onOverClick(View view) {
Intent myIntent = new Intent(view.getContext(), OverSafan.class);
startActivityForResult(myIntent, 0);
}
public void nProductenClick(View view) {
Intent myIntent = new Intent(view.getContext(), Over_Safan.class);
startActivityForResult(myIntent, 0);
}
public void onTwitterClick(View view) {
Intent myIntent = new Intent(view.getContext(), Over_Safan.class);
startActivityForResult(myIntent, 0);
}
}
I define the listener to the button in the layout xml:
android:onClick="OnOverClick"
I tried it a long time and also searched the internet for "View.class source not found" but can't find anything to solve my problem. As you see in the question I'm using ActionBarSherlock.

You won't get the onOverClick callback in the fragment. Use something like the following in your onActivityCreated() method.
// Handle onclick
getView().findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// do something
}
});
See this question for more options: How to handle button clicks using the XML onClick within Fragments
EDIT:
For onCreateView you can do the following:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_layout, container, false);
view.findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// do something
}
});
return view;
}

Not sure if this is the problem, but I see that the name of your method is defined as:
public void onOverClick(View view)
but in the XML you specify:
android:onClick="OnOverClick"
Letter "O" is capitalized in xml.

Related

Trying to open activity from button press in fragment. Nothing happens

Tried to open a fragment from a button in a fragment. Wasnt able to get this to work so decided just to make it an activity. Tried doing this but still am not able to open the activity on the button press.
public class ZonnepaneelLayout extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.zonnepaneel_layout, container, false);
Button button2 = (Button)view.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), AddZonnepaneel.class);
startActivity(i);
}
});
getActivity().setTitle("Zonnepaneel");
return view;
}
}
Activity
public class AddZonnepaneel extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.add_zonnepaneel_layout);
}
}
Try using the getContext() instead of the getActivity()
Intent intent = new Intent(getContext(), AddZonnepaneel.class);
startActivity(i);
First of all, your variable View can't start with uppercase letter, set it to lowercase :)
Try to use
getActivity().startActivity(i);
getActivity().finish();

When you open a new activity in a fragment the app stops

The problem that Teno I want to use an intro slide in my app through fragments and the third fragment I want to put a button that leads to another activity and in Android studio the code does not make any error but when I run the app and click the app button It stops what is it?
public ThirdFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_third, container, false);
viewPager = getActivity().findViewById(R.id.viewPager);
back1 = view.findViewById(R.id.slideThereback);
back1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewPager.setCurrentItem(1);
}
});
//The TextView "Done " is the one I want to click on the Take me to another activity and that up to now gives me an error to run in the emulator
done = view.findViewById(R.id.Done);
done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(getActivity(), MenuP.class);
startActivity(myIntent);
}
});
return view;
}
I hope I can provide the code or say my error to open a new activity in a fragment and not close the app to Ejecutarce on a mobile device
Use the below code in your onClick() method.
Intent myIntent = new Intent(getActivity(), MenuP.class);
getActivity().startActivity(myIntent);
Check that R.id.Done does exists in R.layout.fragment_third. if it doesn't exist then done View might be null.

How to switch to two fragments of the same activity using intent in android java

i have two fragments of the same activity(navigation drawer) i added a button on one among them to switch to another but i am getting an error.i don't know how this works . please help me
public class HomeFragment extends Fragment {
public HomeFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
SwitchBtn= (AppCompatButton) rootView.findViewById(R.id.btnswitch);
// Register Button Click event
SwitchBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(getActivity(),
FriendsFragment.class);
getActivity().startActivity(intent);
}
});
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onDetach() {
super.onDetach();
}
}
You're switching between fragments, not starting a new activity.
Replace this
Intent intent = new Intent(getActivity(),
FriendsFragment.class);
getActivity().startActivity(intent);
With this
getFragmentManager().beginTransaction()
.replace(R.id.your_container_id, new FriendsFragment())
.commit()
You can't open a fragment as such. What you need to do is either create them on the same activity, use a 'screen' variable that controls display by using the .setVisible on your components.
Or, what you can do is put your fragments on different activities using intent.
private Content C = getApplicationContext();
Intent myIntent = new Intent(C,yourActivity.class);
C.startActivity(myIntent);

SlidingMenu in Child Activities Navigation Android

I am trying to add left side slide menu inside my application but i am facing little trouble getting the slide menu to the following child activities of the fragment.
So far i have tried using this sliding menu example and when i got this image now inside the "Find People" fragment class i have static data.
Find People (Parent Activity) on Button Click go to ---> Friends (Child Activity of Find People) on Button Click go to ----> Names (Child Activity of Friends)
I am trying to get the same sliding menu with the Child Activities also that i failed to achieve so far please can any one help me out
public class FindPeopleFragment extends Fragment {
public FindPeopleFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
Button btn = (Button) rootView.findViewById(R.id.butto);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Friends.class);
startActivity(intent);
}
});
return rootView;
}
}
Friends (Child Activity)
public class Friends extends Activity {
Button btnNext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.friends_xml);
btnNext = (Button)findViewById(R.id.btnNexts);
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Go to Names Child Activity
}
});
}
}
And Similarly Names (Child Activity)
How do i get the same slide menu in both the child activities (Friends & Names). Any idea ??
Plz help me out in this.
Code Snippet will be much helpful.
Thanks
As #Piyush Gupta said you need to call new Fragment instead of calling activities,
In Place of activity you need to use Fragment and move from one Fragment to next used this code
Instead of using Fragment element use Framelayout id of the fragment Xml
Replace ur code with this
public class FindPeopleFragment extends Fragment {
public FindPeopleFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
Button btn = (Button) rootView.findViewById(R.id.butto);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragementDemo fd = new FragementDemo();
android.support.v4.app.FragmentTransaction ft =
getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fd); // content_frame is your FrameLayout container
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
});
return rootView;
}
}
And your next child activity will extend Fragment not Activity like this :
public class Friends extends SherlockFragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragementdemo, container, false);
// use button click code as used in FindPeopleFragment
return rootView;
}
}
Hope it works fine now for you!!

Open Another Activity Using Fragment Button

Okay so I've tried two types of code to get this to work and it keeps giving me force closes when I press the button to go into another Activity. I'm using a Fragment and there's a button in that Fragments code but I can't seem to get it to work. I'm not an experienced Android developer but I'm trying my best to learn.
Here's the Java code:
1st Method
public class About extends Fragment {
Intent intent;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.about, container, false);
intent = new Intent(getActivity(), Contact_Developer.class);
final Button button = (Button) rootView.findViewById(R.id.btnContactDev);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
return rootView;
}
}
2nd Method
public class About extends Fragment {
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.about, container, false);
Button button = (Button) rootView.findViewById(R.id.btnContactDev);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getActivity(), Contact_Developer.class);
getActivity().startActivity(intent);
}
});
return rootView;
}
}
I really don't know what's going on and why I'm getting force closes but if anyone could help me and explain a little what I did wrong that'd be more than enough
Do not handle the onClick for the fragment button in the Fragment. Let it go it's parent activity. And start the activity from the parent activity.
To make sure that the button onClick event is sent to the parent activity, make sure, in your about.xml, for the button with id btnContactDev, you have the following parameter:
<Button android:id="#+id/btnContactDev"
android:onClick="buttonClick"
...
/>
and in your parent activity (parent of About fragment), you have:
public void buttonClick(View v) {
switch(v.getId()) {
case R.id.btnContactDev:
Intent myIntent = new Intent();
myIntent.setClassName(your_package_name_string, your_activity_name_string);
// for ex: your package name can be "com.example"
// your activity name will be "com.example.Contact_Developer"
startActivity(myIntent);
break;
}
}
HTH.
PS: This solution is very specific for what your requirement. In general, it's best to handle the onClick events related to the fragment inside the fragment class.
PS: Yes, as the other solution says, make sure you have registered the Contact_Developer Activity in your Manifest file.
Have you declared the proper activity in the AndroidManifest.xml? Each activity other then the first should be declared inside the application tag, like this:
<activity
android:name=".Contact_Developer"
android:label="#string/app_name" >
</activity>
If it's not found, it will give force close.
I think the issue here is that the activity is not ready.
I would not recommend you following harikris's solution even though it works. It's usually bad practice to put code (onClick() which handles the click event) in your XML file. For example, it will be very difficult to hunt that piece of code down to review/ change it in the future yourself, not to mention another developer in your team.
I'd suggest to listen to the event the activity is ready i.e. onActivityCreated() within your Fragment class. Override the implementation of the same method and call something in your activity e.g. onFragmentReady() like in the example below.
Fragment class:
public class About extends Fragment {
Intent intent;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.about, container, false);
intent = new Intent(getActivity(), Contact_Developer.class);
final Button button = (Button) rootView.findViewById(R.id.btnContactDev);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
return rootView;
}
#Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((MyActivity) this.getActivity()).onFragmentReady();
}
}
Activity class:
public class MyActivity extends FragmentActivity {
private void onFragmentReady() {
Log.i(TAG, "testing");
intent = new Intent(getActivity(), Contact_Developer.class);
final Button button = (Button) rootView.findViewById(R.id.btnContactDev);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
}
public class About extends Fragment {
...
}
}
I think you should use getContext() or getApplicationContext() instead of getActivity()
so code will be
Intent intent =new Intent(getContext(),Contact_Developer.class);
startActivity(intent);

Categories