1

Ormlite を使用して持続層を実装しています。コードのいくつかの部分で再利用できるボタンがアプリケーションに含まれているため、 を実装して拡張するクラスを作成したいと思いView.OnClickListenerます。しかし、それを行って、クラスのメソッド内にオブジェクトを保持するように Dao を取得しようとすると、呼び出されなかったという が表示されます。私は以下のようにしています:OrmLiteBaseActivity<DataBaseHelper>DataBaseHelperonClick()ButtonIllegalArgumentExceptiononCreate()

saveButton = (Button) findViewById(R.id.SaveProfileButton);
saveButton.setTag(profile);
saveButton.setOnClickListener(new SaveButton());

そして、SaveButtonクラスで:

Dao<Profile, Long> profileDao = null;
Profile profileToSave = null;

try {
    profileDao = getHelper().getProfileDao();
} catch (SQLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();   
}

を評価するときにエラーが発生しますgetHelper。何か案は?前もって感謝します!

4

1 に答える 1

1

そのような Activity() クラスをインスタンス化することはできません。システムによって開始されたアクティビティのみを使用できます。そうしないと、コンテキストが正しく設定されません。

onClickListener を設定するアクティビティでは、(this) を渡してそこでクリック イベントを処理するか、次のことを試すことができます。

編集:

saveButton = (Button) findViewById(R.id.SaveProfileButton);
saveButton.setTag(profile);
saveButton.setOnClickListener(new SaveButtonListener(this));

SaveButtonListener.java:

public class SaveButtonListener extends OnClickListener() { 

    private Activity context;

    public SaveButton(Activity c) {
        this.context = c;
    }

    @Override
    public void onClick(...) {
        ... your logic ...
        context.doSomething();
        context.findViewById(R.id.something);
    }
}
于 2012-04-17T01:42:43.267 に答える