2013-08-05 107 views
0

正試圖啓動一個地圖活動,它可以讓你當前的GPS位置。當按下按鈕時它工作正常,直到我決定爲用戶添加一個功能,以便在禁用時打開他的GPS。我使用了我在Google上找到的代碼片段,它使用了某種處理程序類。它沒有工作,我一直在我的錯誤日誌中看到它,所以我把它拿出來了。但是當我按下按鈕時,程序會一直返回空指針異常。開始地圖活動拋出空指針異常

我的問題是「我如何得到該程序回到之前啓動地圖活動等,如果可能的話,我怎麼把在GPS檢查

這是我的logcat

08-05 08:24:50.411: E/AndroidRuntime(17838): FATAL EXCEPTION: main 
08-05 08:24:50.411: E/AndroidRuntime(17838): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.g6.georeminderv3/com.g6.georeminderv3.CheckInActivity}: java.lang.NullPointerException 
08-05 08:24:50.411: E/AndroidRuntime(17838): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872) 
08-05 08:24:50.411: E/AndroidRuntime(17838): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893) 
08-05 08:24:50.411: E/AndroidRuntime(17838): at android.app.ActivityThread.access$1500(ActivityThread.java:135) 
08-05 08:24:50.411: E/AndroidRuntime(17838): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054) 
08-05 08:24:50.411: E/AndroidRuntime(17838): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-05 08:24:50.411: E/AndroidRuntime(17838): at android.os.Looper.loop(Looper.java:150) 
08-05 08:24:50.411: E/AndroidRuntime(17838): at android.app.ActivityThread.main(ActivityThread.java:4385) 
08-05 08:24:50.411: E/AndroidRuntime(17838): at java.lang.reflect.Method.invokeNative(Native Method) 
08-05 08:24:50.411: E/AndroidRuntime(17838): at java.lang.reflect.Method.invoke(Method.java:507) 
08-05 08:24:50.411: E/AndroidRuntime(17838): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 
08-05 08:24:50.411: E/AndroidRuntime(17838): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 
08-05 08:24:50.411: E/AndroidRuntime(17838): at dalvik.system.NativeStart.main(Native Method) 
08-05 08:24:50.411: E/AndroidRuntime(17838): Caused by: java.lang.NullPointerException 
08-05 08:24:50.411: E/AndroidRuntime(17838): at com.google.android.maps.ItemizedOverlay.populate(ItemizedOverlay.java:312) 
08-05 08:24:50.411: E/AndroidRuntime(17838): at com.g6.georeminderv3.OverlayItems.addOverlay(OverlayItems.java:22) 
08-05 08:24:50.411: E/AndroidRuntime(17838): at com.g6.georeminderv3.CheckInActivity.onCreate(CheckInActivity.java:100) 
08-05 08:24:50.411: E/AndroidRuntime(17838): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072) 
08-05 08:24:50.411: E/AndroidRuntime(17838): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836) 
08-05 08:24:50.411: E/AndroidRuntime(17838): ... 11 more 
08-05 08:24:52.322: D/Process(17838): killProcess, pid=17838 
08-05 08:24:52.322: D/Process(17838): dalvik.system.VMStack.getThreadStackTrace(Native Method) 
08-05 08:24:52.322: D/Process(17838): java.lang.Thread.getStackTrace(Thread.java:745) 
08-05 08:24:52.322: D/Process(17838): android.os.Process.killProcess(Process.java:797) 
08-05 08:24:52.322: D/Process(17838): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:108) 
08-05 08:24:52.322: D/Process(17838): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:854) 
08-05 08:24:52.322: D/Process(17838): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:851) 
08-05 08:24:52.322: D/Process(17838): dalvik.system.NativeStart.main(Native Method) 
08-05 08:24:52.322: I/Process(17838): Sending signal. PID: 17838 SIG: 9 

這我mainactivity啓動檢入活動

public class MainActivity extends Activity implements OnClickListener { 
ImageButton add, settings, checkIn; 
ListView list; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_main); 
    findviews(); 
    populateListView(); 

    settings.setOnClickListener(this); 
    add.setOnClickListener(this); 
    checkIn.setOnClickListener(this); 

} 

