背景&标题无法解析或是一个字段

时间:2014-07-30 18:53:50

标签: android android-layout android-activity

我正在关注Android游戏编程傻瓜:Derek James。它看起来也过时了,因为在书中它不包含fragment_main。所以我只是复制了fragment_main并将其替换为activity_main并删除了fragment_main(我按照如何摆脱此论坛上的fragment_main教程)。

我正试图在书中打出鼹鼠的游戏。

无论如何,当我按照这本书时,我遇到两个错误,它说我现在可以运行该程序,但我无法做到这一点,因为:

  

背景无法解析或不是字段
  标题无法解析或不是字段

我的所有内容都和书一样,但为什么我会收到这些错误,我在Google上查了一下答案或类似的东西,但我找不到错误,如果有人可以帮我解决,我真的很感激。很抱歉写了一整段,但我应该告诉你我做了什么。

这是我的WhackAMoleView.java

 package com.whackamole;

 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
 import android.os.Handler;
 import android.os.Message;
 import android.util.AttributeSet;
 import android.view.MotionEvent;
 import android.view.SurfaceHolder;
 import android.view.SurfaceView;

  public class WhackAMoleView extends SurfaceView implements
    SurfaceHolder.Callback {

  private Context myContext;
  private SurfaceHolder mySurfaceHolder;
  private Bitmap backgroundImg;
  private int screenW = 1;
  private int screenH = 1;
  private boolean running = false;
  private boolean onTitle = true;
  private WhackAMoleThread thread;

  public WhackAMoleView(Context context, AttributeSet attrs) {
    super(context, attrs);

    SurfaceHolder holder = getHolder();
    holder.addCallback(this);

    thread = new WhackAMoleThread(holder, context, new Handler()

    {
        @Override
        public void handleMessage(Message m) {

        }
    });

    setFocusable(true);

 }

 public WhackAMoleThread getThread() {
    return thread;

}

  class WhackAMoleThread extends Thread {

    public WhackAMoleThread(SurfaceHolder surfaceHolder, Context context,
            Handler handler) {
        mySurfaceHolder = surfaceHolder;
        myContext = context;
        backgroundImg = BitmapFactory.decodeResource(
                context.getResources(), R.drawable.title);

    }

    @Override
    public void run() {
        while (running) {
            Canvas c = null;
            try {
                c = mySurfaceHolder.lockCanvas(null);
                synchronized (mySurfaceHolder) {
                    draw(c);
                }
            } finally {
                if (c != null) {
                    mySurfaceHolderunlockCanvasAndPost(c);
                }
            }

        }
    }

    private void mySurfaceHolderunlockCanvasAndPost(Canvas c) {
        // TODO Auto-generated method stub

    }

    private void draw(Canvas canvas) {
        try {
            canvas.drawBitmap(backgroundImg, 0, 0, null);
        } catch (Exception e) {
        }
    }

    boolean doTouchEvent(MotionEvent event) {
        synchronized (mySurfaceHolder) {
            int eventaction = event.getAction();
            int x = (int) event.getX();
            int Y = (int) event.getY();

            switch (eventaction) {

            case MotionEvent.ACTION_DOWN:
                break;

            case MotionEvent.ACTION_MOVE:
                break;

            case MotionEvent.ACTION_UP:
                if (onTitle) {
                    backgroundImg = BitmapFactory
                            .decodeResource(myContext.getResources(),
                                    R.drawable.background);
                    backgroundImg = Bitmap.createScaledBitmap(
                            backgroundImg, screenW, screenH, true);
                    onTitle = false;
                }

                break;
            }
        }
        return true;
    }

    public void setSurfaceSize(int width, int height) {
        synchronized (mySurfaceHolder) {
            screenW = width;
            screenH = height;
            backgroundImg = Bitmap.createScaledBitmap(backgroundImg, width,
                    height, true);
        }
    }

    public void setRunning(boolean b) {
        running = b;
    }
 }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    return thread.doTouchEvent(event);

 }

  @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    thread.setSurfaceSize(width, height);
 }

 @Override
 public void surfaceCreated(SurfaceHolder holder) {
    thread.setRunning(true);
    if (thread.getState() == Thread.State.NEW) {
        thread.start();
    }
 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
    thread.setRunning(false);
   }
 }

这是我的MainActivity.java

package com.whackamole;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {

 private WhackAMoleView myWhackAMoleView;

 /**Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags
    (WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.whackamole_layout);
    myWhackAMoleView = (WhackAMoleView)
            findViewById(R.id.mole);
    myWhackAMoleView.setKeepScreenOn(true);

 }
 }

这是我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.whackamole"
android:versionCode="1"
android:versionName="1.0" >

 <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">       

    <activity
        android:name="com.whackamole.MainActivity"
        android:screenOrientation="landscape"
        android:configChanges="keyboardHidden|orientation"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

1 个答案:

答案 0 :(得分:-2)

{{1}}