2

我目前正在做一个关于 eclipse android 开发的教程系列,并试图复制代码以帮助记住所有内容的功能,但我收到 onSaveInstanceState(Bundle) 的错误,它说“onSaveInstanceState(Bundle) 方法未定义类型对象”。我已经检查了很多次,一切都是准确的,但没有发现任何问题。

protected void onSaveInstanceState(Bundle outState)
     {
        super.onSaveInstanceState(outState);

        outState.putDouble(TOTAL_BILL, finalBill);
        outState.putDouble(CURRENT_TIP, tipAmount);
        outState.putDouble(BILL_WITHOUT_TIP, finalBill);
     }
4

2 回答 2

1

你会想要使用@Override并确保你的类 extends Activity。只要你有这两个东西,你应该是好的。

class MyActivity extends Activity {

    @Override // May be public depending on the class you are extending
    protected void onSaveInstanceState(Bundle outState) {

        outState.putDouble(TOTAL_BILL, finalBill);
        outState.putDouble(CURRENT_TIP, tipAmount);
        outState.putDouble(BILL_WITHOUT_TIP, finalBill);

        // Wait till after you've added your items to pass the bundle
        super.onSaveInstanceState(outState);
     }
}

根据Activity文档页面,其他扩展的类ActivityAccountAuthenticatorActivityActivityGroupAliasActivityExpandableListActivityFragmentActivityListActivityNativeActivityActionBarActivityLauncherActivityPreferenceActivityTabActivity

如果您想弄清楚某些东西是如何工作的,那么 Android 源代码也是一个很好的查看位置。很多时候,这些注释对事物的实现方式以及它们所依赖的内容进行了更多描述。

参考:核心/java/android/app/Activity.java

    /**
     * Called to retrieve per-instance state from an activity before being killed
     * so that the state can be restored in {@link #onCreate} or
     * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
     * will be passed to both).
     *
     * <p>This method is called before an activity may be killed so that when it
     * comes back some time in the future it can restore its state.  For example,
     * if activity B is launched in front of activity A, and at some point activity
     * A is killed to reclaim resources, activity A will have a chance to save the
     * current state of its user interface via this method so that when the user
     * returns to activity A, the state of the user interface can be restored
     * via {@link #onCreate} or {@link #onRestoreInstanceState}.
     *
     * <p>Do not confuse this method with activity lifecycle callbacks such as
     * {@link #onPause}, which is always called when an activity is being placed
     * in the background or on its way to destruction, or {@link #onStop} which
     * is called before destruction.  One example of when {@link #onPause} and
     * {@link #onStop} is called and not this method is when a user navigates back
     * from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
     * on B because that particular instance will never be restored, so the
     * system avoids calling it.  An example when {@link #onPause} is called and
     * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
     * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
     * killed during the lifetime of B since the state of the user interface of
     * A will stay intact.
     *
     * <p>The default implementation takes care of most of the UI per-instance
     * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
     * view in the hierarchy that has an id, and by saving the id of the currently
     * focused view (all of which is restored by the default implementation of
     * {@link #onRestoreInstanceState}).  If you override this method to save additional
     * information not captured by each individual view, you will likely want to
     * call through to the default implementation, otherwise be prepared to save
     * all of the state of each view yourself.
     *
     * <p>If called, this method will occur before {@link #onStop}.  There are
     * no guarantees about whether it will occur before or after {@link #onPause}.
     * 
     * @param outState Bundle in which to place your saved state.
     * 
     * @see #onCreate
     * @see #onRestoreInstanceState
     * @see #onPause
     */
    protected void onSaveInstanceState(Bundle outState) {
        outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
        Parcelable p = mFragments.saveAllState();
        if (p != null) {
            outState.putParcelable(FRAGMENTS_TAG, p);
        }
        getApplication().dispatchActivitySaveInstanceState(this, outState);
    }
于 2013-10-13T08:08:57.747 回答
1

onSaveInstanceState(Bundle outState)方法放在方法之外的正确位置textWatcher
只需放在分号之后,它标志着textWatcher方法的结束。 [};]

protected void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    outState.putDouble(Total_Bill, finalBill);
    outState.putDouble(Tip_Amount, tipamount);
    outState.putDouble(Bill_Without_Tip, Billbeforetip);
}

错误应该消失。

于 2014-08-20T23:24:07.383 回答