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));
Related
I'm trying to make a new activity from a fragment that has 2 buttons. The button showed up on the emulator, but when I clicked it it doesn't do anything. I've followed many tutorials online but most of them do onClickListener on a single button. Sorry I'm new to android development.
public class UserFragment extends Fragment implements View.OnClickListener {
Activity context = getActivity();
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_user, container, false);
Button register = v.findViewById(R.id.btnAkunRegister);
Button login = v.findViewById(R.id.btnAkunLogin);
return v;
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnAkunLogin:
Intent moveIntent = new Intent(context, LoginActivity.class);
startActivity(moveIntent);
break;
case R.id.btnAkunRegister:
moveIntent = new Intent(context, RegisterActivity.class);
startActivity(moveIntent);
break;
}
}
}
update: I've tried following the answer from stackoverflow, but it doesn't solve the problem.
You should set onClickListener for your buttons. You implement this interface in you
Frgamnet, so you should set this as listener in your onCreateView() method.
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_user, container, false);
Button register = v.findViewById(R.id.btnAkunRegister);
Button login = v.findViewById(R.id.btnAkunLogin);
//set listener
register.setOnClickListener(this);
login.setOnClickListener(this);
return v;
}
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));
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!!!
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;
}