我有这个程序,基本上当你触摸屏幕需要停止"人" 这个人只能从左到右移动。
我的班级人员
package com.example.pedestriancrossing2;
公共类人员{
private float x,y;
private int height,width;
private int screenWidth;
private int destination;
private boolean needStop;
private float tempX;
public Person (float x, float y, int height, int width, int screenW)
{
this.x = x;
this.y = y;
this.height = height;
this.width = width;
this.screenWidth = screenW;
this.destination = -1;
}
public float left()
{
return x-width;
}
public float right()
{
return x+width;
}
public float Top()
{
return y-height;
}
public float Bottom()
{
return y+height;
}
public void setDestination (int destination)
{
this.destination = destination;
}
public void movePerson ()
{
if(right()>=screenWidth)
{
x = 0;
}
if(destination == -1)
x++;
}
}
和我的主要活动>>
package com.example.pedestriancrossing2;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.graphics.Point;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.app.Activity;
import android.view.View.OnTouchListener;
import android.util.Log;
public class MainActivity extends ActionBarActivity {
private Person person;
@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Point p = new Point();
this.getWindowManager().getDefaultDisplay().getSize(p);
int screenWidth = p.x;
int screenHeigh = p.y;
Car car = new Car( screenWidth/2 , screenHeigh/2 , 15 , screenHeigh);
Person person = new Person( 0 , screenHeigh/2 , 10 , 20 , screenWidth);
CarView carview = new CarView(this, car);
PersonView personview = new PersonView(this, person);
GameView gameview = new GameView(this);
gameview.setViews(carview, personview);
setContentView(gameview);
CarMover moveCar = new CarMover(car,gameview);
PersonMover movePerson = new PersonMover(person, gameview);
person.setDestination(-1);
moveCar.start();
movePerson.start();
/*setContentView(R.layout.activity_main);*/
/*if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}*/
}
public boolean onTouchEvent(MotionEvent event)
{
if (person != null) return false;
int destination = event.getAction();
if(destination == MotionEvent.ACTION_DOWN)
{
person.setDestination(1);
}
if(destination == MotionEvent.ACTION_UP)
{
person.setDestination(-1);
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
/*@Override*/
/*public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int isTouched;
isTouched = event.getAction();
if(isTouched == MotionEvent.ACTION_DOWN)
{
person.setNeedStop(true);
}
if(isTouched == MotionEvent.ACTION_UP)
{
person.setNeedStop(false);
}
return true;
}*/
}
logcat的:
03-30 13:57:45.092: E/AndroidRuntime(856): java.lang.NullPointerException
03-30 13:57:45.092: E/AndroidRuntime(856): at com.example.pedestriancrossing2.MainActivity.onTouchEvent(MainActivity.java:57)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.app.Activity.dispatchTouchEvent(Activity.java:2461)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.dispatchTouchEvent(ActionBarActivityDelegateICS.java:260)
03-30 13:57:45.092: E/AndroidRuntime(856): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.View.dispatchPointerEvent(View.java:7886)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.os.MessageQueue.nativePollOnce(Native Method)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.os.MessageQueue.next(MessageQueue.java:138)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.os.Looper.loop(Looper.java:123)
03-30 13:57:45.092: E/AndroidRuntime(856): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-30 13:57:45.092: E/AndroidRuntime(856): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 13:57:45.092: E/AndroidRuntime(856): at java.lang.reflect.Method.invoke(Method.java:515)
03-30 13:57:45.092: E/AndroidRuntime(856): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-30 13:57:45.092: E/AndroidRuntime(856): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-30 13:57:45.092: E/AndroidRuntime(856): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
好了解决您的问题,您应该确保person
引用您班级的实例。
实施例
person = new Person();
onCreate
中的。人就是一个例子,用你的班级改变它。
在您为实例提供实例之前,不能使用person
。
将onTouchEvent更改为
public boolean onTouchEvent(MotionEvent event)
{
if (person != null) return false;
int destination = event.getAction();
if(destination == MotionEvent.ACTION_DOWN)
{
person.setDestination(1);
}
if(destination == MotionEvent.ACTION_UP)
{
person.setDestination(-1);
}
return true;
}
在onTouchEvent
获取实例之前,可能会以某种奇怪的方式调用person
。