Getting error "cannot resolve findViewById (int)"

I have a main action and when I click a button I go to the Tab screen. Now I want the Clickable button to be clicked on these tabs and the toast is displayed when the button is clicked. Here is the code I'm using:

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 findViewById is not a sign symbol for this activity. Is there an alternative way to include clickable buttons in the fragment activity as above?

Run the project and I will get this error. (I don't know if the proposed changes worked, my application crashed with the following error) This is a fragment activity and I am trying to create a toast on a button click.

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

      

I got the error:

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) - btn_order.setOnClickListener (new OnClickListener ()

+3


source to share


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

      



+6


source


In your question, you do not define a return statement, findViewbyId

just use the return statement parameter before using .



 View abc = inflater.inflate(R.layout.activitys, container,false);  
 btn1 = (Button) abc.findViewById(R.id.btns);  
 return abc;

      

+1


source


 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;

}

      

+1


source


When you use Fragment, you initialize the xml fragment with LayoutInflater in onCreateView:

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

      

After getting the xml link using LayoutInflater, try to initialize the Fragment xml all child views:

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

      

return xml snippet reference to onCreateView in last statement:

return view;

      

Note: In your code, you are directly returning the xml snippet before initializing the Button, so alwasys try to return the xml snippet reference in the last onCreateView () declaration.

0


source


make the following changes ...

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

now previx findViewById by myView.findViewById.

This should do the trick.

0


source







All Articles