Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当触摸屏打开时,OnTouchEvent()程序退出_Android - Fatal编程技术网

Android 当触摸屏打开时,OnTouchEvent()程序退出

Android 当触摸屏打开时,OnTouchEvent()程序退出,android,Android,我有一个程序,基本上当你触摸屏幕时需要停止“人” 只从左向右移动的人 我的个人课 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 (fl

我有一个程序,基本上当你触摸屏幕时需要停止“人” 只从左向右移动的人

我的个人课

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;


}*/
}

日志:

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)

确定要解决您的问题,您应该确保
person
引用类的实例

范例

person = new Person();
onCreate
中。Person是一个例子,随班级一起改变它

在为其分配实例之前,不能使用
person


换成它

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;
}

可能是在
person
获取实例之前,以某种奇怪的方式调用了
onTouchEvent

?问题是什么?问题是当我执行程序时,一切正常,但当我触摸屏幕时,程序突然崩溃。我正在使用nexus S作为模拟器。好的,添加logcat以便我们可以帮助您。这里是日志文件的下载链接:抱歉,我无法在此处添加它。为您添加的是,您应该只添加应用程序崩溃时出现的logcat,而不是所有内容!我已经做了,但仍然不起作用。Person-Person=新用户(0,屏幕高度/2,10,20,屏幕宽度);这是您想要的吗?是的,您添加它的地方?并确保它发生在onTouchEvent之前,或者使用
if(person==null)return false暂时避免它我可以添加person和myactivity类的所有代码,如果它有帮助的话。我现在看不出任何错误,我编辑了我的答案