Android方法startActivity(Intent)未定义类型new View.OnClickListener(){}

时间:2012-12-21 01:36:41

标签: java android android-intent android-activity

public class ListSceneAdapter extends OAListSceneAdapter {

    public ListSceneAdapter(Context context) {
        super(context, R.layout.listitem); // pass the custom UI layout for list items
    }

    @Override
    protected void setupListItemView(View listItemView, OAScene scene) {
        // fill the custom UI with the given scene data for the list item
        ((TextView)listItemView.findViewById(R.id.listItemName)).setText(scene.name);

        // add listener for the button
        final OAScene _scene = scene;
        ((Button)listItemView.findViewById(R.id.listItemButton)).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getContext(), _scene.description, Toast.LENGTH_SHORT).show();
                    }
                });

        ((Button)listItemView.findViewById(R.id.listItemMap)).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        double lat = 1.559034;
                        double longi =103.641486;

                        Intent intent = new Intent(
                                android.content.Intent.ACTION_VIEW,
                                Uri.parse("http://maps.google.com/maps?saddr="+ lat + "," + longi+ "&daddr="+_scene.getLatitude()+","+_scene.getLongitude()+""));
                        intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
                        startActivity(intent);

                        //Toast.makeText(getContext(), _scene.description, Toast.LENGTH_SHORT).show();
                    }
                });
    }



}

继承人的代码。当我尝试点击并打开从谷歌地图api导航的新活动..它给这个错误.. 方法startActivity(Intent)未定义类型new View.OnClickListener(){}

继承LOGCAT

