I want to start an activity from my AlertDialog "ShowEditDialog" but get "Non-static method cannot be referenced from a static context" when I try to pass an intent to start the new activity.
One of the reasons I can't just make my AlertDialog non-static is because I reference it from a static method in my AccountManager. Is there any way around this to get it to work? The codes are below, the "Non-static from static" issue is in the first snippet at b.setNeutralButton.
Feel free to ask for any other code if needed.
Alerts.class ( snippet of relevant part ) ( Non-Static from static problem is in b.setNeutralButton )
public class Alerts extends Activity {
public static AlertDialog ShowEditDialog(final Context con,final Account Acc)
{
AlertDialog.Builder b=new AlertDialog.Builder(con);
b.setTitle("Account Details");
LayoutInflater li=LayoutInflater.from(con);
View v=li.inflate(R.layout.editdialog, null);
b.setIcon(android.R.drawable.ic_input_get);
b.setView(v);
final TextView txtName=(TextView)v.findViewById(R.id.txtDelName);
final TextView txtAmt=(TextView)v.findViewById(R.id.txtDelAmt);
final TextView txtPurpose=(TextView)v.findViewById(R.id.txtDelPurpose);
final Spinner spinTerm=(Spinner)v.findViewById(R.id.spinTerm);
final Spinner spinStat=(Spinner)v.findViewById(R.id.spinStat);
Utilities.ManageTermSpinner(con, spinTerm);
for(int i=0;i<spinTerm.getCount();i++)
{
long id=spinTerm.getItemIdAtPosition(i);
if(id==Acc.getTerms())
{
spinTerm.setSelection(i, true);
break;
}
}
Utilities.ManageStatSpinner(con, spinStat);
for(int j=0;j<spinStat.getCount();j++)
{
long id=spinStat.getItemIdAtPosition(j);
if(id==Acc.getStatus())
{
spinStat.setSelection(j, true);
break;
}
}
txtName.setText(Acc.getName());
txtAmt.setText(String.valueOf(Acc.getAmt()));
b.setPositiveButton("Modify", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Acc.setName(txtName.getText().toString());
Acc.setAmt(Integer.valueOf(txtAmt.getText().toString()));
Acc.setPurpose(txtPurpose.getText().toString());
Acc.setTerms((int) spinTerm.getItemIdAtPosition(spinTerm.getSelectedItemPosition()));
Acc.setStatus((int) spinStat.getItemIdAtPosition(spinStat.getSelectedItemPosition()));
try
{
DatabaseHelper db=new DatabaseHelper(con);
db.UpdateAcc(Acc);
}
catch(Exception ex)
{
CatchError(con, ex.toString());
}
}
});
---HERE--->b.setNeutralButton("Full Details", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// getBaseContext() is where the problem is linked to
Intent myIntent = new Intent(getBaseContext(), AccountDetails.class);
startActivity(myIntent);
}
});
b.setNegativeButton("Cancel", null);
return b.create();
}
}
AccountManager.class (snippet of relative part )
public class AccountManager extends Activity {
DatabaseHelper dbHelper;
static public GridView grid;
TextView txtTest;
TextView txtTest2;
TextView txtAccounts;
TextView filter;
Spinner spinStat;
Spinner spinTerm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
grid = (GridView) findViewById(R.id.grid);
spinStat = (Spinner) findViewById(R.id.spinStat);
spinTerm = (Spinner) findViewById(R.id.spinTerm);
final DatabaseHelper db = new DatabaseHelper(this);
try {
grid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
try {
SQLiteCursor cr = (SQLiteCursor) parent.getItemAtPosition(position);
String name = cr.getString(cr.getColumnIndex(DatabaseHelper.colName));
int amount = cr.getInt(cr.getColumnIndex(DatabaseHelper.colAmount));
String purpose = cr.getString(cr.getColumnIndex(DatabaseHelper.colPurpose));
String Terms = cr.getString(cr.getColumnIndex(DatabaseHelper.colTermsClass));
String Status = cr.getString(cr.getColumnIndex(DatabaseHelper.colStatClass));
String date = cr.getString(cr.getColumnIndex(DatabaseHelper.colDate));
String editdate = cr.getString(cr.getColumnIndex(DatabaseHelper.colEditDate));
Account acc = new Account(name, amount, purpose, db.GetTermsID(Terms),date,editdate,db.GetStatID(Status));
acc.SetID((int) id);
---HERE--->AlertDialog diag = Alerts.ShowEditDialog(AccountManager.this, acc);
diag.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
ShowGrid();
}
});
diag.show();
} catch (Exception ex) {
Alerts.CatchError(AccountManager.this, ex.toString());
}
}
});
} catch (Exception ex) {
}
}
Instead of getBaseContext()
use con
which passing as parameter to ShowEditDialog
:
Intent myIntent = new Intent(con, AccountDetails.class);
con.startActivity(myIntent);
Or you can also get Context
using b.getContext()
for passing to Intent constructor as first parameter and accessing startActivity
method