在onTouchEvent'上遇到问题在android中

时间:2012-10-16 08:44:05

标签: android

我正在通过观看Thenewboston的视频来练习Android开发。

从他的一个视频中,我正在尝试制作onTouchEvent(),但我收到错误:Java.lang.NullPointerException

The newboston 134 number tutorial

我该怎么办?

现在整个代码是:

    package com.chupamobile.android.googlemaps;

import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.MotionEvent;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class GoogleMapsActivity extends MapActivity {

    MapView map;
    Long start, stop;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        map = (MapView) findViewById(R.id.mymap);
        map.setBuiltInZoomControls(true);

        Touchy t = new Touchy();
        List<Overlay> overlayList = map.getOverlays();
        overlayList.add(t);
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

    class Touchy extends Overlay {
        public boolean onTouchEvent(MotionEvent e, MapView m) { 
            if (e.getAction() == MotionEvent.ACTION_DOWN) {
                start = e.getEventTime();
            }
            if (e.getAction() == MotionEvent.ACTION_UP) {
                stop = e.getEventTime();
            }
            if (stop - start > 1500) {
                AlertDialog alert = new AlertDialog.Builder(
                        GoogleMapsActivity.this).create();
                alert.setTitle("Pick an option");
                alert.setMessage("I told u pick an option");
                alert.setButton("Place a pin",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // TODO Auto-generated method stub

                            }
                        });
                alert.show();
                return true;
            }

            return false;
        }
    }
}

我的错误列表:

Warning: 10-16 10:06:57.106: E/MapActivity(1649): Couldn't get connection factory client


10-16 10:07:03.645: W/dalvikvm(1649): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
10-16 08:17:31.174: W/dalvikvm(1349): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
10-16 08:17:31.334: E/AndroidRuntime(1349): FATAL EXCEPTION: main
10-16 08:17:31.334: E/AndroidRuntime(1349): java.lang.NullPointerException
10-16 08:17:31.334: E/AndroidRuntime(1349):     at com.chupamobile.android.googlemaps.GoogleMapsActivity$Touchy.onTouchEvent(GoogleMapsActivity.java:45)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at com.google.android.maps.OverlayBundle.onTouchEvent(OverlayBundle.java:63)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at com.google.android.maps.MapView.onTouchEvent(MapView.java:679)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.view.View.dispatchTouchEvent(View.java:5541)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1951)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1712)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1912)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1860)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.view.View.dispatchPointerEvent(View.java:5721)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2890)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2466)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2475)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.os.Looper.loop(Looper.java:137)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at android.app.ActivityThread.main(ActivityThread.java:4424)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at java.lang.reflect.Method.invokeNative(Native Method)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at java.lang.reflect.Method.invoke(Method.java:511)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-16 08:17:31.334: E/AndroidRuntime(1349):     at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:4)

尝试创建OnTouchListner。

例如

    button.setOnTouchListener(new View.OnTouchListener() 
    {

        public void onClick(View v) 
        {
            }

答案 1 :(得分:0)

你可以这样做。

说你需要在按钮中点击触摸(即点击我猜的事件),而不是在你的xml代码中添加这个

android:onClick=onClick

并在你的oncreate方法之后的活动中写下这个

public void onClick(View v){

   switch(v.getId()){

      case R.id.urbuttonid:{

        Log.e("click event fired?","yes");        

        break;

      }

   }

}

执行此操作后检查您的日志

答案 2 :(得分:0)

在上面的代码中,我使用了Long start, stop;而不是long start, stop;这是错误。