的onCreate计数为关闭,但code看起来不错 [英] onCreate count was off but code looks fine

查看:146
本文介绍了的onCreate计数为关闭,但code看起来不错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请纠正我,如果我错了,但我已经做了所有的下方,我仍然得到一个错误,如果我运行test2.java,我得到的onCreate计数的JUnit测试误差。

Please correct me if Im wrong but i have done all the below and i still get an Error if i run test2.java, I get the onCreate count was off error on the jUnit test.

请condider以下:

Please condider the below:

public class ActivityOne extends Activity {

    private static final String RESTART_KEY = "restart";
    private static final String RESUME_KEY = "resume";
    private static final String START_KEY = "start";
    private static final String CREATE_KEY = "create";

    // String for LogCat documentation
    private final static String TAG = "Lab-ActivityOne";

    // Lifecycle counters

    // TODO:
    // Create counter variables for onCreate(), onRestart(), onStart() and
    // onResume(), called mCreate, etc.
    // You will need to increment these variables' values when their
    // corresponding lifecycle methods get called

private int mCreate = 0;
private int mRestart = 0;
private int mResume  = 0;
private int mStart  = 0;
    // TODO: Create variables for each of the TextViews, called
        // mTvCreate, etc. 

private  TextView mTvCreate;
private  TextView mTvRestart;
private  TextView mTvResume;
private  TextView mTvStart;

    Bundle instancestate;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);

        instancestate = savedInstanceState;
        // TODO: Assign the appropriate TextViews to the TextView variables
        // Hint: Access the TextView by calling Activity's findViewById()
        // textView1 = (TextView) findViewById(R.id.textView1);

        mTvCreate = (TextView) findViewById(R.id.create);
        mTvRestart = (TextView) findViewById(R.id.restart);
        mTvResume = (TextView) findViewById(R.id.resume);
        mTvStart = (TextView) findViewById(R.id.start);

        Button launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo); 
        launchActivityTwoButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO:
                // Launch Activity Two
                // Hint: use Context's startActivity() method

                // Create an intent stating which Activity you would like to start
                    Intent activityTwo = new Intent(ActivityOne.this,ActivityTwo.class);
                    startActivity(activityTwo);
                // Launch the Activity using the intent 
            }
        });

        // Check for previously saved state
        if (savedInstanceState != null) {
            // TODO:
            // Restore value of counters from saved state
               mCreate = savedInstanceState.getInt(CREATE_KEY);
               mRestart = savedInstanceState.getInt(RESTART_KEY);
               mStart = savedInstanceState.getInt(START_KEY);
               mResume = savedInstanceState.getInt(RESUME_KEY);
            // Only need 4 lines of code, one for every count variable
        }

        // TODO: Emit LogCat message
        Log.i(TAG, "Entered onCreate() method" +mCreate);
        // TODO:
        // Update the appropriate count variable
        ++mCreate;
        // Update the user interface via the displayCounts() method
        displayCounts();
    }

    // Lifecycle callback overrides

    @Override
    public void onStart() {
        super.onStart();
        // TODO: Emit LogCat message
        Log.i(TAG, "Entered onStart() method" +mStart);
        // TODO:
        // Update the appropriate count variable
     ++mStart;
        // Update the user interface
        displayCounts();

    }

    @Override
    public void onResume() {
        super.onResume();
        // TODO: Emit LogCat message
        Log.i(RESUME_KEY, "Entered onResume() method" +mResume);
        // TODO:
        // Update the appropriate count variable
        ++mResume;
        // Update the user interface
        displayCounts();

    }

    @Override
    public void onPause() {
        super.onPause();
        //onSaveInstanceState(instancestate);
        // TODO: Emit LogCat message
        Log.i(TAG, "Entered onPause() method" +mStart);
    }

    @Override
    public void onStop() {
        super.onStop();
        // TODO: Emit LogCat message
        Log.i(TAG, "Entered onStop() method" +mStart);
    }

    @Override
    public void onRestart() {
        super.onRestart();

        // TODO: Emit LogCat message
        Log.i(TAG, "Entered onRestart() method" +mResume);
        // TODO:
        // Update the appropriate count variable
        ++mRestart;
        // Update the user interface
        displayCounts();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // TODO: Emit LogCat message
        Log.i(TAG, "Entered onDestroy() method");
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        // TODO:
        // Save state information with a collection of key-value pairs
        // 4 lines of code, one for every count variable
            savedInstanceState.putInt(CREATE_KEY, mCreate);
            savedInstanceState.putInt( RESTART_KEY, mRestart);
            savedInstanceState.putInt(START_KEY, mStart);
            savedInstanceState.putInt(RESUME_KEY, mResume);

            super.onSaveInstanceState(savedInstanceState);
    }

    // Updates the displayed counters
    public void displayCounts() {
        mTvCreate.setText("onCreate() calls: " + mCreate);
        mTvStart.setText("onStart() calls: " + mStart);
        mTvResume.setText("onResume() calls: " + mResume);
        mTvRestart.setText("onRestart() calls: " + mRestart);

    }
}

这是堆栈跟踪:

junit.framework.AssertionFailedError: onCreate() count was off.
at course.labs.activitylab.test.Test2.testRun(Test2.java:55)

...

推荐答案

您的方法的onSaveInstanceState应该是这样的:

Your onSaveInstanceState method should be this:

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

    savedInstanceState.putInt(CREATE_KEY, mCreate);
    savedInstanceState.putInt(RESTART_KEY, mRestart);
    savedInstanceState.putInt(START_KEY, mStart);
    savedInstanceState.putInt(RESUME_KEY, mResume);    
 }

这篇关于的onCreate计数为关闭,但code看起来不错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