Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error "cannot resolve findViewById(int)'

I have a main activity and when I press a button, i am navigated to a swipe Tab view. Now I want to have a Clickable Button in these tabs and present a toast when the button is pressed. Here is the code I use:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class laundry extends Fragment {
Button btn_service;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.activity_laundry, container,false);
    btn_service = (Button) findViewById(R.id.btn_service);
    btn_service.setOnClickListener(this);
}

But the findViewById is not a know symbol to this activity. Is there an alternative way to incorporate clickable buttons in fragment activity like above?

Ran the project and I get this error. ( I cant know if the suggested changes worked, my app crashed with the following error) This is a fragment activity and I am trying to generate a toast when a button is clicked.

public class in_room_dining extends Fragment
{
Button btn_order;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    // TODO Auto-generated method stub

    btn_order.setOnClickListener(new OnClickListener()
{
 public void onClick(View v)
{
Toast.makeText(getActivity(), "text", Toast.LENGTH_SHORT).show();
}
});
 return inflater.inflate(R.layout.activity_in_room_dining, container, false);
}
}

The error I got is :

11-29 10:57:42.820      938-938/com.appt.shreyabisht.staymax E/AndroidRuntime﹕ FATAL EXCEPTION:     main
Process: com.appt.shreyabisht.staymax, PID: 938
java.lang.NullPointerException
        at com.appt.shreyabisht.staymax.in_room_dining.onCreateView(in_room_dining.java:23)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)

(in_room_dining.java:23) is btn_order.setOnClickListener(new OnClickListener()

like image 294
shrbisht Avatar asked Sep 03 '26 02:09

shrbisht


2 Answers

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class laundry extends Fragment {
Button btn_service;
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // TODO Auto-generated method stub 
final View rootView = inflater.inflate(R.layout.activity_laundry, container,
                false);

    btn_service = (Button) rootView.findViewById(R.id.btn_service);
    btn_service.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

         //your Code Goes Here    
        }
    });
    return rootView;
} 
like image 164
koutuk Avatar answered Sep 04 '26 14:09

koutuk


 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View V = inflater.inflate(R.layout.activity_laundry, container, false);

btn_service = (Button) v.findViewById(R.id.btn_service);

return V;

}
like image 23
stacktry Avatar answered Sep 04 '26 16:09

stacktry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!