surfaceview + buttons findviewbyid返回null

时间:2012-11-18 17:10:30

标签: android surfaceview findviewbyid

我的代码还可以,但不幸的是我的代码给了我空指针异常。我知道通过在findviewbyid之前编写setcontentview它不会发生但是然后通过这样做我的表面不会更新。我尝试了很多搜索这个但我无法得到正确的答案,请帮助我。我的代码为mainctivity: -

package com.example.snakes;

public class MainActivity extends Activity implements View.OnClickListener{

MySurfaceClass sc;
Button up,down,left,right;
//View v1,v2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    sc = new MySurfaceClass(this);
    //setContentView(R.layout.activity_main);

    up = (Button) findViewById(R.id.button1);
    down = (Button) findViewById(R.id.button2);
    left = (Button) findViewById(R.id.button3);
    right = (Button) findViewById(R.id.button4);

    up.setOnClickListener(this);
    down.setOnClickListener(this);
    left.setOnClickListener(this);
    right.setOnClickListener(this);
    setContentView(R.layout.activity_main);

    // Toast.makeText(this, "done", Toast.LENGTH_SHORT).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void onClick(View v) {
    switch(v.getId()){
    case R.id.button1:
        sc.posY = sc.posY - 50 ;
        Toast.makeText(this, "up", Toast.LENGTH_SHORT).show();
    //   sc = new MySurfaceClass(this);
      //   setContentView(R.layout.activity_main);
        break;

    case R.id.button2:
        sc.posY = sc.posY + 50 ;
        Toast.makeText(this, "down", Toast.LENGTH_SHORT).show();
        // sc = new MySurfaceClass(this);
         //setContentView(R.layout.activity_main);
        break;

    case R.id.button3:
        sc.posX = sc.posX - 50 ;
        Toast.makeText(this, "left", Toast.LENGTH_SHORT).show();
        //sc = new MySurfaceClass(this);
        //setContentView(R.layout.activity_main);
        break;

    case R.id.button4:
        sc.posX = sc.posX + 10 ;
        Toast.makeText(this, "right", Toast.LENGTH_SHORT).show();
        //setContentView(R.layout.activity_main);
        break;
    }
}
}

表面活动代码: -

package com.example.snakes;


public class MySurfaceClass extends SurfaceView implements Runnable {

private SurfaceHolder sh;
private Thread t = null;
private boolean isRunning;
private Bitmap b;
protected int posX, posY;

public MySurfaceClass(Context context) {
    super(context);
    init();
}

public MySurfaceClass(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public MySurfaceClass(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

void init() {
    isRunning = true;
    b = BitmapFactory.decodeResource(getResources(), R.drawable.face);
    posX = 0;
    posY = 0;
    sh = this.getHolder();
    t = new Thread(this);
    t.start();
};

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawRect(100, 100, 100, 100, null);
}

public void run() {
    while (isRunning) {
        if (!sh.getSurface().isValid())
            continue;

        Canvas c = sh.lockCanvas();
        c.drawRGB(5, 150, 10);
        c.drawBitmap(b, posX, posY, null);
        sh.unlockCanvasAndPost(c);
    }
}
  }

和xml: -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >



<LinearLayout
    android:id="@+id/view1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/grey"
    android:weightSum="100" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="up" 
        android:layout_weight="25"/>

    <Button
        android:id="@+id/button2"
        android:layout_weight="25"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="down" />

    <Button
        android:id="@+id/button3"
        android:layout_weight="25"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="left" />

    <Button
        android:id="@+id/button4"
        android:layout_weight="25"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="right" />
</LinearLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.snakes.MySurfaceClass
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/surfaceView"
        />
</FrameLayout>

2 个答案:

答案 0 :(得分:2)

在使用 findViewById 获取对小部件的任何引用之前,必须先调用

setContentView 方法,否则在将按键侦听器设置为按钮时将获得NullPointerExceptions。

我会先让按钮工作,然后担心表面视图。

答案 1 :(得分:0)

您的代码已损坏。你必须setContentView()否则没有附加视图。