1
我正在嘗試通過Drag和Touch Listener創建簡單的應用程序。但是當我通過內部類將TouchListener設置爲TextView控件時,獲取NullPointerException
:這裏是代碼。NULL在Android中實現Touch Listener時出現指針異常
public class MainActivity extends Activity
{
private TextView option1, choice1;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
option1 = (TextView)findViewById(R.id.option_1);
setContentView(R.layout.activity_main);
option1.setOnTouchListener(new ChoiceTouchListener()); [NULLPOINTER]
}
private final class ChoiceTouchListener implements OnTouchListener
{
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
if(arg1.getAction() == MotionEvent.ACTION_DOWN)
{
ClipData clipdata = ClipData.newPlainText("","");
DragShadowBuilder shadowbuilder = new DragShadowBuilder(arg0);
arg0.startDrag(clipdata, shadowbuilder, arg0, 0);
return true;
}
else
{
return false;
}
}
}
}