Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7611759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:44:35+00:00 2026-05-31T01:44:35+00:00

I tried to call the method of another class, it is in the same

  • 0

I tried to call the method of another class, it is in the same package but when I test it on my phone, it will force close.

basically I create an object of LocalSQLite class, sqlite and use sqlite.method()
below is my code:

Sqlitetest2Activity.java

package com.test.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class Sqlitetest2Activity extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LocalSQLite sqlite = new LocalSQLite();
    sqlite.createTable();        
    }
}

LocalSQLite.java

package com.test.test;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

public class LocalSQLite extends Activity {
    SQLiteDatabase mydb;
    private static String DBNAME = "city.db";    // SQLITE DATABASE FILE NAME.
    private static String TABLE = "citytable";       // TABLE NAME

/* CREATE TABLE IF NOT EXISTS */
public void createTable(){
    try{
    mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
    mydb.execSQL("CREATE TABLE IF  NOT EXISTS "+ TABLE +" (ID INTEGER PRIMARY KEY, CITY TEXT);");
    mydb.close();
    }catch(Exception e){
        Toast.makeText(getApplicationContext(), "Error in creating table", Toast.LENGTH_LONG);
    }
}

/* DROPS TABLE */
public void dropTable(){
    try{
        mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
        mydb.execSQL("DROP TABLE " + TABLE);
        mydb.close();
    }catch(Exception e){
        Toast.makeText(getApplicationContext(), "Error encountered while dropping table.", Toast.LENGTH_LONG);
    }
}

/* THIS FUNCTION INSERTS DATA TO THE DATABASE */
public void insertIntoTable(String city_id, String city_name){
    try{
        mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
        mydb.execSQL("INSERT INTO " + TABLE + "(ID, CITY) VALUES('"+city_id+"','"+city_name+"')");
        mydb.close();
    }catch(Exception e){
        Toast.makeText(getApplicationContext(), "Error in inserting into table", Toast.LENGTH_LONG);
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:44:36+00:00Added an answer on May 31, 2026 at 1:44 am

    You can’t instantiate an Activity’s class object. To call a method, what you can do is –

    Put the method as static, and call it using LocalSQLite.createTable();

    I see an other mistake in your LocalSQLite class. You don’t have to extend Activity, when there is no screen associated with it. So just remove extends Activity, and that should solve your problem! 🙂

    Let me know if you have any questions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to create a generic method which can call overloaded methods? I tried but
Hi i want to call a C# method.. I already tryed with webmethod, but
I'm trying to use a method from within another method in a class. I
Original post: How to access a timer from another class in C# I tried
I've dug the manual and tried extensivelly to drop the extra function(){...} call after
I want to display a success msg after finishing an Ajax call. I tried
Tried to map it from Preferences -> Settings -> Keyboard, but the key combo
Tried a bunch of things but I can't get it to work consistently amid
tried to find the answer by googling and in MSDN but with no luck.
So, I need to make a method within a class protected from re-definition. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.