Android App Crashing (Fragment and Xml Onclick)

Android app crashing (fragment and xml onclick)

Activity:

If are having activity and if you define android:onClick attribute in XML then you just need to define a method with the same name in Activity.

Fragment:

But whenever you have Fragment and if you want to define click listener by just defining android:onClick attribute then you have to define a method with the same name in actual activity from where Fragment has been called.

OR you can simply implement a click listener programmatically.

When Onclick operates in fragment xml, app crashes

It worked, Try this

FromDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataPickerDialog_from.show();
}
});

ToDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataPickerDialog_to.show();
}
});

App crashes with OnClickListener in Fragment

You need to add the item click listener in ListView and use that. You can't just find the ListViews items because there would be multiple of them.

mTaskListView.setOnItemClickListener(this); // And remember to implement the interface

https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html

Then you can move your code from method onClick to onItemClick

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView taskTextView = view.findViewById(R.id.task_title);
String task = String.valueOf(taskTextView.getText());
SQLiteDatabase db = mHelper.getWritableDatabase();
// and so on...
}

and ofcourse you need to remove the lines which are causing the null pointer exception:

Button b = view.findViewById(R.id.btn_delete);
b.setOnClickListener(this);

Kotlin: setOnClickListener on Fragment Crashes app

Set your on click listener in onViewCreated, in onCreateView you are calling onClick listener before the view could set.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
calculateNutCountButton.setOnClickListener {
Toast.makeText(
activity,"Success.",
Toast.LENGTH_SHORT).show()
}
}

App crashes on button click, (using xml android:onClick and Fragment classes)

your problem is in xml file you set attributes OnClick="sendText" and defining method in fragment Actually you have to defining it in MainActivity not it Fragment

public void sendText(View view){
// your code ....
}

Actually you this way do not Applied encapsulation object oriented properties –
so best way to do it like this :
you have to override onCreateView method in NotesFragment class to set onClickListner like this

private Button foo; // as globle var
private OnButtonsClicked boo; // as globle var

public void onAttach(Activity activity) {
super.onAttach(activity);
this.boo = (OnButtonsClicked) activity;
}
public interface OnButtonsClicked {

public void sendText(View v);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.notes,
container, false);
foo = view.findViewByid(R.id.foo); // button id
foo.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
boo.sendText(v);
}
});
return view;
}

and in your Activity implement OnButtonsClicked interface like this

 public class MainActivity extends FragmentActivity implements
ActionBar.TabListener, NotesFragment.OnButtonsClicked {
// your code ....

@override
public void sendText(View v){
// button click code here
}
}

Button from Fragment is Crashing the app

After you inflate your view

View view = inflater.inflate(R.layout.fragment_carprofile_list, container, false);

you assume that view is RecyclerView. That means root element is recyclerview.

Then trying to find a button inside of that recyclerview by doing

Button imgbtn = (Button) view.findViewById(R.id.editCarProfileButton);

which returns null ofcourse. Then trying to set a click listener on null object.

Looks like your xml file is wrong. If your each row has button that you are trying to find, you need to catch it in your adapter class then notify fragment to do your process.

SOLUTION (EDIT):

Remove these lines from onCreateView in Fragment.

Button imgbtn = (Button) view.findViewById(R.id.editCarProfileButton);
imgbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(getActivity(),ReserveActivity.class));
}
});

You have already a interface that notify the fragment.
So that we are going to use it for editing.

So the final adapter class should be like

public class MyCarProfileRecyclerViewAdapter extends RecyclerView.Adapter<MyCarProfileRecyclerViewAdapter.ViewHolder> {

private final List<CarProfile> mValues;
private final OnListFragmentInteractionListener mListener;

public MyCarProfileRecyclerViewAdapter(List<CarProfile> items, OnListFragmentInteractionListener listener) {
mValues = items;
mListener = listener;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_carprofile, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mPlateNumberTextView.setText(mValues.get(position).getPlateNumber());
holder.mBrandTextView.setText(mValues.get(position).getBrand());
holder.mModelTextView.setText(mValues.get(position).getModel());
holder.mColorTextView.setText(mValues.get(position).getColor());
}

@Override
public int getItemCount() {
return mValues.size();
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

public final TextView mPlateNumberTextView;
public final TextView mBrandTextView;
public final TextView mModelTextView;
public final TextView mColorTextView;
public CarProfile mItem;

public ViewHolder(View view) {
super(view);

view.setClickable(true); // This is important
view.setOnClickListener(this);

mPlateNumberTextView = (TextView) view.findViewById(R.id.plateNumberTextView);
mBrandTextView = (TextView) view.findViewById(R.id.brandTextView);
mModelTextView = (TextView) view.findViewById(R.id.modelTextView);
mColorTextView = (TextView) view.findViewById(R.id.colorTextView);
}

public void onClick(View view) {
if (null != mListener) {
mListener.onListFragmentInteraction(mValues.get(getAdapterPosition()));
}
}

@Override
public String toString() {
return super.toString() + " '" + mPlateNumberTextView.getText() + "'";
}
}

Then your fragment

    @Override
public void onListFragmentInteraction(CarProfile profile) {
// TODO Pass your profile to ReserverActivity by using bundles.
startActivity(new Intent(getActivity(), ReserveActivity.class));
}

You can improve the code by notify fragment on your button click. So your button click listener and notify code must also be in here.

Android app crashing with OnClickListener in fragment

At first sight you should replace:

final Button send = (Button) getActivity().findViewById(R.id.send);

with:

final Button send = (Button) mView.findViewById(R.id.send);

R.id.send id is contained in the Fragment view mView.

App crashes when i open it with Fragments

Replace this

<fragment
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

with this

<View
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

U are mixing dynamic and static fragments



Related Topics



Leave a reply



Submit