2012-09-23 97 views
0

我正在開發一個應用程序,當我按下按鈕時顯示地圖上的位置。將數據從數據庫傳遞到活動

但是,我想要得到的經緯度數據,這個按鈕被接收,從數據庫中。

我已經有一個名爲「DetaljiProizvoda」的活動,它從數據庫中獲取項目的詳細信息,並在數據庫中爲緯度和經度添加了(在文件DatabaseHelper.java中)int類型的列。

我想經緯度數據傳遞給GM​​apsActivity.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); 

    }  

DatabaseHelper.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 

logcat的:

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 
+0

首先嚐試檢查遊標是否爲null而不是(cursor.getCount()== 1) –

+0

爲什麼我應該這樣做?我的意思是爲什麼遊標必須爲空? – Noctilux

+0

如果沒有獲得的數據,那麼遊標將間接沒有數據,它將爲空,並使用cursor.getCount()可能會麻煩你 –

回答

0

你的onCreate月底關閉光標()方法。然後在方法changeActivity()中嘗試使用它。

+0

我已經改變了這一點,但是當我輸入最後一個活動(GMapsActivity)時,即顯示地圖位置時,我的應用程序仍然處於崩潰狀態。你能寫完整的代碼嗎? – Noctilux

+0

你能否提供一個新的logcat(修復光標問題後的一個)? – sinisha

+0

我在開始時就做到了,因爲我可以看到。我在onCreate()方法結束時關閉了遊標,然後我嘗試在changeActivity()中使用它。那麼我做錯了什麼? – Noctilux

相關問題