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!!!
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;
}
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));
I have home activity which have tab control and on first tab the main activity is shown. On main tab i have buttons which will redirect to another activity.
and cannot redirect to another activity.
public class Main1 extends Fragment
{
ImageView saving, loan;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
saving = (ImageView) container.findViewById(R.id.imgSavingCollection);
loan = (ImageView) container.findViewById(R.id.imgLoanCollection);
saving.setOnClickListener((View.OnClickListener) container);
loan.setOnClickListener((View.OnClickListener) container);
View v = inflater.inflate(R.layout.activity_main1, container, false);
return v;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.imgSavingCollection:
Intent savingactivity = new Intent(v.getContext(), SavingCollection.class);
startActivity(savingactivity);
break;
}
}
}
instead of
Intent savingactivity = new Intent(v.getContext(), SavingCollection.class);
startActivity(savingactivity);
add this
Intent savingactivity = new Intent(getActivity(), SavingCollection.class);
startActivity(savingactivity );
Try to change your code like,
public class Main1 extends Fragment
{
ImageView saving, loan;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main1, container, false);
saving = (ImageView) rootView.findViewById(R.id.imgSavingCollection);
loan = (ImageView) rootView.findViewById(R.id.imgLoanCollection);
saving.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent savingactivity = new Intent(getActivity(), SavingCollection.class);
startActivity(savingactivity);
}
});
return rootView;
}
}
also check your manifest.xml
<activity android:name=".SavingCollection"/>
this may helps you
problem may be with v.getContext(), so just replace it by getActivity() when you declaring intent.
There is issue with your onCreateView, replace below code with your onCreateView
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_main1, container, false);
saving = (ImageView) v.findViewById(R.id.imgSavingCollection);
loan = (ImageView) v.findViewById(R.id.imgLoanCollection);
saving.setOnClickListener((View.OnClickListener) container);
loan.setOnClickListener((View.OnClickListener) container);
return v;
}
Use this
Intent savingactivity = new Intent(getActivity(), SavingCollection.class);
startActivity(savingactivity);
Replace v.getContext() to getActivity().
and Define your layout line upper side
View v = inflater.inflate(R.layout.activity_main1, container, false);
Full code :
View v = inflater.inflate(R.layout.activity_main1, container, false);
saving = (ImageView) container.findViewById(R.id.imgSavingCollection);
loan = (ImageView) container.findViewById(R.id.imgLoanCollection);
saving.setOnClickListener((View.OnClickListener) container);
loan.setOnClickListener((View.OnClickListener) container);
return v;
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;
}
Im getting an error when I press the button in my fragment view. The button is rendered but when I click it I get the following error:
09-23 18:29:01.242 9332-9332/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at foit.startingpoint.nl.androiddrawer.LocatiesFragment.onCreateView(LocatiesFragment.java:28)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Button button = (Button) getView().findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(LocatiesFragment.this.getActivity(), SecondActivity.class);
startActivity(myIntent); }
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_locaties, container, false);
}
your initializing is incorrect, you must do as following code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_locaties, container, false);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(getActivity(),SecondActivity.class);
startActivity(myIntent);
}
});
return view;
}
as onCreateView not finished getView is null in your code so you must create one view and use that.
You cannot simply getView() from nowhere when finding button id. First layout is inflated.
View view = inflater.inflate(R.layout.fragment_locaties, container, false);
Button button = (Button) view.findViewById(R.id.button);
...
return view;