I've tab activity in which i want to use a button, but when i click on button, it force closes the app. Can you please tell me what is going on, i am new to Android.
public class HomeActivity extends SherlockFragment {
private Button bt;
private Context con;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home_layout, container, false);
bt = (Button)rootView.findViewById(R.id.btn);
bt.setOnClickListener(new View.OnClickListener(){
#Override public void onClick(View v)
{
Toast.makeText(con, "hello", Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
when i click on button, it force closes the app
Because con object of Context is null. which is used for showing Toast message in Button onClick method.
Do it as:
Toast.makeText(v.getContext(), "hello", Toast.LENGTH_LONG).show();
And initialize con object by calling getActivity() in onCreateView as:
con=getActivity();
If you can avoid that, don't keep a reference to the context; instead of the
private Context con;
get the current Activity in the code, so use
Toast.makeText(getActivity(), "hello", Toast.LENGTH_LONG).show();
Btw, this seems to be the cause of your crash, as Context is null.
Related
In my app I have tabbed activity with two fragments. At the fragment I want to use button to open dialog alert. But for five hours now I'm struggling with actual clicking on button. I tried ways with making onClick with xml, Implement OnClickListener But nothing worked. The button is just doing nothing. I tried to debug it and it seems that onCreateView method is not called at all. Any tips?
public class PlayersFragment extends Fragment {
public PlayersFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_players, container, false);
Button addButton = (Button) view.findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "YES is clicked!",
Toast.LENGTH_LONG).show();
}
});
return view;
}
Override the onViewCreated method in your fragment class. Then put the onClickListener on your Button with the method.
This is a part of my calc_fragment.xml file ,
<Button
android:id="#+id/b7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dp"
android:background="#drawable/roundedbutton"
android:fontFamily="sans-serif-thin"
android:text="Add"
android:textColor="#color/light_grey"
android:textSize="45sp"
android:onClick="clicked"/>
that is linked with the following fragment:
public class CalcFragment extends Fragment {
private TextView textView;
private String text;
private Vibrator vibe;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.calc_fragment, container, false);
return rootView;
}
public void clicked (View v){ <--- "Method clicked is never used"
...content...
}
I get a warning "Method clicked is never used" but I my button is linked with this method
A Fragment is tightly linked with its activity. So, the above method declaration in xml will basically look for that method in the Activity class. One of the ways is that you can explicitly link your method and the button:
public class CalcFragment extends Fragment {
private TextView textView;
private String text;
private Vibrator vibe;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.calc_fragment, container, false);
View myButton = rootView.findViewById(R.id.b7);
rootView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clicked(v);
}
});
return rootView;
}
public void clicked (View v){ // Your method
...content...
}
Get rid of the 'android:onClick="clicked"' property in your xml.
This is because the XML code for handling clicks does not work for the Fragment class, it currently works only with the Activity class. As such, this method will never be called. See this question for more details.
In the onCreateView method of the CalcFragment, you need to declare and initialize the button after the initializing the rootView:
Button b7 = (Button) rootView.findViewById(R.id.b7);
You then need an onClickListener instead of defining your own clicked method. There are two methods to listen to a click of a button.
The first method:
You can set the onClickListener within the onCreatView as follows:
b7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something
}
});
The second method:
You can define an onClickListener as follows:
final View.OnClickListener b7OnClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something
}
};
When using the second method, you will need to set the onClickListener like you did in the first method however in this case you will pass the name of the onClickListener object you created.
Example:
b7.setOnClickListener(b7OnClickListener);
signup screen with of two different modules driver and customer
Hi everyone I am working in android from past one year but now I am creating an app in which am using fragments instead of activity even after serching from so many sites and blogs am unable to implement what I want.
My requirement is as you see on the image two radio button one for customer and one for driver so when user click on any one of options then signup screen appears which is on fragment and one button is there at bottom which is on activity so I want how to get all edittext value on button click so that i can send these values to the server. My main problem is getting the values of edittext fields on button click.
So any help will be appriciated.
thanks in advance
Try like this
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.your_fragment, container, false);
this.initViews(rootView);
selectProfessionsTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
whta_you_want_on_button_click();
}
});
}
private void initViews(View view) {
//define your edittext like FirstNameEditText = (EditText) view.findViewById(R.id.FirstNameEditText);
}
now define the whta_you_want_on_button_click() method
in your activity, put below code in your button's click event.
try {
Fragment rg = getSupportFragmentManager().findFragmentByTag("YourFragmentTag");
if (rg != null) {
EditText et = (EditText) ((YourFragment) rg).getView().findViewById(R.id.edittext);
String etValue = et.getText().toString();
}
} catch (Exception e) {
}
Your context will change from getApplicationContext() to getActivity.getApplicationContext() when you are referencing a fragment instead of an activity
You have to create your edittext in this fashion
While assigning the layout you need to assign in this way.
View view = inflater.inflate(R.layout.fragmentlayout,container,false);
And while assigning edittext you need to take it as below
EditText edt = (EditText) view.findViewById(R.id.yourid);
String str = edt.getText().toString();
So on button click you can get the edittext string easily from onClick method with above code.
You can use static keyword make all edittext static so you will get all edittext values in activity on button click.
in your fragment suppose MyFragment.java
public class MyFragment extends Fragment{
View rootView;
public static EditText edt;
public MyFragment() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_main, container, false);
init();
return rootView;
}
protected void init(){
edt = (EditText) rootView.findViewById(R.id.edt);
}
}
in your activity suppose MainActivity.java
public class MainActivity extends Activity{
Button start;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewByid(R.id.button);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try{
String value = MyFragment.edt.getText().toString().trim();
Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
}catch(Exception e){}
}
});
}
}
I have an activity and a fragment inside it.inside fragment, there is a button, and on click of button a dialog shows.
Everything works, until user do a orientation change and click button after it.
IllegalStateException(cannot perform this action after onsaveinstancestate) occurs when user clicks button after orientation change. I'm using android support framework.
Anybody have any idea regarfing this?
Activity Code
public void openMoreDialog(String shareData, String link) {
DialogFragment dialog = new MoreDialog(shareData, link);
dialog.show(getSupportFragmentManager(), "MoreDialog");
}
Fragment Code
public void onAttach(Activity activity) {
super.onAttach(activity);
mControl = (ActivityControl)activity;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ImageButton moreButton = (ImageButton)v.findViewById(R.id.moreButton);
moreButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mControl.openMoreDialog(shareData, link);
}
});
return rootView;
}
FragmentDialog code
public class MoreDialog extends DialogFragment {
private String mShareData;
private String mLink;
public MoreDialog(String shareData, String link){
mShareData = shareData;
mLink = link;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.more_dialog, null);
Button openBtn = (Button)dialogView.findViewById(R.id.openBtn);
openBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openLink(mLink);
}
});
Button shareBtn = (Button)dialogView.findViewById(R.id.shareBtn);
shareBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
shareNews(mShareData);
}
});
builder.setView(dialogView);
return builder.create();
}
private void openLink(String link){
}
private void shareNews(String data){
}
}
Helpful link & solution how to:
https://stackoverflow.com/a/17413324/619673 and btw, constructor in fragment must be empty! Documentation:
http://developer.android.com/reference/android/app/Fragment.html
public Fragment ()
Added in API level 11
Default constructor.
Every fragment must have an empty constructor, so
it can be instantiated when restoring its activity's state. It is
strongly recommended that subclasses do not have other constructors
with parameters, since these constructors will not be called when the
fragment is re-instantiated; instead, arguments can be supplied by the
caller with setArguments(Bundle) and later retrieved by the Fragment
with getArguments().
Applications should generally not implement a constructor. The first
place application code an run where the fragment is ready to be used
is in onAttach(Activity), the point where the fragment is actually
associated with its activity. Some applications may also want to
implement onInflate(Activity, AttributeSet, Bundle) to retrieve
attributes from a layout resource, though should take care here
because this happens for the fragment is attached to its activity.
I have two different layouts. One is which loads while start of the Activity and the other which loads after running some checks and creates a custom dialog. The Dialog has a button in it to trigger, at this point in time, onclick has a Toast message so I can confirm that the button has been clicked. Unfortunately I can't able to get any response when the button is clicked. I've been all over the web and I can't quite find what I'm missing.
public class myactivity extends Activity{
Dialog accesspopup;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myactivity);
View inflatedView = getLayoutInflater().inflate(R.layout.dialoglayout, null);
final Button cabtn = (Button)inflatedView.findViewById(R.id.cb);
cabtn.setOnClickListener(cListener);
}
private OnClickListener cListener = new OnClickListener() {
public void onClick(View v) {
//Log.d("HiThereActivity", "THIS IS DEBUG OUTPUT TO LOGCAT");
Toast.makeText(myactivity.this, "The Start button was clicked.", Toast.LENGTH_LONG).show();
}
};
public void showPopup(){
accesspopup = new Dialog(myactivity.this);
accesspopup.setContentView(R.layout.pop_window);
accesspopup.setCancelable(false);
accesspopup.setTitle("Window Title");
accesspopup.show();
}
I did some more searching around and found that I need to create the OnClickListener inside the method which I am using to build and display the Dialog and not in the OnCreate.
use this way...
public class myactivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myactivity);
View inflatedView = getLayoutInflater().inflate(R.layout.dialoglayout, null);
final Button cabtn = (Button)inflatedView.findViewById(R.id.cb);
cabtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(myactivity.this, "The Start button was clicked.", Toast.LENGTH_LONG).show();
}
});
}
May be still your R.layout.activity_myactivity is the controllable Contentview in your activity.
So you have to define your new layout as setContentView.
or you mentioned it is a Dialog box.
So you can add a content view for a dialog like the following,
Dialog d = new Dialog (this);
d.setContentView(your inflated view);