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()
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;
}
In your question you are not defining return statement, before use findViewbyId
just use parameter of return statement.
View abc = inflater.inflate(R.layout.activitys, container,false);
btn1 = (Button) abc.findViewById(R.id.btns);
return abc;
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;
}
When you use Fragment you have initialize Fragment xml using LayoutInflater in onCreateView :
View view = inflater.inflate(R.layout.activity_laundry, container,false);
After get xml reference using LayoutInflater try initialize Fragment xml all child views :
btn_service = (Button) view.findViewById(R.id.btn_service);
btn_service.setOnClickListener(this);
return Fragment xml reference to onCreateView at last statement :
return view;
Note : In your code you directly return Fragment xml before initialize Button so alwasys try to return Fragment xml reference at last statement of onCreateView().
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.