calling an activity from a non-activity(simple layout) through separate class - android

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

Related

I can't make a new intent / activity from fragment that has 2 buttons

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

error link image button to fragment error: no suitable constructor found for

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

How to open Activity from Fragment using intent?

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

how can i call fragment from listadapter

Hi i am calling fragment from my custom adapter but its displaying error can any one help me here is the MyListAdapter snippet
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final TextView view = new TextView(parent.getContext());
view.setText(values.get(position));
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view1) {
// Get the position
Log.w(MyListAdapter.LOG_KEY, "MyListAdapter Lable Clicked");
Intent intent= new Intent(context, SingleItemView.class);
context.startActivity(intent);
}
});
return view;
}
and my SingleItemView.class
public class SingleItemView extends Fragment{
LinearLayout linearLayout;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
linearLayout = (LinearLayout) inflater.inflate(R.layout.listview_item, container, false);
setHasOptionsMenu(true);
return linearLayout;
}
}
and here is the error message
enter image description here
You can not start Fragment by Intent. Fragment have to be hosted by any Activity. You have to navigate to this Activity.
If you want to start special Fragment in those Activity (for example any Fragment in ViewPager) put any flag in your Intent. By this flag in your Activity you can determine which Fragment should be started.
It is because you can't call Fragments via Intent, Fragment is a part of an FragmentActivity
All in all Fragment is a content not container, so you need to create a FragmentActivity and add Fragment(Favourite) in that, and then call
Intent intent1 = new Intent(context, SomeFragmentActivity.class);
startActivity(intent1);
A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity.
http://developer.android.com/reference/android/app/Fragment.html

Start Activity within a fragment [duplicate]

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

Categories

Resources