这样么
Button lb=(Button)findViewById(R.id.button1);

解决方案 »

  1.   

    Button lb=(Button)view.findViewById(R.id.button1);这个view是你自定义的
      

  2.   

    编译不能通过,Cannot make a static reference to the non-static method findViewById(int) from the type View
      

  3.   

    编译不能通过,Cannot make a static reference to the non-static method findViewById(int) from the type View
      

  4.   

    你在哪里使用了static方法的 去掉试试呢
      

  5.   

    要先导入画面
    setContentView(R.layout.你画面的xml);
      

  6.   

    View view =  LayoutInflater.from(context).inflate(布局文件,null);
    Button btn = (Button)view.findViewById(id);
      

  7.   

    我放下源码
    MainActivity
    setContentView(R.layout.activity_main);
    GameView 
    public class GameView extends View
    {
    public GameView(Context context, AttributeSet attrs)
    {
    super(context, attrs);
    Button button = (Button)findViewById(R.id.button1);
    System.out.println(button);//输出是null
    }
    }
    XMLactivity_main
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/view1"
            android:layout_marginLeft="96dp"
            android:layout_marginTop="73dp"
            android:text="Button" />       <view
               android:id="@+id/view1"
               android:layout_width="100dp"
               android:layout_height="100dp"
               android:layout_alignParentTop="true"
               android:layout_alignRight="@+id/button1"
               android:layout_marginRight="23dp"
               android:layout_marginTop="65dp"
               class="com.example.testview.GameView" />
      

  8.   

    根据你这样写有问题,我不知道我写的对不对
    View view =  LayoutInflater.from(context).inflate(R.layout.activity_main,null);
    Button button = (Button)findViewById(R.id.button1);
    System.out.println(button);
    错误提示:The method inflate(int, ViewGroup) in the type LayoutInflater is not applicable for the arguments (int)
      

  9.   

    肯定不对啊 Button button = (Button)findViewById(R.id.button1);这里默认的是activity.this.findViewById(R,buton1)而在activity的布局文件中应该找不到这个button
      

  10.   

    activity_main有button的啊,为什么找不到呢?
    我上面说的报错是指View view =  LayoutInflater.from(context).inflate(R.layout.activity_main,null);这行
    源码我上面贴出来了,求帮忙看看。
      

  11.   

    LayoutInflater inflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.xmlactivity_main, this);
    Button button = (Button)findViewById(R.id.button1);
    System.out.println(button);看看这样有用吗?
      

  12.   

    R.id.button1,你这个ID应该不是唯一的吧,把这个改成唯一的,看结果还是不是NULL
      

  13.   

    findViewbyid这个方法不是静态的,所以不能再类里面直接调用,试试。(button)this.findViewbyid或者GameView.this.findViewbyid
      

  14.   

    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.activity_main, this);
    这2句报错,错误信息是The method inflate(int, ViewGroup) in the type LayoutInflater is not applicable for the arguments (int)
    这方法我试过,但折腾不出来。。
      

  15.   

    我想你理解错我的意思了,我是在GameView里用的findViewbyid方法来获取activit_main布局里的button,如果像你这样写的话,与findViewbyid方法没差的,因为默认就是GameView来调用,而GameView里没有button,所以输出肯定还是null。
      

  16.   

    LayoutInflater inflater = (LayoutInflater) context
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     View view=inflater.inflate(R.layout.xmlactivity_main, this);
     Button button = (Button)view.findViewById(R.id.button1);
     System.out.println(button);
      

  17.   

    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.activity_main, this);
    这2句报错,错误信息是The method inflate(int, ViewGroup) in the type LayoutInflater is not applicable for the arguments (int)
    这方法我试过,但折腾不出来。。如果可以,我觉得可以让GameView继承linearlayout,这样应该就可以用了。