Tổng hợp Sửa giúp lỗi NullPointerException

Thảo luận trong 'Thảo luận về lập trình Android' bắt đầu bởi LexusCMT, 30/8/16.

  1. LexusCMT

    LexusCMT New Member

    Lượt xem: 263
    em hiện dang làm một app lưu trự các Marker do mình tự chọn trên google maps vào DB(Sqlite)
    em có tham khảo một vài ví dụ nhưng luôn bị lỗi này. Mong mọi người sẻ giúp đỡ em
    mainActivity.java
    Mã:
    package com.example.tuanchau.mapobj;
     
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.location.Criteria;
    import android.location.Location;
    import android.location.LocationManager;
    import android.support.v4.app.FragmentActivity;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.EditText;
     
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.OnMapReadyCallback;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.BitmapDescriptorFactory;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.Marker;
    import com.google.android.gms.maps.model.MarkerOptions;
     
    import java.util.List;
     
    public class MainActivity extends FragmentActivity  {
     
    	Context context = this;
    	GoogleMap googlemap;
    	MarkerDataSource data;
     
    	/**
    	 * Called when the activity is first created.
    	 */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		initMap();
    		//addTwittertoMap();
    		googlemap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
     
    			public void onInfoWindowClick(Marker marker) {
    				marker.remove();
    				data.deleteMarker(new MyMarkerObj(marker.getTitle(), marker.getSnippet(), marker.getPosition().latitude + " " + marker.getPosition().longitude));
    			}
    		});
    		googlemap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
     
    			public void onMapLongClick(final LatLng latlng) {
    				LayoutInflater li = LayoutInflater.from(context);
    				final View v = li.inflate(R.layout.alertdialog, null);
    				AlertDialog.Builder builder = new AlertDialog.Builder(context);
    				builder.setView(v);
    				builder.setCancelable(false);
     
     
    				builder.setPositiveButton("Create", new DialogInterface.OnClickListener() {
     
    					public void onClick(DialogInterface dialog, int which) {
    						EditText title = (EditText) v.findViewById(R.id.ettitle);
    						EditText snippet = (EditText) v.findViewById(R.id.etsnippet);
    						googlemap.addMarker(new MarkerOptions()
    								.title(title.getText().toString())
    								.snippet(snippet.getText().toString())
    								.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
    								.position(latlng)
    						);
    						String sll = latlng.latitude + " " + latlng.longitude;
    						data.addMarker(new MyMarkerObj(title.getText().toString(), snippet.getText().toString(), sll));
    					}
    				});
     
    				builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
     
    					public void onClick(DialogInterface dialog, int which) {
    						dialog.cancel();
    					}
    				});
     
    				AlertDialog alert = builder.create();
    				alert.show();
     
    			}
    		});
    	}
     
    	public void onLocationChanged(Location location) {
    	}
     
    	public void onStatusChanged(String provider, int status, Bundle extras) {
    	}
     
    	public void onProviderEnabled(String provider) {
    	}
     
    	public void onProviderDisabled(String provider) {
    		AlertDialog.Builder builder = new AlertDialog.Builder(this);
    		builder.setTitle("Phone is in airplane mode");
    		builder.setCancelable(false);
    		builder.setPositiveButton("Enable GPS", new DialogInterface.OnClickListener() {
     
    			public void onClick(DialogInterface dialog, int which) {
    				Intent startGps = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    				startActivity(startGps);
    			}
    		});
    		builder.setNegativeButton("Leave GPS off", new DialogInterface.OnClickListener() {
     
    			public void onClick(DialogInterface dialog, int which) {
    				dialog.cancel();
    			}
    		});
     
    		AlertDialog alert = builder.create();
    		alert.show();
    	}
    	private void initMap(){
    		
    		SupportMapFragment mf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    		mf.getMapAsync(new OnMapReadyCallback() {
    			@Override
    			public void onMapReady(GoogleMap googleMap) {
    				// Add a marker in Sydney and move the camera
    				LatLng sydney = new LatLng(-34, 151);
    				googlemap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    				googlemap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    			}
    		});
    	}
     
    	private void addTwittertoMap() {
    		LatLng pos = new LatLng(37.7769904, -122.4169725);
    		MarkerOptions mm = new MarkerOptions();
    		googlemap.addMarker(new MarkerOptions()
    				.title("Twitter")
    				.snippet("Twitter HQ")
    				.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
    				.position(pos)
    		);
     
    	}
     
    	@Override
    	protected void onPause() {
    		data.close();
    		super.onPause();
    	}
     
    	@Override
    	protected void onResume() {
    		try {
    			data.open();
     
    		} catch (Exception e) {
    			Log.i("hello", "hello");
    		}
    		super.onResume();
    	}
     
     
     
    }
    
    MySQLHelper.java
    Mã:
    package com.example.tuanchau.mapobj;
     
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
     
    /**
     * Created by tuanchau on 8/30/16.
     */
    public class MySQLHelper extends SQLiteOpenHelper {
     
    	public static final String TABLE_NAME = "locations";
     
    	public static final String ID_COL = "loc_id";
    	public static final String TITLE = "loc_title";
    	public static final String SNIPPET = "loc_snippet";
    	public static final String POSITION = "loc_position";
    	private static final int D_VERSION = 1;
     
    	private static final String DB_NAME = "markerlocations.db";
    	private static final String DB_CREATE =
    			"create table "+ TABLE_NAME + "("
    					+ ID_COL + " integer primary key autoincrement, "
    					+ TITLE + " text, "
    					+ SNIPPET + " text, "
    					+ POSITION + " text);"
    			;
    	public MySQLHelper(Context context) {
    		super(context, DB_NAME, null, D_VERSION);
    	}
     
     
    	@Override
    	public void onCreate(SQLiteDatabase db) {
    		db.execSQL(DB_CREATE);
    	}
     
    	@Override
    	public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    		db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
    		onCreate(db);
    	}
    }
    
    MyMarkerObj.java
    Mã:
    package com.example.tuanchau.mapobj;
     
    /**
     * Created by tuanchau on 8/30/16.
     */
    public class MyMarkerObj {
    	private long id;
    	private String title;
    	private String snippet;
    	private String position;
     
    	public MyMarkerObj() {
    	}
     
    	public MyMarkerObj(long id, String title, String snippet, String position) {
    		this.id = id;
    		this.title = title;
    		this.snippet = snippet;
    		this.position = position;
    	}
    	public MyMarkerObj(String title, String snippet, String position) {
    		this.title = title;
    		this.snippet = snippet;
    		this.position = position;
    	}
     
    	/**
    	 * @return the id
    	 */
    	public long getId() {
    		return id;
    	}
     
    	/**
    	 * @param id the id to set
    	 */
    	public void setId(long id) {
    		this.id = id;
    	}
     
    	/**
    	 * @return the title
    	 */
    	public String getTitle() {
    		return title;
    	}
     
    	/**
    	 * @param title the title to set
    	 */
    	public void setTitle(String title) {
    		this.title = title;
    	}
     
    	/**
    	 * @return the snippet
    	 */
    	public String getSnippet() {
    		return snippet;
    	}
     
    	/**
    	 * @param snippet the snippet to set
    	 */
    	public void setSnippet(String snippet) {
    		this.snippet = snippet;
    	}
     
    	/**
    	 * @return the position
    	 */
    	public String getPosition() {
    		return position;
    	}
     
    	/**
    	 * @param position the position to set
    	 */
    	public void setPosition(String position) {
    		this.position = position;
    	}
     
     
     
    }
    
    MarkerDataSource.java
    Mã:
    package com.example.tuanchau.mapobj;
     
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
     
    import java.util.ArrayList;
    import java.util.List;
     
    /**
     * Created by tuanchau on 8/30/16.
     */
    public class MarkerDataSource {
     
    	MySQLHelper dbhelper;
    	SQLiteDatabase db;
     
    	String[] cols = {MySQLHelper.TITLE, MySQLHelper.SNIPPET, MySQLHelper.POSITION};
     
    	public MarkerDataSource(Context c) {
    		dbhelper = new MySQLHelper(c);
     
    	}
     
    	public void open() throws SQLException {
    		db = dbhelper.getWritableDatabase();
    	}
     
    	public void close(){
    		db.close();
    	}
     
    	public void addMarker(MyMarkerObj m){
    		ContentValues v = new ContentValues();
     
    		v.put(MySQLHelper.TITLE, m.getTitle());
    		v.put(MySQLHelper.SNIPPET, m.getSnippet());
    		v.put(MySQLHelper.POSITION, m.getPosition());
     
    		db.insert(MySQLHelper.TABLE_NAME, null, v);
     
    	}
     
    	public List<MyMarkerObj> getMyMarkers(){
    		List<MyMarkerObj> markers = new ArrayList<MyMarkerObj>();
     
    		Cursor cursor = db.query(MySQLHelper.TABLE_NAME, cols, null, null, null, null, null);
     
    		cursor.moveToFirst();
    		while (!cursor.isAfterLast()) {
    			MyMarkerObj m = cursorToMarker(cursor);
    			markers.add(m);
    			cursor.moveToNext();
    		}
    		cursor.close();
     
     
    		return markers;
    	}
     
    	public void deleteMarker(MyMarkerObj m){
    		db.delete(MySQLHelper.TABLE_NAME, MySQLHelper.POSITION + " = '" + m.getPosition() + "'", null);
    	}
     
     
    	private MyMarkerObj cursorToMarker(Cursor cursor) {
    		MyMarkerObj m = new MyMarkerObj();
    		m.setTitle(cursor.getString(0));
    		m.setSnippet(cursor.getString(1));
    		m.setPosition(cursor.getString(2));
    		return m;
    	}
     
     
    }
    
    Em xin cảm ơn tất cả mọi người ạ
    android, google map, map

Chia sẻ trang này