1

I'm trying to build an app that extracts url from the database according to the intent data passed by the main activity

I've implemented the code but the app keeps crashing/force closes.

Here's my DBHelper class

package com.snplabs.learncpp;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

final protected static String DATABASE_NAME="cppreference";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null,2);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion >= newVersion) return;
db.execSQL("DROP DATABASE IF EXISTS " + DATABASE_NAME +";");
onCreate(db);
}
@Override
public void onCreate(SQLiteDatabase db) {

//here is the database definition
db.execSQL("CREATE TABLE cppref " +
"(_id INTEGER PRIMARY KEY, link TEXT);");
//insert pre-configured records
db.execSQL("INSERT INTO cppref (_id, link) VALUES(1,'URL_HERE');");
db.execSQL("INSERT INTO cppref (_id, link) VALUES(2,'URL_HERE');");
db.execSQL("INSERT INTO cppref (_id, link) VALUES(3,'URL_HERE');");
db.execSQL("INSERT INTO cppref (_id, link) VALUES(4,'URL_HERE');");
db.execSQL("INSERT INTO cppref (_id, link) VALUES(5,'URL_HERE');");
  }


}

And here's the class that uses DBHelper to get the url:

package com.snplabs.learncpp;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

public class ViewItem extends Activity{
private DBHelper dbhelper=new DBHelper(this);
private SQLiteDatabase db;
private String pgurl;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.item_view);

    db = dbhelper.getReadableDatabase();

    TextView txtProduct = (TextView) findViewById(R.id.product_label);

    Intent i = getIntent();
    // getting attached intent data
    String product = i.getStringExtra("product");
    int data=Integer.parseInt(product.substring(4));
    // displaying selected product name

    txtProduct.setText(product);

    Cursor quer=db.rawQuery("SELECT link FROM cppref "+"WHERE _id='"+data+"';", null);
            //-fetch record
            if(quer.getCount()!=0){
            quer.moveToFirst();//go to first row
            pgurl=quer.getString(1).toString();
            }
            else{
            //display some notice here saying no data found
            txtProduct.setText("Error");
            }

    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.setWebViewClient(new WebViewClient());
    myWebView.loadUrl(pgurl);
}

}

1
  • post your logcat then. Commented Mar 26, 2014 at 5:57

1 Answer 1

1

First correct this Move this

 DBHelper dbhelper=new DBHelper(this);

Inside onCreate(......) after setContentView(.....)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.