0

私はすべてを試しましたが、これは どこでもアプリケーションコンテキストを使用していますか? さらにいくつかのことがあり、何も機能しません。

Exception を拡張する DatabaseException クラスにアプリケーション コンテキストが必要です。sharedpreferences例外が発生したときにいくつかチェックしたいので必要です。コードは基本的にこれです:

public class DatabaseException extends Exception {

private static final long serialVersionUID = 1L;

DatabaseException(){
    super();
}

DatabaseException(String s){
    super(s);
}

public DatabaseException (String s, Throwable t){
    super (s,t);
    kindOfException(t.getCause(), s);
}

DatabaseException (Throwable t){
    super (t);
    kindOfException(t.getCause(), null);
}

...

private void createLog() {
    Log.d("Database Exception","On Create Log");
    String listLogs;
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(DatabaseException.class.context);

もちろんこれはうまくいきません。それで...お願い、誰か私を助けてくれませんか?

4

3 に答える 3

1
public class DatabaseException extends Exception {

private static final long serialVersionUID = 1L;

public YourContext context;
public DatabaseException(YourContext context){
    this.context = context;
}
}

例外をキャッチした場合:

try {
    ...
} (catch DatabaseException e) {
    MyContext context = e.context;
}
于 2012-10-23T13:38:53.717 に答える
0

簡単な回答:
1. DatabaseException コンストラクターのパラメーターとしてアプリケーション コンテキストを追加します。
2. DatabaseException を他のクラスの内部クラスとして配置します。(他のクラスは Activity を意味します...)、したがって、内部クラスで外部クラスのフィールド/メソッドを使用できます。
3. ...

于 2012-10-23T13:40:24.997 に答える
0

それは単に..できません。しかし、Execption上げられたときに実行できます

private void createLog() {
    this.getContext();
    Log.d("Database Exception","On Create Log");
    String listLogs;
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(DatabaseException..
}

catch句で必要な操作。

パラメータとして渡すこともできますが、避けてください。

于 2012-10-23T13:33:57.317 に答える