2

我正在尝试从布局 XML 文件获取资源setText()TextView

当用户点击谷歌地图中的标记时。这是我onTap()为标记编写的代码。

直到我不添加TextView,我的代码工作正常。当我单击标记并且一切正常时,它会显示对话框。

当我将这些行添加到时onTap(),我运行应用程序并单击标记,它停止说Unfortunately map has stopped

我收到 LogCat 错误消息:

分派输入事件时出错

我创建了一个类 CustomOverlayItem 扩展 OverlayItem 类。Tv_descriptionTextView是布局文件 cutom_dialog.xml 的一部分。

当用户单击标记时,如何在运行时动态setText()调用?TextView

@Override
protected boolean onTap(int index) {
    Dialog dialog = new Dialog(context, android.R.style.Theme_Dialog);
    TextView tv_description = new TextView(context);
    tv_description = (TextView) dialog.findViewById(R.id.tv2_description);
    tv_description.setText("checking");

    dialog.setContentView(R.layout.custom_dialog);
    dialog.show();

    return true;

}

这是 logcat 输出:

07-31 11:59:16.271: E/Trace(2143): error opening trace file: No such file or directory (2)
07-31 11:59:17.741: E/ZoomButtonsController(2143): Cannot make the zoom controller visible if the owner view is not attached to a window.
07-31 11:59:18.151: E/MapActivity(2143): Couldn't get connection factory client
07-31 11:59:28.531: E/InputEventReceiver(2143): Exception dispatching input event.
07-31 11:59:28.541: E/MessageQueue-JNI(2143): Exception in MessageQueue callback: handleReceiveCallback
07-31 11:59:28.591: E/MessageQueue-JNI(2143): java.lang.NullPointerException
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at com.incubers.map.Markers.onTap(Markers.java:41)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:453)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:356)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:533)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at com.google.android.maps.MapView.onTouchEvent(MapView.java:683)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.view.View.dispatchTouchEvent(View.java:7127)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.view.View.dispatchPointerEvent(View.java:7307)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3172)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3117)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4224)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.os.MessageQueue.nativePollOnce(Native Method)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.os.MessageQueue.next(MessageQueue.java:125)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.os.Looper.loop(Looper.java:124)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at android.app.ActivityThread.main(ActivityThread.java:4745)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at java.lang.reflect.Method.invokeNative(Native Method)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at java.lang.reflect.Method.invoke(Method.java:511)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-31 11:59:28.591: E/MessageQueue-JNI(2143):   at dalvik.system.NativeStart.main(Native Method)
07-31 11:59:28.681: E/AndroidRuntime(2143): FATAL EXCEPTION: main
07-31 11:59:28.681: E/AndroidRuntime(2143): java.lang.NullPointerException
07-31 11:59:28.681: E/AndroidRuntime(2143):     at com.incubers.map.Markers.onTap(Markers.java:41)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:453)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:356)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:533)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at com.google.android.maps.MapView.onTouchEvent(MapView.java:683)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.view.View.dispatchTouchEvent(View.java:7127)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.view.View.dispatchPointerEvent(View.java:7307)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3172)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3117)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4224)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.os.MessageQueue.nativePollOnce(Native Method)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.os.MessageQueue.next(MessageQueue.java:125)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.os.Looper.loop(Looper.java:124)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at android.app.ActivityThread.main(ActivityThread.java:4745)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at java.lang.reflect.Method.invoke(Method.java:511)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-31 11:59:28.681: E/AndroidRuntime(2143):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

我相信这条线是错误的。

tv_description = (TextView) dialog.findViewById(R.id.tv2_description);

您正在尝试查找TextView对话框中不存在的内容。

您的对话框是默认的Dialog,不会有这个TextView。在你setContentView()的对话框上做一个布局,TextView在上面这行之前有这个。

于 2012-07-31T07:02:08.247 回答
1

您可以尝试TextView通过以下方式更新您的Handler

public Handler MainHandler = new Handler() 
{
    @Override
    public void handleMessage(Message msg) 
    {
        _mytextview.setText((String)msg.obj);
    }
}; 

发送来自您的消息onTap()

Message _message = new Message();
_message.obj = "my text to update";
MainHandler.sendMessage(_message);

希望它有帮助:-)

[编辑] 这是我尝试过的代码:

custom_dialog.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" android:id="@+id/Overlay_Layout" android:orientation="vertical">
<TextView android:textAppearance="?android:attr/textAppearanceSmall" android:layout_width="wrap_content" android:layout_height="wrap_content"  android:layout_margin="5dp" android:text="@string/empty" android:id="@+id/tv2_description">    </TextView>
</LinearLayout>

在我的onTouchEvent()

Dialog dialog = new Dialog(_context, android.R.style.Theme_Dialog);
dialog.setContentView(R.layout.custom_dialog);
TextView tv_description = (TextView) dialog.findViewById(R.id.tv2_description);
tv_description.setText("checking");
dialog.show();

它对我有用..

于 2012-07-31T06:43:58.770 回答