java - the method getactivity() is undefined for the type Use In activity_main for User Database -
i trying create user database on activity_main page when users sign app, lead them next major page. here activitymain.java file currently:
package com.example.awesomefilebuilder; import android.content.contentvalues; import android.content.intent; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class mainactivity extends base_activity { public final static string extra_message = "com.example.awesomefilebuilder.message"; public final string tag = "mainactivity"; public final string edit_message ="username"; public static final string database_name = "data"; public static final string table_name = "comments_table"; public static final string c_id = "_id"; public static final string name = "name"; public static final string comment = "comment"; public static final string email = "email"; public static final string time = "time"; public static final string password = "password"; public static final int version = 1; view view; sqlitedatabase db; dbhelper dbhelper; public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { dbhelper dbhelper = new dbhelper(getactivity()); db = dbhelper.getwritabledatabase(); view = inflater.inflate(r. layout.activity_main, container,false); contentvalues cv = new contentvalues(); cv.put(dbhelper.name, name); cv.put(dbhelper.comment, comment); cv.put(dbhelper.email, email); cv.put(dbhelper.password, password); db.insert(dbhelper.table_name, null, cv); button query = (button) view.findviewbyid(r.id.query); query.setonclicklistener(new onclicklistener() { public void onclick(view arg0) { string [] columns = {dbhelper.name, dbhelper.comment, dbhelper.email}; cursor cursor = db.query(dbhelper.table_name, null, null, null, null, null, null); cursor.movetofirst(); while(cursor.movetonext()) { string name = cursor.getstring(cursor.getcolumnindex(dbhelper.name)); string comment = cursor.getstring(cursor.getcolumnindex(dbhelper.comment)); string password = cursor.getstring(cursor.getcolumnindex(dbhelper.password)); string email = cursor.getstring(cursor.getcolumnindex(dbhelper.email)); toast.maketext(getactivity(), "name = "+ name +"/ncomment= "+ comment,toast.length_short).show(); } cursor.close(); } }); return view; } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); log.i(tag, "oncreate" ); } @override public void ondestroy() { super.ondestroy(); // call superclass // stop method tracing activity started during oncreate() android.os.debug.stopmethodtracing(); } /** called when user clicks send button */ public void sendmessage(view view) { intent intent = new intent(this, homepagemain.class); edittext edittext = (edittext) findviewbyid(r.id.createpicture); string message = edittext.gettext().tostring(); intent.putextra(extra_message, message); startactivity(intent); log.i(tag, "sendmessage" ); } }
i read around on site , understand getactivity() option should used on fragments, don't want use fragment simple user login screen. can either replace getactivity() option, or in general fix error? also, not want have connect seperate database though have 1 readily avalible use when can create database in application, have done. if needs see pages of coding make easier solve please let me know. in advance! :)
you cannot use getactivity() in activity class because method of fragment class. in case can :
dbhelper dbhelper = new dbhelper(this);
or
dbhelper dbhelper = new dbhelper(mainactivity.this);
or
dbhelper dbhelper = new dbhelper(getapplicationcontext());
note: code pasted here syntactically wrong because oncreateview() not method of activity class , method of fragment class. should write code in oncreate() method written in oncreateview() method.strong text
Comments
Post a Comment