2013-04-23 176 views
0

我在我的應用程序開發的最後階段,但我找不到應用程序崩潰的原因,希望有人在這裏可以。它正在接受來自不同文件的輸入,但是當我嘗試搜索時,該應用程序會掛起一分鐘,然後崩潰。Android應用程序崩潰

據我logcat的文件,它達到崩潰前行是

mapV = (MapView) findViewById(R.id.mapView);然後崩潰。同樣在LogCat中,它說packagename.DisplaySearchOptions上的java.lang.NullPointerException。

這裏是我的代碼 -

// All imports 

public class DisplaySearchOptions extends MapActivity 
{ 
    MapController mControl; 
    GeoPoint GeoP; 
    MapView mapV; 
    EditText numberOfRooms; 

int theAvailablePropertiesBedrooms[] = {4, 3, 3, 1, 5}; 
int theAvailablePropertiesprice[] = {300000, 180000, 200000, 50000, 450000}; 
double LatHouses[] = {53.426599, 53.425500, 53.425122, 53.413081, 53.416208}; 
double LongHouses[] = {-7.931568, -7.939915, -7.950454, -7.957041, -7.904963}; 

double desiredRooms = 0; 
double desiredPrice = 0; 

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(GeoP, screenPts); 

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

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.search_options); 
    Log.d("Events", "Inside onCreate method"); 

    Button searchBtn = (Button) findViewById(R.id.btnSearch); 
    Log.d("Events", "Button searchBtn = (Button) findViewById(R.id.btnSearch); LINE REACHED"); 

    mapV = (MapView) findViewById(R.id.mapView); 
    Log.d("Events", "mapV = (MapView) findViewById(R.id.mapView); LINE REACHED"); 

    mapV.setBuiltInZoomControls(true); 
    mapV.setSatellite(true);   

    final List<Overlay> mapOverlays = mapV.getOverlays(); 
    Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher); 
    final HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this); 

    // Starting point on map 
    double lat = 53.423933100000000000; 
    double longi = -7.940689799999973000; 

    GeoP = new GeoPoint((int) (lat * 1E6), (int) (longi * 1E6)); 

    mControl = mapV.getController(); 
    mControl.animateTo(GeoP); 
    mControl.setZoom(15); 

    searchBtn.setOnClickListener(new Button.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Log.d("Events", "searchBtn.setOnClickListener(new Button.OnClickListener() LINE REACHED"); 

      EditText numberOfRooms = (EditText) findViewById(R.id.numberRoomsnew); 

      String myEditValue = numberOfRooms.getText().toString(); 

      desiredRooms = Integer.parseInt(myEditValue); 

      Spinner priceEntered = (Spinner) findViewById(R.id.priceRange); 
      String myEditValue2 = priceEntered.getSelectedItem().toString(); 

      desiredPrice = Integer.parseInt(myEditValue2); 

      double roomScoreForHouses[] = { 0, 0, 0, 0, 0 }; 
      double priceScoreForHouses[] = { 0, 0, 0, 0, 0 }; 

      for (int i = 0; i < 5; i++) 
      { 
       if (desiredRooms == theAvailablePropertiesBedrooms[i]){ 
        roomScoreForHouses[i] = 1.0; 
       } else if (desiredRooms == theAvailablePropertiesBedrooms[i] + 1){ 
        roomScoreForHouses[i] = 0.75; 
       } else if (desiredRooms == theAvailablePropertiesBedrooms[i] - 1){ 
        roomScoreForHouses[i] = 0.75; 
       } else if (desiredRooms == theAvailablePropertiesBedrooms[i] + 2){ 
        roomScoreForHouses[i] = 0.5; 
       } else{ 
        roomScoreForHouses[i] = 0; 
       } 
      } 

      // Repeat this for price 
      for (int i = 0; i < 5; i++) 
      { 
       if (desiredPrice >= theAvailablePropertiesprice[i] - 50000 && desiredPrice <= theAvailablePropertiesprice[i] + 50000){ 
        priceScoreForHouses[i] = 1.0; 
       } 
       else if (desiredPrice == theAvailablePropertiesprice[i] + 1){ 
        priceScoreForHouses[i] = 0.75; 
       } 
       else if (desiredPrice == theAvailablePropertiesprice[i] - 1){ 
        priceScoreForHouses[i] = 0.75; 
       } 
       else if (desiredPrice == theAvailablePropertiesprice[i] + 2){ 
        priceScoreForHouses[i] = 0.5; 
       } 
       else{ 
        priceScoreForHouses[i] = 0; 
       } 
      } 

      for (int i = 0; i < 5; i++) { 
       if ((roomScoreForHouses[i] + priceScoreForHouses[i]) > .75) 
       { 
        GeoPoint point = new GeoPoint((int) (LatHouses[i] * 1E6),(int) (LongHouses[i] * 1E6)); 
        OverlayItem overlayitem = new OverlayItem(point,"House Information:", "Price:"+ theAvailablePropertiesprice[i]+ " Num Rooms "+ theAvailablePropertiesBedrooms[i]); 
        itemizedoverlay.addOverlay(overlayitem); 
       } 
       mapOverlays.add(itemizedoverlay); 
      } 
     } 
    });  

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

}

