从 Alertdialog 启动另一个 Activity

Starting another Activity from an Alertdialog

我想从我的 AlertDialog "ShowEditDialog" 启动一个 activity,但是当我尝试传递启动新 activity 的意图时得到 "Non-static method cannot be referenced from a static context"。

我不能让我的 AlertDialog 成为非静态的原因之一是因为我从我的 AccountManager 中的静态方法引用它。 有什么办法让它工作吗?代码如下,"Non-static from static" 问题在 b.setNeutralButton 的第一个片段中。

如有需要,请随时索取任何其他代码。

Alerts.class(相关部分的片段)(来自静态的非静态问题在 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(相关部分的片段)

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) {
  }
}

而不是 getBaseContext() 使用 con 作为参数传递给 ShowEditDialog :

Intent myIntent = new Intent(con, AccountDetails.class);
con.startActivity(myIntent);

或者您也可以使用 b.getContext() 获取 Context 作为第一个参数传递给 Intent 构造函数并访问 startActivity 方法