Android 从Alertdialog启动另一个活动
我想从AlertDialog“ShowEditDialog”启动一个活动,但当我试图传递启动新活动的意图时,会得到“无法从静态上下文引用非静态方法” 我不能将AlertDialog设置为非静态的原因之一是我从AccountManager中的静态方法引用它。 有没有办法让它发挥作用?代码如下,“从静态到非静态”问题出现在b.setNeutralButton的第一个代码段中 如果需要的话,您可以随意询问其他代码 Alerts.class(相关部分的片段)(b.setNeutralButton中存在来自静态的非静态问题)Android 从Alertdialog启动另一个活动,android,android-intent,static,android-alertdialog,non-static,Android,Android Intent,Static,Android Alertdialog,Non Static,我想从AlertDialog“ShowEditDialog”启动一个活动,但当我试图传递启动新活动的意图时,会得到“无法从静态上下文引用非静态方法” 我不能将AlertDialog设置为非静态的原因之一是我从AccountManager中的静态方法引用它。 有没有办法让它发挥作用?代码如下,“从静态到非静态”问题出现在b.setNeutralButton的第一个代码段中 如果需要的话,您可以随意询问其他代码 Alerts.class(相关部分的片段)(b.setNeutralButton中存在来
公共类警报扩展活动{
公共静态警报对话框ShowEditDialog(最终上下文con、最终帐户Acc)
{
AlertDialog.Builder b=新建AlertDialog.Builder(con);
b、 setTitle(“账户详情”);
LayoutInflater li=LayoutInflater.from(con);
视图v=li.充气(R.layout.editdialog,null);
b、 setIcon(android.R.drawable.ic\u input\u get);
b、 setView(v);
最终文本视图txtName=(文本视图)v.findviewbyd(R.id.txtDelName);
最终文本视图txtmat=(文本视图)v.findviewbyd(R.id.txtDelAmt);
最终文本视图txtPurpose=(文本视图)v.findViewById(R.id.txtDelPurpose);
最终微调器微调项=(微调器)v.findViewById(R.id.spinTerm);
最终微调器spinStat=(微调器)v.findViewById(R.id.spinStat);
实用程序.ManageTermSpinner(con,spinTerm);
对于(int i=0;i而不是getBaseContext()
使用con
作为参数传递给ShowEditDialog
:
Intent myIntent = new Intent(con, AccountDetails.class);
con.startActivity(myIntent);
或者您也可以使用b.getContext()
获取Context
,将其作为第一个参数传递给Intent构造函数并访问startActivity
method您应该花一分钟来清理第二个代码段清理第二个代码段(我希望更好,很抱歉这样做),并指定“从静态到非静态”的问题是。谢谢,使用con
就足够了。@Astatine0936:刚刚添加了两种方式非常感谢,至少我现在知道了其他选择。再次感谢:)
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) {
}
}
Intent myIntent = new Intent(con, AccountDetails.class);
con.startActivity(myIntent);