12-21 11:30:10.867: D/SensorManager(4782): ====>>>>>Num Sensor: 1
12-21 11:30:10.867: D/SensorManager(4782): ====>>>>>Num Sensor: 2
12-21 11:30:10.875: D/SensorManager(4782): ====>>>>>Num Sensor: 3
12-21 11:30:10.875: D/SensorManager(4782): ====>>>>>Num Sensor: 4
12-21 11:30:10.875: D/SensorManager(4782): ====>>>>>Num Sensor: 5
12-21 11:30:10.875: D/SensorManager(4782): ====>>>>>Num Sensor: 6
12-21 11:30:10.875: D/SensorManager(4782): ====>>>>>Num Sensor: 0
12-21 11:30:10.898: I/inertialManager(4782): Rotation is: 0 or 180
12-21 11:30:10.898: I/intertialManager(4782): Natural Orientation is landscape
12-21 11:30:11.000: W/System.err(4782): java.lang.NumberFormatException: unable to parse 'main' as integer
12-21 11:30:11.015: W/System.err(4782):     at java.lang.Integer.parse(Integer.java:383)
12-21 11:30:11.015: W/System.err(4782):     at java.lang.Integer.parseInt(Integer.java:372)
12-21 11:30:11.015: W/System.err(4782):     at java.lang.Integer.parseInt(Integer.java:332)
12-21 11:30:11.015: W/System.err(4782):     at java.lang.Integer.valueOf(Integer.java:506)
12-21 11:30:11.015: W/System.err(4782):     at com.hitlabnz.androidar.utils.SceneXMLReader.characters(SceneXMLReader.java:427)
12-21 11:30:11.015: W/System.err(4782):     at org.apache.harmony.xml.ExpatParser.text(ExpatParser.java:165)
12-21 11:30:11.015: W/System.err(4782):     at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
12-21 11:30:11.015: W/System.err(4782):     at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:518)
12-21 11:30:11.015: W/System.err(4782):     at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:479)
12-21 11:30:11.015: W/System.err(4782):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318)
12-21 11:30:11.015: W/System.err(4782):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)
12-21 11:30:11.015: W/System.err(4782):     at com.hitlabnz.androidar.utils.SceneXMLReader.readManifest(SceneXMLReader.java:549)
12-21 11:30:11.015: W/System.err(4782):     at com.hitlabnz.androidar.data.management.DataLoaderLocalXML.getSceneData(DataLoaderLocalXML.java:72)
12-21 11:30:11.015: W/System.err(4782):     at com.hitlabnz.androidar.data.management.DataLoaderLocalXML.getSceneData(DataLoaderLocalXML.java:1)
12-21 11:30:11.015: W/System.err(4782):     at com.hitlabnz.outdoorar.data.OADataManagerAssets.loadScenes(OADataManagerAssets.java:200)
12-21 11:30:11.015: W/System.err(4782):     at com.hitlabnz.outdoorar.data.OADataManager.startLoading(OADataManager.java:266)
12-21 11:30:11.015: W/System.err(4782):     at com.hitlabnz.outdoorar.api.OAListComponentBase.setupScenes(OAListComponentBase.java:87)
12-21 11:30:11.015: W/System.err(4782):     at com.hitlabnz.outdoorar.api.OAListComponentBase.onCreate(OAListComponentBase.java:167)
12-21 11:30:11.015: W/System.err(4782):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-21 11:30:11.015: W/System.err(4782):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-21 11:30:11.015: W/System.err(4782):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-21 11:30:11.015: W/System.err(4782):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-21 11:30:11.023: W/System.err(4782):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-21 11:30:11.023: W/System.err(4782):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 11:30:11.023: W/System.err(4782):     at android.os.Looper.loop(Looper.java:130)
12-21 11:30:11.023: W/System.err(4782):     at android.app.ActivityThread.main(ActivityThread.java:3687)
12-21 11:30:11.023: W/System.err(4782):     at java.lang.reflect.Method.invokeNative(Native Method)
12-21 11:30:11.023: W/System.err(4782):     at java.lang.reflect.Method.invoke(Method.java:507)
12-21 11:30:11.023: W/System.err(4782):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-21 11:30:11.023: W/System.err(4782):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-21 11:30:11.023: W/System.err(4782):     at dalvik.system.NativeStart.main(Native Method)
12-21 11:30:11.054: W/System.err(4782): java.lang.NumberFormatException: unable to parse 'main' as integer
12-21 11:30:11.062: W/System.err(4782):     at java.lang.Integer.parse(Integer.java:383)
12-21 11:30:11.062: W/System.err(4782):     at java.lang.Integer.parseInt(Integer.java:372)
12-21 11:30:11.062: W/System.err(4782):     at java.lang.Integer.parseInt(Integer.java:332)
12-21 11:30:11.070: W/System.err(4782):     at java.lang.Integer.valueOf(Integer.java:506)
12-21 11:30:11.070: W/System.err(4782):     at com.hitlabnz.androidar.utils.SceneXMLReader.characters(SceneXMLReader.java:427)
12-21 11:30:11.070: W/System.err(4782):     at org.apache.harmony.xml.ExpatParser.text(ExpatParser.java:165)
12-21 11:30:11.070: W/System.err(4782):     at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
12-21 11:30:11.070: W/System.err(4782):     at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:518)
12-21 11:30:11.070: W/System.err(4782):     at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:479)
12-21 11:30:11.070: W/System.err(4782):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318)
12-21 11:30:11.070: W/System.err(4782):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)
12-21 11:30:11.070: W/System.err(4782):     at com.hitlabnz.androidar.utils.SceneXMLReader.readManifest(SceneXMLReader.java:549)
12-21 11:30:11.070: W/System.err(4782):     at com.hitlabnz.androidar.data.management.DataLoaderLocalXML.getSceneData(DataLoaderLocalXML.java:72)
12-21 11:30:11.070: W/System.err(4782):     at com.hitlabnz.androidar.data.management.DataLoaderLocalXML.getSceneData(DataLoaderLocalXML.java:1)
12-21 11:30:11.070: W/System.err(4782):     at com.hitlabnz.outdoorar.data.OADataManagerAssets.loadScenes(OADataManagerAssets.java:200)
12-21 11:30:11.070: W/System.err(4782):     at com.hitlabnz.outdoorar.data.OADataManager.startLoading(OADataManager.java:266)
12-21 11:30:11.070: W/System.err(4782):     at com.hitlabnz.outdoorar.api.OAListComponentBase.setupScenes(OAListComponentBase.java:87)
12-21 11:30:11.070: W/System.err(4782):     at com.hitlabnz.outdoorar.api.OAListComponentBase.onCreate(OAListComponentBase.java:167)
12-21 11:30:11.070: W/System.err(4782):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-21 11:30:11.070: W/System.err(4782):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-21 11:30:11.070: W/System.err(4782):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-21 11:30:11.070: W/System.err(4782):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-21 11:30:11.070: W/System.err(4782):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-21 11:30:11.070: W/System.err(4782):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 11:30:11.070: W/System.err(4782):     at android.os.Looper.loop(Looper.java:130)
12-21 11:30:11.070: W/System.err(4782):     at android.app.ActivityThread.main(ActivityThread.java:3687)
12-21 11:30:11.070: W/System.err(4782):     at java.lang.reflect.Method.invokeNative(Native Method)
12-21 11:30:11.070: W/System.err(4782):     at java.lang.reflect.Method.invoke(Method.java:507)
12-21 11:30:11.070: W/System.err(4782):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-21 11:30:11.070: W/System.err(4782):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-21 11:30:11.070: W/System.err(4782):     at dalvik.system.NativeStart.main(Native Method)
12-21 11:30:11.242: W/System.err(4782): java.lang.NumberFormatException: unable to parse 'main' as integer
12-21 11:30:11.242: W/System.err(4782):     at java.lang.Integer.parse(Integer.java:383)
12-21 11:30:11.242: W/System.err(4782):     at java.lang.Integer.parseInt(Integer.java:372)
12-21 11:30:11.242: W/System.err(4782):     at java.lang.Integer.parseInt(Integer.java:332)
12-21 11:30:11.242: W/System.err(4782):     at java.lang.Integer.valueOf(Integer.java:506)
12-21 11:30:11.242: W/System.err(4782):     at com.hitlabnz.androidar.utils.SceneXMLReader.characters(SceneXMLReader.java:427)
12-21 11:30:11.242: W/System.err(4782):     at org.apache.harmony.xml.ExpatParser.text(ExpatParser.java:165)
12-21 11:30:11.242: W/System.err(4782):     at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
12-21 11:30:11.242: W/System.err(4782):     at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:518)
12-21 11:30:11.242: W/System.err(4782):     at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:479)
12-21 11:30:11.242: W/System.err(4782):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318)
12-21 11:30:11.242: W/System.err(4782):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)
12-21 11:30:11.242: W/System.err(4782):     at com.hitlabnz.androidar.utils.SceneXMLReader.readManifest(SceneXMLReader.java:549)
12-21 11:30:11.242: W/System.err(4782):     at com.hitlabnz.androidar.data.management.DataLoaderLocalXML.getSceneData(DataLoaderLocalXML.java:72)
12-21 11:30:11.242: W/System.err(4782):     at com.hitlabnz.androidar.data.management.DataLoaderLocalXML.getSceneData(DataLoaderLocalXML.java:1)
12-21 11:30:11.242: W/System.err(4782):     at com.hitlabnz.outdoorar.data.OADataManagerAssets.loadScenes(OADataManagerAssets.java:200)
12-21 11:30:11.250: W/System.err(4782):     at com.hitlabnz.outdoorar.data.OADataManager.startLoading(OADataManager.java:266)
12-21 11:30:11.250: W/System.err(4782):     at com.hitlabnz.outdoorar.api.OAListComponentBase.setupScenes(OAListComponentBase.java:87)
12-21 11:30:11.250: W/System.err(4782):     at com.hitlabnz.outdoorar.api.OAListComponentBase.onCreate(OAListComponentBase.java:167)
12-21 11:30:11.250: W/System.err(4782):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-21 11:30:11.250: W/System.err(4782):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-21 11:30:11.250: W/System.err(4782):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-21 11:30:11.250: W/System.err(4782):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-21 11:30:11.250: W/System.err(4782):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-21 11:30:11.250: W/System.err(4782):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 11:30:11.250: W/System.err(4782):     at android.os.Looper.loop(Looper.java:130)
12-21 11:30:11.250: W/System.err(4782):     at android.app.ActivityThread.main(ActivityThread.java:3687)
12-21 11:30:11.250: W/System.err(4782):     at java.lang.reflect.Method.invokeNative(Native Method)
12-21 11:30:11.250: W/System.err(4782):     at java.lang.reflect.Method.invoke(Method.java:507)
12-21 11:30:11.250: W/System.err(4782):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-21 11:30:11.250: W/System.err(4782):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-21 11:30:11.250: W/System.err(4782):     at dalvik.system.NativeStart.main(Native Method)
12-21 11:30:11.398: D/dalvikvm(4782): GC_CONCURRENT freed 265K, 47% free 3001K/5639K, external 499K/517K, paused 3ms+10ms
12-21 11:30:11.398: I/GPSManager(4782): Network Location Provider available
12-21 11:30:11.468: E/SensorManager(4782): registerListener :: handle = 1600615030  name= Gravity Sensor delay= 20000 Listener= com.hitlabnz.androidar.sensors.inertial.InertialManager$3@4055af88
12-21 11:30:11.468: E/SensorManager(4782): =======>>>Sensor Thread RUNNING <<<========
12-21 11:30:11.476: E/SensorManager(4782): registerListener :: handle = 1  name= MMC328X delay= 20000 Listener= com.hitlabnz.androidar.sensors.inertial.InertialManager$2@4055ac88
12-21 11:30:11.476: E/SensorManager(4782): reg :: handle = 1
12-21 11:30:12.687: E/SensorManager(4782): unregisterListener:: all sensors,  listener = com.hitlabnz.androidar.sensors.inertial.InertialManager$2@4055ac88
12-21 11:30:12.695: E/SensorManager(4782): unregisterListener:: all sensors,  listener = com.hitlabnz.androidar.sensors.inertial.InertialManager$3@4055af88
12-21 11:30:12.804: I/GPSManager(4782): Network Location Provider available
12-21 11:30:12.867: E/SensorManager(4782): registerListener :: handle = 1600615030  name= Gravity Sensor delay= 20000 Listener= com.hitlabnz.androidar.sensors.inertial.InertialManager$3@4055af88
12-21 11:30:12.867: E/SensorManager(4782): registerListener :: handle = 1  name= MMC328X delay= 20000 Listener= com.hitlabnz.androidar.sensors.inertial.InertialManager$2@4055ac88
12-21 11:30:12.875: E/SensorManager(4782): reg :: handle = 1
12-21 11:30:12.976: D/AndroidRuntime(4782): Shutting down VM
12-21 11:30:12.976: W/dalvikvm(4782): threadid=1: thread exiting with uncaught exception (group=0x40018578)
12-21 11:30:13.039: E/AndroidRuntime(4782): FATAL EXCEPTION: main
12-21 11:30:13.039: E/AndroidRuntime(4782): java.lang.NullPointerException
12-21 11:30:13.039: E/AndroidRuntime(4782):     at com.hitlabnz.tutorialbasic.ListSceneAdapter$2.onClick(ListSceneAdapter.java:53)
12-21 11:30:13.039: E/AndroidRuntime(4782):     at android.view.View.performClick(View.java:2485)
12-21 11:30:13.039: E/AndroidRuntime(4782):     at android.view.View$PerformClick.run(View.java:9080)
12-21 11:30:13.039: E/AndroidRuntime(4782):     at android.os.Handler.handleCallback(Handler.java:587)
12-21 11:30:13.039: E/AndroidRuntime(4782):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-21 11:30:13.039: E/AndroidRuntime(4782):     at android.os.Looper.loop(Looper.java:130)
12-21 11:30:13.039: E/AndroidRuntime(4782):     at android.app.ActivityThread.main(ActivityThread.java:3687)
12-21 11:30:13.039: E/AndroidRuntime(4782):     at java.lang.reflect.Method.invokeNative(Native Method)
12-21 11:30:13.039: E/AndroidRuntime(4782):     at java.lang.reflect.Method.invoke(Method.java:507)
12-21 11:30:13.039: E/AndroidRuntime(4782):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-21 11:30:13.039: E/AndroidRuntime(4782):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-21 11:30:13.039: E/AndroidRuntime(4782):     at dalvik.system.NativeStart.main(Native Method)

