0

設定アクティビティの Dialogpreference クラスを拡張するカスタム設定クラスを定義しました。

public class YesNoPreference extends DialogPreference {

private boolean mWasPositiveResult;
    DashboardActivity dashboardActivity;
Context prefContext;

public YesNoPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    userSession = new UserSessions(context);
    prefContext = context;
}

@Override
protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);

    if (callChangeListener(positiveResult)) {
        setValue(positiveResult);

        /* Unset all user shared preferences */
        userSession.unsetSessionData();

    try {   
    dashboardActivity = new DashboardActivity();
    //dashboardActivity.loginScreen();
    Intent dashboard = new Intent(prefContext, DashboardActivity.class);
    dashboardActivity.startActivity(dashboard);

     } catch (Exception e) {
     e.printStackTrace();
     }
    }
}

/**
 * Sets the value of this preference, and saves it to the persistent store
 * if required.
 * 
 * @param value The value of the preference.
 */
public void setValue(boolean value) {
    mWasPositiveResult = value;

    persistBoolean(value);

    notifyDependencyChange(!value);
}

ダイアログ設定を使用して、アプリケーションからユーザーをログアウトしています。したがって、ユーザーが [OK] を選択すると、共有設定が解除され、ユーザーはログイン ページに誘導されます。

Activity クラスで関数を作成してから、このクラスで呼び出してみました。Intent クラスも使用しましたが、実行は次の場所で停止します

dashboardActivity.startActivity(dashboard);

Null ポインター例外を生成します。

解決策を見つけるために、私を助けてください。

public class SettingsActivity extends Activity {

@Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Display the fragment as the main content.
    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new SettingsFragment())
            .commit();
   }

}

public class SettingsFragment extends PreferenceFragment {

@Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       // Load the preferences from an XML resource
       addPreferencesFromResource(R.xml.pref_settings);
   }
}
4

1 に答える 1

1

使用する

prefContext.startActivity(dashboard);

それ以外の

dashboardActivity.startActivity(dashboard);

メソッドにアクセスしますstartActivityDashboardActivity現在、メソッドにアクセスするための Activity のインスタンスを作成しようとしていますstartActivityprefContextアクティビティの開始に使用

于 2013-03-28T12:33:44.370 に答える