How to open activity from fragment - android

I am just learning programming and faced with such an error. I need to open activity ruki from home fragment. But when I run the emulator, my application crashes
What am I doing wrong
ImageButton ruki;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, null);
final ImageButton imageButton = (ImageButton) view. findViewById(R.id.ruki);
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.ruki:
Intent intent = new Intent( HomeFragment.this.getActivity(), ruki_activity.class);
startActivity(intent);
break;
}
}
};
ruki.setOnClickListener(onClickListener);
return inflater.inflate(R.layout.fragment_home, container, false);
}
}

you are never initiating ImageButton ruki;, you are obtainging reference to final ImageButton imageButton, which isn't even used. ruki.setOnClickListener(onClickListener); will cause NullPointerException (ALWAYS post exception stacktrace)
instead of creating new variable
final ImageButton imageButton = (ImageButton) view. findViewById(R.id.ruki);
just attach result of findViewById to declared one
ruki = (ImageButton) view. findViewById(R.id.ruki);

public void onClick(View view) {
Intent intent= new Intent(getActivity(),UsingBackKey.class);
startActivity(intent);
}

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

imagebutton inside fragment nullPoint Exception

I am putting ImageView inside Fragment. I have other question also but cannot find useful.
this is my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_fragment_contect,container,false);
ImageButton imageButton = (ImageButton)getView().findViewById( R.id.ib);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity( new Intent( getActivity(), Enquiry.class ) );
}
});
return v;
}
You need to do like this in getview() you getting null pointer exception use view.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_fragment_contect,container,false);
ImageButton imageButton = v.findViewById(R.id.ib);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity( new Intent( getActivity(), Enquiry.class ) );
}
});
return v;
}
You got NullPointException because of using getview():
ImageButton imageButton = (ImageButton)getView().findViewById( R.id.ib);
Solution: use v instead of getView()
ImageButton imageButton = (ImageButton) v.findViewById( R.id.ib);
Android Fragment Referance

Intent from Fragment to a MapsActivity from onCreateView

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!!!

Button click throws nullpointerexception in fragment

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;

Null Pointer Exception when setting new text to text view

I am trying to run an AndroidApp on my phone. I am able to show my welcome fragment, moreover I can trigger the log-message. Unfortunately I am getting a null pointer exception if I want to change the text value from 'welcome' to 'Neuer Text'. What went wrong? I am quite new to android development.
public class WelcomeFragment extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
TextView text1 = (TextView) v.findViewById(R.id.welcome_text);
text1.setText("NeuerText");
}
}
In the onClick(), v is the view item that was clicked, e.g. the button, not the view that is inflated in onCreateView().
You should use getView():
public class WelcomeFragment extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
switch (v.getId) {
case R.id.button1:
TextView text1 = (TextView) getView().findViewById(R.id.welcome_text);
text1.setText("NeuerText");
break;
}
}
}
Also, if you don't want that action taking place for every button you may want to consider using a switch statement in your onClick().
You cant get the TextView from the button's onClick passed parameter ("View v"), since this is the actual button's view.
You should do something like:
public class WelcomeFragment extends Fragment implements OnClickListener {
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
TextView text1 = (TextView) view.findViewById(R.id.welcome_text);
text1.setText("NewerText"); //Also fixed typo
}
}
In OnClick(View v) , v is the button that you are clicking on. The TextView does not belong to Button, it belongs to R.layout.fragment_welcome. So you find and initialize the TextView inside onCreateView() of the fragment, and then use it inside onClick();
Some what like this:
public class WelcomeFragment extends Fragment implements OnClickListener {
TextView tv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
tv = (TextView) v.findViewById(R.id.welcome_text);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
tv.setText("NeuerText");
}
}

Categories

Resources