OAListSceneAdapter类

/*
 * Copyright 2011 the Human Interface Technology Laboratory New Zealand, University of Canterbury.
 * http://www.hitlabnz.org
 * 
 * This software is provided under the license terms described in LICENSE.TXT file distributed with this software package.
 */

package com.hitlabnz.outdoorar.api;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.hitlabnz.androidar.data.SceneData;
import com.hitlabnz.outdoorar.R;

/**
 * Basic adapter class for making custom layout for list items in list components. 
 * @author Gun Lee
 */
public class OAListSceneAdapter extends ArrayAdapter<SceneData> {

    protected LayoutInflater layoutInflater;
    private int listItemLayoutId;
    private Context c; 

    /**
     * Instantiate with the context to access the layout resource for list items.
     * Subclasses should override this method and call super(context, layoutResId) with a customized layout resource.
     * @param context   context for accessing resources
     */
    public OAListSceneAdapter(Context context) {
        this(context, R.layout.oa_list_item);
        c =context;
    }

    /**
     * Instantiate with the context to access the layout resource for list items.
     * Subclasses should NOT override this method.
     * @param context       context for accessing resources
     * @param layoutResId   resource id of the list item layout
     */
    protected OAListSceneAdapter(Context context, int layoutResId) {
        super(context, R.layout.list_item);

        this.layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        listItemLayoutId = layoutResId;
    }

