2012-06-16 45 views
-1

我試圖一個開發application..following是一個片段Android的Java錯誤

class metro_nodes { 
public String station; 
public GeoPoint point; } 

public class mainscreen extends MapActivity { 
/** Called when the activity is first created. */ 

MapController controller; 
double latitude,longitude; 
LocationManager loc; 
Location lastknownloc; 
LocationListener loclistener; 
List<GeoPoint> geopoints = new ArrayList<GeoPoint>(); 
MapView mapView; 
private LinkedList<metro_nodes> station_location = new LinkedList<metro_nodes>(); 
metro_nodes anand_nagar; 
anand_nagar.station = "anand_nagar"; 
    } 

現在在倒數第二行的捐贈 - 站「標誌的語法錯誤‘’,VariableDeclaratorId預計此令牌後「

如果我把大括號括在這個語句,然後錯誤得到刪除..但然後我得到java lang空指針異常... 什麼是problm?

+5

你或許應該嘗試做的是涉及到的東西這個複雜的應用之前學習一些基本的Java。這會讓你自己更容易。 – Austin

回答

0

你還沒有發佈代碼給你的語法錯誤......但我猜測你得到了空指針異常,因爲你從未初始化任何「metro_nodes」對象。

例如:

// This is a helper class 
class MetroNode { 
    public String station; 
    public GeoPoint point; 

    public MetroNode (String station, GeoPoint point) { 
    this.station = station; 
    this.point = point; 
    } 
} 

// This is the module's main class 
public class MainScreen extends MapActivity { 

    // This is public and private data for each class instance 
    private LinkedList<MetroNode> station_location = null; 

    // This is the activity's initialization 
    public void onCreate(Bundle savedInstanceState) 
    station_locations = new LinkedList<MetroNode>(); 
    MetroNode metroNode = new MetroNode ("anand_nagar", new GeoPoint (1.00, 2.00)); 
    station_locations.add (metroNode); 
    ... 
+1

你不應該創建活動的構造函數...使用onCreate ... –

+1

@Dheeresh辛格:點好了 - 我沒有注意到上課恰巧是Android的「活動」。我的觀點是*每個*對象都需要被「創建」(它不會僅僅由「魔術」發生),並且每個類都需要一些初始化方法(通常是構造函數)。我認爲OP的問題更多的是「Java 101」,而不是Android編程本身。恕我直言...... – paulsm4

+0

感謝您的回覆......我的一個非常愚蠢的錯誤... – vigenere

0

空指針的贖罪是因爲你還沒有使用裝箱新

metro_nodes anand_nagar; 
anand_nagar = new metro_nodes();//<----------------need this line to avoid NPE 
anand_nagar.station = "anand_nagar"; 

,並請遵循標準,如類名metro_nodes可變anand_nagar的情況下應該是這樣MetroNode在java中.....

public class MainScreen extends MapActivity { 


    private MapController mController; 
    private double mLatitude,mLongitude; 
    private LocationManager mLoc; 
    private Location mLastknownloc; 
    private LocationListener mLoclistener; 

    private MapView mMapView; 
    private LinkedList<MetroNode> mStationLocations; 
    MetroNode mAnandNagar; 


    //in general we use cunstructor for initliaztion but in android we usesonCreate 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // ..other code .............. 

     List<GeoPoint> geoPoints = new ArrayList<GeoPoint>(); 
     mStationLocations = new LinkedList<MetroNode>(); 
     mAnandNagar = new MetroNode(); 
     mAnandNagar.setStation("anand_nagar"); 
    } 
} 

class MetroNode { 
    private String station; 
    private GeoPoint point; 

    public MetroNode(){ 
     super();  
    } 

    public MetroNode(String station, GeoPoint point) { 
     super(); 
     this.station = station; 
     this.point = point; 
    } 

    public String getStation() { 
     return station; 
    } 
    public void setStation(String station) { 
     this.station = station; 
    } 
    public GeoPoint getPoint() { 
     return point; 
    } 
    public void setPoint(GeoPoint point) { 
     this.point = point; 
    } 




} 
+0

難道你不覺得三十行,四個方法,兩個構造函數和調用「super()」(甚至沒有基類的時候)對於本質上是兩字段的數據結構只是稍微有點過分嗎? – paulsm4

+0

感謝您的回覆...我的部分非常愚蠢的錯誤... – vigenere

1

您有類字段聲明,第二行是不屬於變量聲明部分的語句 - 必須作爲方法/構造函數的一部分來完成。

通過將它包含在大括號中,實際上是創建了一個類初始化塊,但是在將任何實例分配給NPE之前嘗試訪問anand_nagar變量。 Austin是對的 - 你似乎錯過了一些基本的Java編程知識,所以也許你應該先從一些Java教程開始:http://docs.oracle.com/javase/tutorial/