This question already has answers here:
How do I start an activity from within a Fragment? [duplicate]
(5 answers)
Closed 7 years ago.
I'm trying to start a new Tab1City activity, as I click on a button from Tab1Discover, which is a fragment.
I've try several combinaisons if parameters, looking on stackoverflow, but it keeps compiling and making the app crash at launch, with a :
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at Tab1Discover.onCreateView(Tab1Discover.java:32)
public class Tab1Discover extends Fragment {
#InjectView(R.id.buttonLille)
Button _loginButton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
_loginButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// Start the Signup activity
Intent intent = new Intent(getActivity(), Tab1City.class);
startActivity(intent);
}
});
return inflater.inflate(R.layout.tab_1_disc1_main, container, false);
}
}
Edit:
I've noticed (thanks to the comment) that you're using RoboGuice. After a quick search, I've come up that the reference will only be available in onViewCreated, so move your code there:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab_1_disc1_main, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
_loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Start the Signup activity
Intent intent = new Intent(getActivity(), Tab1City.class);
startActivity(intent);
}
});
}
From your usage of '#InjectView', i'm guessing you're using ButterKnife 6.0.x.
You're missing the part of ButterKnife where you have to tell it WHEN to inject the views.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_1_disc1_main, container, false);
ButterKnife.inject(this, rootView);
_loginButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// Start the Signup activity
Intent intent = new Intent(getActivity(), Tab1City.class);
startActivity(intent);
}
});
return rootView;
}
With this code, you're telling ButterKnife where to find the view so it can set up the bindings. From here on, you can simply add all the #InjectView you want, and it'll take care of the bindings automatically.
Thanks a lot, that's now working :
public class Tab1Discover extends Fragment {
#InjectView(R.id.buttonLille)
Button _lilleButton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_1_disc1_main, container, false);
ButterKnife.inject(this, rootView);
_lilleButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// Start the Signup activity
Intent intent = new Intent(getActivity(), Tab1City.class);
startActivity(intent);
}
});
return rootView;
}
Related
Hey guys so i'm trying to get my imagebutton to work within my fragment. What do I need to change? error: no suitable constructor found
public class HomeFragment extends Fragment {
Button myproductButton;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.home_fragment, container, false);
myproductButton = myproductButton.findViewById(R.id.imageButton2);
myproductButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent intent = new Intent(HomeFragment.this, myProducts.class);
startActivity(intent);
}
});
}
}
you should get inflate your layout first then find Your Button
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.home_fragment, container, false);// inflate layout first
myproductButton = view.findViewById(R.id.imageButton2);// here is how to get button
myproductButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), myProducts.class); // you should pass activity as context
startActivity(intent);
}
});
return view;
}
I am trying to call an activity from a non-activity class (simple class).
I created a tabbed activity. then I created a layout for fragments. and created a separate class in which I am doing some validation and calling an activity. but I am unable to start the activity from that non-activity class.
this is the function in which i am having problem
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.guarding_services_layout, container, false);
p_guard_srvc = p_guard_srvc.findViewById(R.id.guard_booknow_btn);
p_guard_srvc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//this line, i am unable to write the correct method
Intent i = new Intent( GuardingScreen.this , HomeScreen.class);
}
});
return rootView;
}
try this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.guarding_services_layout, container, false);
p_guard_srvc = p_guard_srvc.findViewById(R.id.guard_booknow_btn);
p_guard_srvc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent( getActivity() , HomeScreen.class);
getActivity().startActivity(i);
}
});
return rootView;
}
You should pass activity context to your non-activity class, and start your activity using context. for example: you can pass activity context in Non-Activity class's constructor or you can make a setter method for context.
context.startActivity(new Intent(context,YourClassName.class));
I have added a floating share button in home_fragment.xml. But I don't know where to start on how to add functionality to that share button. Please help.
This is the code of fragment java file, I have tried do coding but I failed, I'll be glad if anyone can help.
public class HomeFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, null);
}
}
You can use a View.OnClickListener to invoke a callback method when the button is clicked. See https://developer.android.com/reference/android/view/View.OnClickListener for more information.
Like #kAliert already answered, you have to get an instance of the View firstand there have been plenty of similiar qustions and answers that could've helped.
Apologies in advance for any styling or grammar mistakes
Here is an example of how to do this in a Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View homeFragmentView= inflater.inflate(R.layout.fragment_home, container, false);
FloatingActionButton animationDetailShare= homeFragmentView.findViewById(R.id.animation_detail_share);
btnCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//The logic for the button
}
});
return homeFragmentView;
}
Get an instance of your view and find the button. It isnĀ“t that hard and i guess its answered plenty of times already.
public class HomeFragment extends Fragment {
Button share_bt;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView= inflater.inflate(fragment_home, container, false);
FloatingActionButton share_bt= rootView.findViewById(R.id.share_bt);
share_bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(Intent.ACTION_SEND);
myIntent.setType("text/plain");
String shareBody = "hu" ;
String shareSub = "mk";
myIntent.putExtra(Intent.EXTRA_SUBJECT,shareBody);
myIntent.putExtra(Intent.EXTRA_TEXT,shareSub);
startActivity(Intent.createChooser(myIntent, "Share Using"));
}
});
return rootView;
}
}
this is my code of my third tab in which i have put a button to intent from this current fragment to my MapsActivity, but as soon as i click my button, the app stops running.
This is my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_tab3, container, false);
mapbtn = (Button) v.findViewById(R.id.mapButton);
mapbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), MapsActivity.class);
startActivity(intent);
}
});
return v;
}
Please someone help!!!
When i use onClickListener on a Button inside a Fragment and use intent to open an Activity, it gives NullPointerException.
This is my code
public class TabFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tabs, container, false);
Button tab1=(Button) getView().findViewById(R.id.tab1upgradeCost);
tab1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), MainActivity.class);
getActivity().startActivity(i);
}
});
return rootView;
}
}
Your getting NLP because tab1 is not initialized with right view
Don't use
Button tab1=(Button) getView().findViewById(R.id.tab1upgradeCost);
In onCreateView() getView() will retrun null because still fragment is not initlized.
Instead use
Button tab1=(Button) rooView.findViewById(R.id.tab1upgradeCost);
I think problem is with your view that getView may return null.
replace your code with this.
I didn't test your code but may be it help.
public class TabFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tabs, container, false);
Button tab1=(Button) rootView.findViewById(R.id.tab1upgradeCost);
tab1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), MainActivity.class);
getActivity().startActivity(i);
}
});
return rootView;
}
}
Change
Button tab1=(Button) getView().findViewById(R.id.tab1upgradeCost);
to
Button tab1=(Button) rootView.findViewById(R.id.tab1upgradeCost);
startActivity(new Intent(getActivity(),Actvty_Ground.class));