    /**
     * Called back for setting up the list item view for a given scene.
     * Subclasses should override this method for setting up the list item view for the scene.
     * Subclasses need NOT to call the method in the super class.
     * @param listItemView  view for the list item, create with the layout resource id given at instan
     * @param scene
     */
    protected void setupListItemView(View listItemView, OAScene scene) {
        TextView nameText = (TextView) listItemView.findViewById(R.id.listItemName);
        TextView categoryText = (TextView) listItemView.findViewById(R.id.listItemCategory);

        nameText.setText(scene.name);
        if(scene.category == null)
            categoryText.setText("");
        else
            categoryText.setText("- " + scene.category);
    }

    /**
     * Called back for a view for the list item.
     * Subclasses should NOT override this method, but override the setupListItemView() method for customization.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null)
            convertView = layoutInflater.inflate(listItemLayoutId, parent, false);

        OAScene scene = (OAScene)this.getItem(position);
        setupListItemView(convertView, scene);
        return convertView;
    }

}

3 个答案:

答案 0 :(得分:13)

那是因为Listener没有启动活动。

我假设传递给你的类的Context可以通过getter方法或类似的东西对整个类使用。如果没有,则可以通过构造函数获取对此Context的全局引用。您可能需要将其设为最终或Listener 可能投诉。

无论如何,你要做的就是打电话

context.startActivity(intent)

而不是

startActivity(intent); 

因为Contexts可以启动活动。

答案 1 :(得分:2)

您可以将对象活动类传递给适配器,从该适配器调用适配器。然后使用该对象调用startActivity。

答案 2 :(得分:0)

 ((Button)listItemView.findViewById(R.id.listItemMap)).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                   Intent intent = new Intent(A.alias,B.class);

                        A.alias.startActivity(intent);
                    }
                });