解决方案 »

  1.   

    补充一下,如果代码改一下:
    mButton=(Button)findViewById(R.id.mButton);
    mButton.setOnClickListener(new OnClickListener()
    {
      @Override
      public void onClick(View v)
        {
            //do something
        }
    });
    此时就会因为findViewById并没有找到正确的资源,导致mButton=null,setOnClickListener会引发NullException.
      

  2.   

    把setContentView放到前面去,这样:public void onCreate(Bundle savedInstanceState)
    {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            mButton=(Button)findViewById(R.id.mButton); 
    }setContentView会把xml资源绑定到Activity上,此后才能直接做find操作,否则一定是空的
      

  3.   


    感谢指点.NULL的问题解决了,的却是这样.但是,IDEA里边的R文件的内容始终为空是什么情况?如下:
    /*___Generated_by_IDEA___*/package com.example.App;/* This stub is only used by the IDE. It is NOT the R class actually packed into the APK */
    public final class R {
    }
    在eclipse中,R文件里面会有好几个静态类.比如id,attr什么的.这里虽然没有,但是也不会报错,程序同样运行正常.请指点