1

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

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

newboston 134号码教程

我应该怎么办?

现在整个代码是:

    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)
4

3 回答 3

4

尝试创建一个 OnTouchListner。

例子

    button.setOnTouchListener(new View.OnTouchListener() 
    {

        public void onClick(View v) 
        {
            }
于 2012-10-16T09:08:18.423 回答
0

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

于 2012-10-16T19:26:15.840 回答
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;

      }

   }

}

这样做后检查你的日志

于 2012-10-16T09:15:06.390 回答