以下是片段活动:
public class in_room_dining extends Fragment {
Button btnord;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_in_room_dining, container, false);
Button btnord = (Button) v.findViewById(R.id.ssched);
btnord.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Please long press the key", Toast.LENGTH_LONG).show();
}
});
return inflater.inflate(R.layout.activity_in_room_dining, container,false);
}
}
当我运行它时,我收到以下错误:
11-29 15:19:53.913
894-894/com.appt.shreyabisht.staymax E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.appt.shreyabisht.staymax, PID: 894
java.lang.NullPointerException
at com.appt.shreyabisht.staymax.in_room_dining.onCreateView(in_room_dining.java:24)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
in_room_dining.java:24是btnord.setOnClickListener(new View.OnClickListener()
答案 0 :(得分:0)
您的btnord
可能为空,因此当您致电SetOnClickListener
时,您将获得例外。
答案 1 :(得分:0)
您的XML布局activity_in_room_dining可能不包含ssched Button。检查你的布局
答案 2 :(得分:0)
同时尝试不像当前那样命名类变量,与方法变量相同。