private void populateListView() { 
    DatabaseHandler db = new DatabaseHandler(this); 



    // start here 
    SQLiteDatabase database = db.getReadableDatabase(); 
    Log.d(DatabaseHandler.TAG, DatabaseHandler.actionsSelectQuery); 
    String[] columns = new String[] { DatabaseHandler.KEY_ACTIONS_ID, 
      DatabaseHandler.KEY_ACTIONS_TITLE, 
      DatabaseHandler.KEY_ACTIONS_PLACE_NAME, 
      DatabaseHandler.KEY_ACTIONS_TIME, 
      DatabaseHandler.KEY_ACTIONS_DATE }; 

    // Cursor cursor = database.rawQuery(db.actionsSelectQuery, null); 
    Cursor cursor = database.query(DatabaseHandler.TABLE_ACTIONS, columns, 
      null, null, null, null, DatabaseHandler.KEY_ACTIONS_ID + " DESC"); 

    String[] from = new String[] { DatabaseHandler.KEY_ACTIONS_TITLE, 
      DatabaseHandler.KEY_ACTIONS_PLACE_NAME, 
      DatabaseHandler.KEY_ACTIONS_TIME, 
      DatabaseHandler.KEY_ACTIONS_DATE }; 
    int[] to = { R.id.title_text, R.id.place_text, R.id.time_text, 
      R.id.date_text }; 

    @SuppressWarnings("deprecation") 
    SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(
      MainActivity.this, R.layout.action_list_layout, cursor, from, 
      to); 
    list.setAdapter(listAdapter); 
    Log.d(DatabaseHandler.TAG, "list view created"); 
    // mySQLiteAdapter.close(); 

} 

private void findviews() { 
    settings = (ImageButton) findViewById(R.id.settings); 
    add = (ImageButton) findViewById(R.id.add_reminder); 
    checkIn = (ImageButton) findViewById(R.id.check_in); 

    list = (ListView) findViewById(R.id.action_list_view); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    //add fav manager here 
    return true; 
} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.settings: 
     startActivity(new Intent(MainActivity.this, SettingsActivity.class)); 
     break; 
    case R.id.add_reminder: 
     startActivity(new Intent(MainActivity.this, AddReminder.class)); 
     MainActivity.this.finish(); 
     break; 
    case R.id.check_in: 
     startActivity(new Intent(MainActivity.this, CheckInActivity.class)); 
     break; 
    } 
} 

}

這是我checkinactivity

public class CheckInActivity extends MapActivity { 
private int place_longitude; 
private int place_latitude; 
Button save; 
EditText chkInPlaceName; 
String chkSavePlaceName,setPlongString,setPlatString; 
TextView setPlong,setPlat; 
GeoPoint point; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_check_in); 

    MapView mapView = (MapView) findViewById(R.id.mapview); 
    final MapController controller = mapView.getController(); 

    LocationManager manager = (LocationManager) this 
      .getSystemService(LOCATION_SERVICE); 
    LocationListener listener = new LocationListener() { 

     @Override 
     public void onStatusChanged(String provider, int status, 
       Bundle extras) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 

     @Override 
     public void onLocationChanged(Location location) { 
      setPlace_latitude((int) location.getLatitude()); 
      setPlace_longitude((int) location.getLongitude()); 

      point = new GeoPoint(getPlace_latitude(), getPlace_longitude()); 

      controller.setCenter(point); 
     } 
    }; 

    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 900, 300, 
      listener); 
    // TODO 900 is the number of seconds to triger a location updat set it 
    // up in the prefrence 
    // TODO 300 is the minimum distace in metres to trigger a location up. 
    // set it up in settings 

    // pinin starts here 
    List<Overlay> mapOverlays = mapView.getOverlays(); 
    Drawable drawable = this.getResources().getDrawable(R.drawable.pin); 
    OverlayItems itemizedoverlay = new OverlayItems(drawable, this); 

    setPlat = (TextView) findViewById(R.id.set_plat); 
    setPlong = (TextView) findViewById(R.id.set_plong); 
    setPlat.setText("Latitude: " + getPlace_latitude()); 
    setPlong.setText("Longitude: " + getPlace_longitude()); 


    OverlayItem overlayitem = new OverlayItem(point, "Hello World!", 
      "I'm here!"); 


    itemizedoverlay.addOverlay(overlayitem); 
    mapOverlays.add(itemizedoverlay); 

    save = (Button) findViewById(R.id.save_btn); 
    save.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      chkInPlaceName = (EditText) findViewById(R.id.check_in_place_name); 
      chkSavePlaceName = chkInPlaceName.getText().toString(); 
      if (chkSavePlaceName.equals("")) { 
       Toast.makeText(CheckInActivity.this, "Enter Place name", 
         Toast.LENGTH_SHORT).show(); 

      } else { 
       DatabaseHandler db = new DatabaseHandler(
         CheckInActivity.this); 
       /** 
       * CRUD Operations 
       * */ 
       // Inserting Action 
       db.addPlace(new Places(chkSavePlaceName, 
         getPlace_latitude(), getPlace_longitude())); 
       startActivity(new Intent(CheckInActivity.this, 
         MainActivity.class)); 
       CheckInActivity.this.finish(); 

      } 

     } 
    }); 

    // pinning ends here 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.check_in, menu); 
    return true; 
} 