這裏是我的search_options.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:orientation="vertical" > 

    <!-- Choose number of rooms label --> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textStyle="bold|italic" 
     android:text="Choose number of bedrooms:" 
     android:textSize="18sp" /> 

    <!-- Number of rooms text field --> 
    <EditText 
     android:id="@+id/numberRoomsnew" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:hint="No. of rooms" 
     android:imeActionLabel="launch" 
     android:inputType="number" /> 

    <!-- Select price range label --> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textStyle="bold|italic" 
     android:text="Select your price range:" 
     android:textSize="18sp" /> 

    <!-- Price range drop down menu --> 
    <Spinner 
     android:id="@+id/priceRange" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:prompt="@string/prompt" 
     android:entries="@array/priceRange" /> 

    <!-- Search button --> 
    <Button 
     android:id="@+id/btnSearch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Search" /> 

</LinearLayout> 

我的清單 -

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.cornboyz.googlemapsC" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <uses-library android:name="com.google.android.maps" /> 

     <!-- Launches this screen first --> 
     <activity 
      android:name="com.cornboyz.googlemapsC.Splash" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.cornboyz.googlemapsC.ButtonClass" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="com.cornboyz.googlemapsC.BUTTONCLASS" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.cornboyz.googlemapsC.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="com.cornboyz.googlemapsC.MAINACTIVITY" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.cornboyz.googlemapsC.SearchOptions" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="com.cornboyz.googlemapsC.SEARCHOPTIONS" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.cornboyz.googlemapsC.DisplaySearchOptions" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="com.cornboyz.googlemapsC.DISPLAYSEARCHOPTIONS" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.cornboyz.googlemapsC.SearchActivity" 
      android:label="@string/title_activity_search" > 
      <intent-filter> 
       <action android:name="com.cornboyz.googlemapsC.SEARCHACTIVITY" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

    </application> 

</manifest> 

這裏是我的完整的LogCat -

04-23 10:54:31.648: E/AndroidRuntime(1613): FATAL EXCEPTION: main 
04-23 10:54:31.648: E/AndroidRuntime(1613): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cornboyz.googlemapsC/com.cornboyz.googlemapsC.DisplaySearchOptions}: java.lang.NullPointerException 
04-23 10:54:31.648: E/AndroidRuntime(1613):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
04-23 10:54:31.648: E/AndroidRuntime(1613):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
04-23 10:54:31.648: E/AndroidRuntime(1613):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
04-23 10:54:31.648: E/AndroidRuntime(1613):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
04-23 10:54:31.648: E/AndroidRuntime(1613):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-23 10:54:31.648: E/AndroidRuntime(1613):  at android.os.Looper.loop(Looper.java:137) 
04-23 10:54:31.648: E/AndroidRuntime(1613):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
04-23 10:54:31.648: E/AndroidRuntime(1613):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-23 10:54:31.648: E/AndroidRuntime(1613):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-23 10:54:31.648: E/AndroidRuntime(1613):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
04-23 10:54:31.648: E/AndroidRuntime(1613):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
04-23 10:54:31.648: E/AndroidRuntime(1613):  at dalvik.system.NativeStart.main(Native Method) 
04-23 10:54:31.648: E/AndroidRuntime(1613): Caused by: java.lang.NullPointerException 
04-23 10:54:31.648: E/AndroidRuntime(1613):  at com.cornboyz.googlemapsC.DisplaySearchOptions.onCreate(DisplaySearchOptions.java:91) 
04-23 10:54:31.648: E/AndroidRuntime(1613):  at android.app.Activity.performCreate(Activity.java:5104) 
04-23 10:54:31.648: E/AndroidRuntime(1613):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
04-23 10:54:31.648: E/AndroidRuntime(1613):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
04-23 10:54:31.648: E/AndroidRuntime(1613):  ... 11 more 
+2

關於什麼的任何視圖崩潰日誌?堆棧跟蹤... – Triode 2013-04-23 09:18:59

+0

search_options.xml包含mapView嗎? – Blackbelt 2013-04-23 09:19:53

+0

可以請你發佈你的XML和Android清單文件? – AndroidEnthusiastic 2013-04-23 09:22:54

回答

0

你search_options佈局似乎並不包含ID 'MapView的'

由於您使用的setContentView用這個佈局,findViewById爲R.id.mapView

返回null
+0

,應用程序仍然崩潰。 – funky 2013-04-23 11:01:50