Reputation: 676
This is a java class of an activity and what im trying to do is generate a toast when a button is clicked but I get the above error. I tried adding a closed parentheses but it threw the same error.
public class btntoast extends Fragment
{
Button btn_order;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// TODO Auto-generated method stub
return inflater.inflate(R.layout.activity_in_room_dining, container, false);
btn_order.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getActivity(), "text", Toast.LENGTH_SHORT).show();
}
}
}
}
Updated code to :
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 thrown is :
11-29 09:38:22.098 1138-1138/com.appt.shreyabisht.staymax W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1ab9ba8)
11-29 09:38:22.138 1138-1138/com.appt.shreyabisht.staymax E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.appt.shreyabisht.staymax, PID: 1138
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)
NOTE: (in_room_dining.java:23) is btn_order.setOnClickListener(new OnClickListener()
Upvotes: 1
Views: 206
Reputation: 2087
Just as simple add );
after define OnClickListener, like this:
btn_order.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "text", Toast.LENGTH_SHORT).show();
}
});
Upvotes: 0
Reputation: 190
try like this :
public class btntoast extends Fragment
{
Button btn_order;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.hello_world, container, false);
btn_order =(Button) v.findViewById(R.id.text);
btn_order.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getActivity(), "text", Toast.LENGTH_SHORT).show();
}
});
return v;
}
}
Upvotes: 0
Reputation: 10395
You missed ")" for setOnClickListener
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);
also its unreachable code because of return statement
put it at the end of onCreateView
;
Upvotes: 1
Reputation: 23
Try this
btn_order.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getActivity(), "text", Toast.LENGTH_SHORT).show();
}
}
});
Upvotes: 1