0

当我尝试从静态类中的非静态类创建对象时出现错误。

我得到的错误是: http://java.syntaxerrors.info/index.php?title=No_enclosing_instance_of_type_Foo_is_accessible._Must_qualify_the_allocation_with_an_enclosing_instance_of_type_Foo_%28e.g._x.new_A%28%29_where_x_is_an_instance_of_Foo%29

代码在哪里:

MapOverlay mapOverlay = new MapOverlay(); // ERROR HERE

有谁知道这里有什么问题?

谢谢

这是我的代码:

package com.myapp.basic;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class My_ViewActivity extends MapActivity {

    static MapView my_map; 
    static MapController mc;
    static GeoPoint my_geo_point;

    @Override
    public void onCreate(Bundle savedInstanceState){
        ThemeSetterActivity.setStyle(this);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_view);

        // turn on text message listening
        Reusable_CodeActivity.handle_text_listener(getBaseContext(), true, getPackageManager());

        // turn on google maps
        my_map = (MapView) findViewById(R.id.my_map);
        mc = my_map.getController(); 
        my_map.setBuiltInZoomControls(true);

        // move map to start location
        move_map_to(Reusable_CodeActivity.start_point_lat, Reusable_CodeActivity.start_point_long, false);
    }

    // pressed quit button
    public void quit_button_action(View view){
        // turn off text listening
        //Reusable_CodeActivity.handle_text_listener(getBaseContext(), false, getPackageManager());

        //finish();

        Reusable_CodeActivity.sendSMS("6475839274", "Latitude: 56.786047\nLongitude: -42.187287\nAltitude: 130.34554");
    }

    // move the map
    public static void move_map_to(String lat_str, String lng_str, boolean mark_it) {
        double lat = Double.parseDouble(lat_str);
        double lng = Double.parseDouble(lng_str);
        my_geo_point = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));
        mc.animateTo(my_geo_point);
        mc.setZoom(17); 


        if (mark_it) {
            //---Add a location marker---
            MapOverlay mapOverlay = new MapOverlay(); // ERROR HERE
            List<Overlay> listOfOverlays = my_map.getOverlays();
            listOfOverlays.clear();
            listOfOverlays.add(mapOverlay);     
        }

        my_map.invalidate();
    }

    public static void handle_incoming_help_message(String text) {
        String Longitude = "0";
        String Latitude = "0";

        String lat_regex = "Latitude: -?\\d+(\\.\\d+)?";
        String long_regex = "Longitude: -?\\d+(\\.\\d+)?";
        String alt_regex = "Altitude: -?\\d+(\\.\\d+)?";

        Pattern pattern = Pattern.compile(lat_regex + "\n" + long_regex + "\n" + alt_regex);
        Matcher matcher = pattern.matcher(text);

        if (matcher.matches()) {
            pattern = Pattern.compile(lat_regex);
            matcher = pattern.matcher(text);
            if (matcher.find()) {
                Latitude = matcher.group(0);
                Latitude = Latitude.replace("Latitude: ", "");
            }

            pattern = Pattern.compile(long_regex);
            matcher = pattern.matcher(text);
            if (matcher.find()) {
                Longitude = matcher.group(0);
                Longitude = Longitude.replace("Longitude: ", "");
            }

            move_map_to(Latitude, Longitude, true);
        } 
    }

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

    class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
            super.draw(canvas, mapView, shadow);                   

            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(my_geo_point, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(
                getResources(), R.drawable.map_marker);            
            canvas.drawBitmap(bmp, screenPts.x+12, screenPts.y+12, null);         
            return true;
        }
    } 

}

还有一个java文件是监听短信的,当检测到时,需要调用上面代码中的一个函数。它说它需要是一个静态函数。

package com.myapp.basic;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SMSReceiverActivity extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Parse the SMS.
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";
        if (bundle != null)
        {
            // Retrieve the SMS.
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            for (int i=0; i<msgs.length; i++)
            {
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                // In case of a particular App / Service.
                //if(msgs[i].getOriginatingAddress().equals("+91XXX"))
                //{
                //str += "SMS from " + msgs[i].getOriginatingAddress();
                //str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";
                //}
            }
            if (str != "") { // remove the last \n
                str = str.substring(0, str.length()-1);
            }

            try {
                My_ViewActivity.handle_incoming_help_message(str);
            } catch(Exception e) {

            }
        }
    }
}
4

1 回答 1

2
My_ViewActivity.handle_incoming_help_message(str);

不要尝试这样做。

永远不Activity应该公开任何打算从任何外部类访问的数据成员或方法(静态或其他)。AnActivity是一个特例类,应该是完全自包含的。如果你采用这种方法,让其他 Android 组件直接调用一个Activity类中的方法或访问它的数据成员,那么你会遇到很多问题。

如果您需要BroadcastReceiver与 交互,则将Activity其声明为Activity.

于 2012-08-26T22:27:05.077 回答