为什么这个onTouchListener会抛出NullPointerException?

时间:2012-05-29 13:50:21

标签: android nullpointerexception ontouchevent ontouchlistener

我不明白,我在同一个应用程序的主要活动中使用了相同的代码,并且效果很好。但是当我在其他活动中使用它时,它会抛出NullPointer ....为什么会这样?

     private TextView drag_txtSeconds; 

     @Override
 public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drag_layout); 

       drag_txtSeconds = (TextView)findViewById(R.id.TxtSeconds); 

       drag_txtSeconds.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v,MotionEvent event) {
                return true; 
            }});
         }

希望你能帮助我:)。

1 个答案:

答案 0 :(得分:1)

确保您的文件drag_layout的{​​{1}}具有属性:

TextView

这是区分大小写的,可能是由于大写字母,请尝试在布局和代码中使用全部小写。