Android应用程序崩溃

时间:2013-04-23 09:18:00

标签: android maps

我正处于我的应用程序开发的最后阶段,但我找不到应用程序崩溃的原因,希望有人在这里可以。它从不同的文件中获取输入,但是当我尝试搜索时,应用程序会挂起一分钟然后崩溃。

根据我的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

1 个答案:

答案 0 :(得分:0)

您的search_options布局似乎不包含任何ID为'mapView'的视图

由于您对此布局使用setContentView,因此对于R.id.mapView,findViewById返回null