基于SQLite查询填充活动并在Android中加入

时间:2013-01-12 16:35:16

标签: android sqlite mobile android-sqlite

我应该首先注意到我对Android开发非常陌生,所以如果我的问题看起来很愚蠢,那就很抱歉。我试图在应用程序中查询我的SQLite数据库,以动态创建活动上的内容。这是我的数据库助手文件中的查询

    public Cursor getWhereToEatActivity (int LocationId)
{
    Cursor cursor = myDataBase.rawQuery("SELECT Location.locationId as _id,Location.title,Location.desc, location.address, location.hours, location.website, location.phone FROM " +
            "TripWhereToEat JOIN Trip ON TripWhereToEat.tripId = Trip.tripId JOIN Location ON " +
            "Location.locationId = TripWhereToEat.locationId where TripWhereToEat.tripId = ?",
            new String[] {Integer.toString(LocationId)});

    cursor.moveToFirst();
    return cursor;
}

以下是选择项目的位置:

 @Override
              public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

                LocationListitem locationItem = (LocationListitem)lvItems.getItemAtPosition(position);

                int locationId = locationItem.getLocationId();

                // Pass the ID into the next activity
                Map<String, String> parms = new HashMap<String, String>();
                parms.put("locationId", Integer.toString(locationId));
                if (listType.equals("whattosee")) {
                    // switchActivity(WhatToSeeActivity.class, parms);
                }
                if (listType.equals("wheretostay")) {
                    // switchActivity(WhereToStayActivity.class, parms);
                }
                if (listType.equals("wheretoeat")) {
                    switchActivity(WhereToEatActivity.class, parms);
                }
              }
            });

以下是活动切换到:

public class WhereToEatActivity extends FragmentActivityBase {

private int LocationId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_where_to_eat);

    AndroidApplication app = (AndroidApplication)getApplication();

    DatabaseHelper myDbHelper = app.getDatabase(); // new DatabaseHelper(this);

    LocationId = Integer.valueOf(this.getParam("LocationId"));

    Cursor tripRecord = myDbHelper.getWhereToEatActivity(LocationId);

    tripRecord.moveToFirst();

    if (!tripRecord.isAfterLast()) {
        String name = tripRecord.getString(tripRecord.getColumnIndex("title"));
        String title = tripRecord.getString(tripRecord.getColumnIndex("desc"));
        String image = tripRecord.getString(tripRecord.getColumnIndex("image"));
        String address = tripRecord.getString(tripRecord.getColumnIndex("address"));

        ImageView mainImageView = (ImageView)findViewById(R.id.image);
        int resId = getResources().getIdentifier("com.integrativelogic.roadtrip:drawable/"+image, null, null); 
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),resId);
        if (bitmap != null)
            mainImageView.setImageBitmap (bitmap);    

        TextView nameView = (TextView)findViewById(R.id.name);
        nameView.setText(name);

        TextView titleView = (TextView)findViewById(R.id.title);

        titleView.setText(Html.fromHtml(title));
        titleView.setMovementMethod(LinkMovementMethod.getInstance());

        TextView addressView = (TextView)findViewById(R.id.address);
        addressView.setText(address);
    }

    tripRecord.close();
}

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

这是我从Logcat得到的错误:

01-12 11:23:52.186: E/AndroidRuntime(15238): FATAL EXCEPTION: main
01-12 11:23:52.186: E/AndroidRuntime(15238): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.integrativelogic.roadtrip/com.integrativelogic.roadtrip.WhereToEatActivity}: java.lang.NumberFormatException: unable to parse '' as integer
01-12 11:23:52.186: E/AndroidRuntime(15238):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1821)
01-12 11:23:52.186: E/AndroidRuntime(15238):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
01-12 11:23:52.186: E/AndroidRuntime(15238):    at android.app.ActivityThread.access$1500(ActivityThread.java:132)
01-12 11:23:52.186: E/AndroidRuntime(15238):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
01-12 11:23:52.186: E/AndroidRuntime(15238):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 11:23:52.186: E/AndroidRuntime(15238):    at android.os.Looper.loop(Looper.java:150)
01-12 11:23:52.186: E/AndroidRuntime(15238):    at android.app.ActivityThread.main(ActivityThread.java:4263)
01-12 11:23:52.186: E/AndroidRuntime(15238):    at java.lang.reflect.Method.invokeNative(Native Method)
01-12 11:23:52.186: E/AndroidRuntime(15238):    at java.lang.reflect.Method.invoke(Method.java:507)
01-12 11:23:52.186: E/AndroidRuntime(15238):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-12 11:23:52.186: E/AndroidRuntime(15238):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-12 11:23:52.186: E/AndroidRuntime(15238):    at dalvik.system.NativeStart.main(Native Method)
01-12 11:23:52.186: E/AndroidRuntime(15238): Caused by: java.lang.NumberFormatException: unable to parse '' as integer
01-12 11:23:52.186: E/AndroidRuntime(15238):    at java.lang.Integer.parseInt(Integer.java:362)
01-12 11:23:52.186: E/AndroidRuntime(15238):    at java.lang.Integer.parseInt(Integer.java:332)
01-12 11:23:52.186: E/AndroidRuntime(15238):    at java.lang.Integer.valueOf(Integer.java:506)
01-12 11:23:52.186: E/AndroidRuntime(15238):    at com.integrativelogic.roadtrip.WhereToEatActivity.onCreate(WhereToEatActivity.java:27)
01-12 11:23:52.186: E/AndroidRuntime(15238):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
01-12 11:23:52.186: E/AndroidRuntime(15238):    at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)

2 个答案:

答案 0 :(得分:0)

因为您是新手我会在您的应用突然崩溃时解释一下调试过程:

第一部分是读取LogCat输出的最后几行:

  
    

引起:java.lang.NumberFormatException:无法解析&#39;&#39;作为整数

  

所以我们知道尝试使空字符串为整数是一个问题。您只能从包含数字的String中创建一个整数。

在Log下面的那几行,你会看到

  
    

at com.integrativelogic.roadtrip.WhereToEatActivity.onCreate(WhereToEatActivity.java:27)

  

所以我们知道它发生在你的WhereToEatActivity的第27行,我不会计算你在这里发布的内容,但我假设它是这一行

LocationId = Integer.valueOf(this.getParam("LocationId"));

因此我认为您可以安全地认为您遇到此错误的原因是因为没有参数&#34; LocationId&#34;或参数为空。

答案 1 :(得分:0)

你的错误就在这里:

Caused by: java.lang.NumberFormatException: unable to parse '' as integer

我怀疑你在这里有一个空位:

LocationId = Integer.valueOf(this.getParam("LocationId"));

如果在此处引用该行:

at com.integrativelogic.roadtrip.WhereToEatActivity.onCreate(WhereToEatActivity.java:27)