@Override 
protected boolean isRouteDisplayed() { 
    return false; 
} 

public int getPlace_longitude() { 
    return place_longitude; 
} 

public void setPlace_longitude(int place_longitude) { 
    this.place_longitude = place_longitude; 
} 

public int getPlace_latitude() { 
    return place_latitude; 
} 

public void setPlace_latitude(int place_latitude) { 
    this.place_latitude = place_latitude; 
} 

}

這是覆蓋類

public class OverlayItems extends ItemizedOverlay<OverlayItem>{ 

Context mContext; 

private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); 

public void addOverlay(OverlayItem overlay) { 
    mOverlays.add(overlay); 
    populate(); 
} 

public OverlayItems(Drawable defaultMarker, Context context) { 
    super(boundCenterBottom(defaultMarker)); 
    mContext = context; 
} 

@Override 
protected OverlayItem createItem(int i) { 
    return mOverlays.get(i); 
    } 

@Override 
public int size() { 
    return mOverlays.size(); 
} 

@Override 
protected boolean onTap(int index) { 
    OverlayItem item = mOverlays.get(index); 
    AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); 
    dialog.setTitle(item.getTitle()); 
    dialog.setMessage(item.getSnippet()); 
    dialog.show(); 
    return true; 
} 

}

+0

你可以粘貼你的類com.g6.georeminderv3.OverlayItems,具體的功能addOverlay – MikeIsrael

回答

0

的問題是,因爲你的觀點是空的。你應該驗證點是否爲null,因爲當你做OverlayItem overlayitem = new OverlayItem(point,「Hello World!」, 「我在這裏!」);該點爲空,onCreate在onLocationChanged之前調用。

對此評論抱歉,這是我在這裏的第一天。

+0

我認爲其餘的錯誤是不重要的,因爲它是藍色的顏色編碼。所有我編輯我的logcat幷包括它。 thx提前。 – CtJnx

+0

這應該是一個評論 – Krishnabhadra

+0

對不起,只是忘了做.. –

0

這是我的答案,但我只能猜測,因爲我不能做一個完整的代碼調試。

在中間你CheckInActivityonCreate(...)某處創建OverlayItem對象:

OverlayItem overlayitem = new OverlayItem(point, "Hello World!", "I'm here!"); 

參數是一個成員變量,一個GeoPoint。直到您的LocationManager指定第一個值onLocationChanged(),此變量纔會被初始化。由於LocationManager可能需要一段時間才能獲取位置並將其分配給GeoPoint,因此您之前可能會執行創建OverlayItem的行,因此point仍然是null

這是我的猜測,請通過適當的調試進行檢查。

+0

我'OverlayItem overlayitem =新的OverlayItem(點,「你好世界!」,「我在這裏!」);'是低於'onLocationChanged() '。所以我想知道如何在'GroPoint'初始化之前執行它。如果你說的是真的,你怎麼建議我去做呢 – CtJnx

+0

代碼在下面,但'onLocationChanged()'是你的'LocationManager'的一個回調函數,將在稍後的時間點被調用。它只是一個函數實現,而不是函數調用。 – Terry