我正在开发一个应用程序,当我按下按钮时,它会在地图上显示位置。
但是,我想从数据库中获取此按钮正在接收的纬度和经度数据。
我已经有一个名为“DetaljiProizvoda”的活动,它从数据库中获取项目的详细信息,并且我在数据库中添加了(在文件 DatabaseHelper.java 中)int 类型的纬度和经度列。
我想将纬度和经度数据传递给 GMapsActivity.java 文件并使用它们来显示地图上每个项目的位置。
我在 LogCat 中遇到这些错误,请帮助:
09-23 18:07:52.986: E/AndroidRuntime(22995): FATAL EXCEPTION: main
09-23 18:07:52.986: E/AndroidRuntime(22995): java.lang.IllegalStateException: Could not execute method of the activity
09-23 18:07:52.986: E/AndroidRuntime(22995): at android.view.View$1.onClick(View.java:3044)
09-23 18:07:52.986: E/AndroidRuntime(22995): at android.view.View.performClick(View.java:3511)
09-23 18:07:52.986: E/AndroidRuntime(22995): at android.view.View$PerformClick.run(View.java:14105)
09-23 18:07:52.986: E/AndroidRuntime(22995): at android.os.Handler.handleCallback(Handler.java:605)
09-23 18:07:52.986: E/AndroidRuntime(22995): at android.os.Handler.dispatchMessage(Handler.java:92)
09-23 18:07:52.986: E/AndroidRuntime(22995): at android.os.Looper.loop(Looper.java:137)
09-23 18:07:52.986: E/AndroidRuntime(22995): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-23 18:07:52.986: E/AndroidRuntime(22995): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 18:07:52.986: E/AndroidRuntime(22995): at java.lang.reflect.Method.invoke(Method.java:511)
09-23 18:07:52.986: E/AndroidRuntime(22995): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-23 18:07:52.986: E/AndroidRuntime(22995): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-23 18:07:52.986: E/AndroidRuntime(22995): at dalvik.system.NativeStart.main(Native Method)
09-23 18:07:52.986: E/AndroidRuntime(22995): Caused by: java.lang.reflect.InvocationTargetException
09-23 18:07:52.986: E/AndroidRuntime(22995): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 18:07:52.986: E/AndroidRuntime(22995): at java.lang.reflect.Method.invoke(Method.java:511)
09-23 18:07:52.986: E/AndroidRuntime(22995): at android.view.View$1.onClick(View.java:3039)
09-23 18:07:52.986: E/AndroidRuntime(22995): ... 11 more
09-23 18:07:52.986: E/AndroidRuntime(22995): Caused by: java.lang.NullPointerException
09-23 18:07:52.986: E/AndroidRuntime(22995): at hr.punctum.LociranjePonudaProizvoda.DetaljiProizvoda.changeActivity(DetaljiProizvoda.java:66)
09-23 18:07:52.986: E/AndroidRuntime(22995): ... 14 more
DetaljiProizvoda.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detalji_proizvoda);
proizvodId = getIntent().getIntExtra("PROIZVODI_ID", 0);
SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT proizv._id, proizv.naziv, proizv.tvrtka, proizv.cijena, proizv.kategorija, proizv.telefonTvrtke, proizv.adresaTvrtke, proizv.latitude, proizv.longitude FROM proizvodi proizv LEFT OUTER JOIN proizvodi mgr ON proizv._id = mgr._id WHERE proizv._id = ?",
new String[]{""+proizvodId});
if (cursor.getCount() == 1)
{
cursor.moveToFirst();
naziv = (TextView) findViewById(R.id.naziv);
naziv.setText(cursor.getString(cursor.getColumnIndex("naziv")));
kategorija = (TextView) findViewById(R.id.kategorija);
kategorija.setText(cursor.getString(cursor.getColumnIndex("kategorija")));
tvrtka = (TextView) findViewById(R.id.tvrtka);
tvrtka.setText(cursor.getString(cursor.getColumnIndex("tvrtka")));
telefonTvrtke = (TextView) findViewById(R.id.telefonTvrtke);
telefonTvrtke.setText(cursor.getString(cursor.getColumnIndex("telefonTvrtke")));
adresaTvrtke = (TextView) findViewById(R.id.adresaTvrtke);
adresaTvrtke.setText(cursor.getString(cursor.getColumnIndex("adresaTvrtke")));
}
cursor.close();
db.close();
}
public void changeActivity(View view) {
Intent intent = new Intent(DetaljiProizvoda.this, GMapsActivity.class);
intent.putExtra("PROIZVODI_ID", cursor.getColumnIndex("_id"));
intent.putExtra("PROIZVODI_LATITUDE", cursor.getColumnIndex("latitude"));
intent.putExtra("PROIZVODI_LONGITUDE", cursor.getColumnIndex("longitude"));
startActivity(intent);
}
}
GMapsActivity.java
公共类 GMapsActivity 扩展 MapActivity {
private MapView mapView;
protected int latitudeE6;
protected int longitudeE6;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gmaps_layout);
latitudeE6 = getIntent().getIntExtra("PROIZVODI_LATITUDE", 0);
longitudeE6 = getIntent().getIntExtra("PROIZVODI_LONGITUDE", 0);
mapView = (MapView) findViewById(R.id.map_view);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this);
GeoPoint point = new GeoPoint(latitudeE6, longitudeE6);
OverlayItem overlayitem = new OverlayItem(point, "Dobrodošli u " , "tvrtku Protis");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
MapController mapController = mapView.getController();
mapController.animateTo(point);
mapController.setZoom(18);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
ListaProizvoda.java
package hr.punctum.LociranjePonudaProizvoda;
public class ListaProizvoda extends ListActivity {
protected EditText searchText;
protected SQLiteDatabase db;
protected Cursor cursor;
protected ListAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = (new DatabaseHelper(this)).getWritableDatabase();
searchText = (EditText) findViewById (R.id.searchText);
}
@SuppressWarnings("deprecation")
//kreiranje metode Search za pretraživanje proizvoda u bazi podataka
public void search(View view) {
// slijedeci sql query vraca rezultat iz baze svih zapisa koji pocinju na slovo uneseno u search polje
cursor = db.rawQuery("SELECT _id, naziv, tvrtka, cijena FROM proizvodi WHERE naziv LIKE ?",
new String[]{searchText.getText().toString() + "%"});
adapter = new SimpleCursorAdapter(this,R.layout.proizvodi_list_artikl,cursor,new String[] {"naziv", "tvrtka", "cijena"},new int[] {R.id.naziv, R.id.tvrtka, R.id.cijena});
setListAdapter(adapter);
}
public void onListItemClick(ListView parent, View view, int position, long id) {
//kreiranje Intenta za komunikaciju s drugim activity-om
Intent intent = new Intent(this, DetaljiProizvoda.class);
Cursor cursor = (Cursor) adapter.getItem(position);
//slanje varijable
intent.putExtra("PROIZVODI_ID", cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(intent);
}
数据库助手.java
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "proizvodidb";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 4);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS proizvodi (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"naziv TEXT, " +
"tvrtka TEXT, " +
"cijena TEXT, " +
"kategorija TEXT, " +
"telefonTvrtke TEXT, " +
"adresaTvrtke TEXT, " +
"latitude INTEGER, " +
"longitude INTEGER)";
db.execSQL(sql);
ContentValues values = new ContentValues();
values.put("naziv", "Nikon D800");
values.put("tvrtka", "MagazinRS");
values.put("cijena", "21600");
values.put("kategorija", "fotoaparati");
values.put("telefonTvrtke", "454562542");
values.put("adresaTvrtke", "Laniste 1B");
values.put("latitude", 458011234);
values.put("longitude", 159520583);
db.insert("proizvodi", "tvrtka", values);
....
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS proizvodi");
onCreate(db);
}
}
detalji_proizvoda.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="12dp"
android:paddingLeft="12dp">
<TextView
android:id="@+id/naziv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/kategorija"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tvrtka"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/telefonTvrtke"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/adresaTvrtke"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnButton"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="left"
android:onClick="changeActivity"
android:text="@string/prikazi_lokaciju"
android:textSize="12dp" />
</LinearLayout>
新的 logcat(在 changeActivity 方法后关闭游标后):
09-23 20:04:30.050: E/AndroidRuntime(995): FATAL EXCEPTION: main
09-23 20:04:30.050: E/AndroidRuntime(995): java.lang.IllegalStateException: Could not execute method of the activity
09-23 20:04:30.050: E/AndroidRuntime(995): at android.view.View$1.onClick(View.java:3044)
09-23 20:04:30.050: E/AndroidRuntime(995): at android.view.View.performClick(View.java:3511)
09-23 20:04:30.050: E/AndroidRuntime(995): at android.view.View$PerformClick.run(View.java:14105)
09-23 20:04:30.050: E/AndroidRuntime(995): at android.os.Handler.handleCallback(Handler.java:605)
09-23 20:04:30.050: E/AndroidRuntime(995): at android.os.Handler.dispatchMessage(Handler.java:92)
09-23 20:04:30.050: E/AndroidRuntime(995): at android.os.Looper.loop(Looper.java:137)
09-23 20:04:30.050: E/AndroidRuntime(995): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-23 20:04:30.050: E/AndroidRuntime(995): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 20:04:30.050: E/AndroidRuntime(995): at java.lang.reflect.Method.invoke(Method.java:511)
09-23 20:04:30.050: E/AndroidRuntime(995): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-23 20:04:30.050: E/AndroidRuntime(995): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-23 20:04:30.050: E/AndroidRuntime(995): at dalvik.system.NativeStart.main(Native Method)
09-23 20:04:30.050: E/AndroidRuntime(995): Caused by: java.lang.reflect.InvocationTargetException
09-23 20:04:30.050: E/AndroidRuntime(995): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 20:04:30.050: E/AndroidRuntime(995): at java.lang.reflect.Method.invoke(Method.java:511)
09-23 20:04:30.050: E/AndroidRuntime(995): at android.view.View$1.onClick(View.java:3039)
09-23 20:04:30.050: E/AndroidRuntime(995): ... 11 more
09-23 20:04:30.050: E/AndroidRuntime(995): Caused by: java.lang.NullPointerException
09-23 20:04:30.050: E/AndroidRuntime(995): at hr.punctum.LociranjePonudaProizvoda.DetaljiProizvoda.changeActivity(DetaljiProizvoda.java:65)
09-23 20:04:30.050: E/AndroidRuntime(995): ... 14 more
日志猫:
09-23 22:46:14.652: E/AndroidRuntime(680): FATAL EXCEPTION: main
09-23 22:46:14.652: E/AndroidRuntime(680): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{hr.punctum.LociranjePonudaProizvoda/hr.punctum.LociranjePonudaProizvoda.ListaProizvoda}: java.lang.ClassNotFoundException: hr.punctum.LociranjePonudaProizvoda.ListaProizvoda
09-23 22:46:14.652: E/AndroidRuntime(680): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
09-23 22:46:14.652: E/AndroidRuntime(680): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-23 22:46:14.652: E/AndroidRuntime(680): at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-23 22:46:14.652: E/AndroidRuntime(680): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-23 22:46:14.652: E/AndroidRuntime(680): at android.os.Handler.dispatchMessage(Handler.java:99)
09-23 22:46:14.652: E/AndroidRuntime(680): at android.os.Looper.loop(Looper.java:137)
09-23 22:46:14.652: E/AndroidRuntime(680): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-23 22:46:14.652: E/AndroidRuntime(680): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 22:46:14.652: E/AndroidRuntime(680): at java.lang.reflect.Method.invoke(Method.java:511)
09-23 22:46:14.652: E/AndroidRuntime(680): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-23 22:46:14.652: E/AndroidRuntime(680): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-23 22:46:14.652: E/AndroidRuntime(680): at dalvik.system.NativeStart.main(Native Method)
09-23 22:46:14.652: E/AndroidRuntime(680): Caused by: java.lang.ClassNotFoundException: hr.punctum.LociranjePonudaProizvoda.ListaProizvoda
09-23 22:46:14.652: E/AndroidRuntime(680): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
09-23 22:46:14.652: E/AndroidRuntime(680): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
09-23 22:46:14.652: E/AndroidRuntime(680): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
09-23 22:46:14.652: E/AndroidRuntime(680): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
09-23 22:46:14.652: E/AndroidRuntime(680): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
09-23 22:46:14.652: E/AndroidRuntime(680): ... 11 more