Question

Can you fix these errors in Android Studio? What I am trying to do is that...

Can you fix these errors in Android Studio?

What I am trying to do is that developing an app to document the lifecycle of an “activity”.

-------------------------------

AppDocumentationActivity.java:

---------------------------------------

package com.example.activity_docmentation;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class AppDocumentationActivity extends AppCompatActivity {

    String message = getResources().getString(R.string.app_lifecycle_log); //Logcat tag
    String mActivityState; //refers to some state of activity
    int instanceTimes; //number of times instance is called
    String ACTIVITY_STATE_KEY;

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

        //recover instance state
        if(savedInstanceState!=null)
        {
            mActivityState = savedInstanceState.getString(ACTIVITY_STATE_KEY);
        }
        else
        {
            instanceTimes=0;
            ACTIVITY_STATE_KEY="";
        }

        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        //logs event message
        String eventMessage=getResources().getString(R.string.on_create_str);
        Log.d(message,eventMessage);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);            
        Log.d(message,savedInstanceState.getString(ACTIVITY_STATE_KEY));
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        instanceTimes++;
        mActivityState=String.valueOf(instanceTimes);

        outState.putString(ACTIVITY_STATE_KEY,mActivityState);

        super.onSaveInstanceState(outState);
    }

    /**
     * Dispatch onStart() to all fragments.  Ensure any created loaders are
     * now started.
     */
    @Override
    protected void onStart() {
        super.onStart();
        //logs event message
        String eventMessage=getResources().getString(R.string.on_start_str);
        Log.d(message,eventMessage);
    }

    /**
     * Dispatch onResume() to fragments.  Note that for better inter-operation
     * with older versions of the platform, at the point of this call the
     * fragments attached to the activity are <em>not</em> resumed.  This means
     * that in some cases the previous state may still be saved, not allowing
     * fragment transactions that modify the state.  To correctly interact
     * with fragments in their proper state, you should instead override
     * {@link #onResumeFragments()}.
     */
    @Override
    protected void onResume() {
        super.onResume();
        //logs event message
        String eventMessage=getResources().getString(R.string.on_resume_str);
        Log.d(message,eventMessage);
    }

    /**
     * Dispatch onPause() to fragments.
     */
    @Override
    protected void onPause() {
        super.onPause();
        //logs event message
        String eventMessage=getResources().getString(R.string.on_pause_str);
        Log.d(message,eventMessage);
    }

    @Override
    protected void onStop() {
        super.onStop();
        //logs event message
        String eventMessage=getResources().getString(R.string.on_stop_str);
        Log.d(message,eventMessage);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //logs event message
        String eventMessage=getResources().getString(R.string.on_destroy_str);
        Log.d(message,eventMessage);
    }

    /*@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }*/
}

----------------------------------------

strings.xml:

---------------------------------------

<resources>
    <string name="app_name">Activity_docmentation</string>
    <string name="app_str">Activity_docmentation</string>
    <string name="app_lifecycle_log">Activity Lifecycle log:</string>
    <string name="on_create_str">onCreate() method</string>
    <string name="on_start_str">onStart() method</string>
    <string name="on_resume_str">onResume() method</string>
    <string name="on_pause_str">onPause() method</string>
    <string name="on_stop_str">onStop() method</string>
    <string name="on_destroy_str">onDestroy() method</string>
    <string name="action_settings">Settings</string>
</resources>

Homework Answers

Answer #1

package com.example.activity_documentation;


import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class AppDocumentationActivity extends AppCompatActivity {

    String message;
    String mActivityState; //refers to some state of activity
    int instanceTimes; //number of times instance is called
    String ACTIVITY_STATE_KEY;

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


        // get the message inside the onCreate
        message = getResources().getString(R.string.app_lifecycle_log); //Logcat tag


        //recover instance state
        if(savedInstanceState!=null)
        {
            mActivityState = savedInstanceState.getString(ACTIVITY_STATE_KEY);
        }
        else
        {
            instanceTimes=0;
            ACTIVITY_STATE_KEY="";
        }

        setContentView(R.layout.activity_main);

       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
       setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                       .setAction("Action", null).show();
            }
        });

        //logs event message
        String eventMessage=getResources().getString(R.string.on_create_str);
        Log.d(message,eventMessage);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.d(message,savedInstanceState.getString(ACTIVITY_STATE_KEY));
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        instanceTimes++;
        mActivityState=String.valueOf(instanceTimes);

        outState.putString(ACTIVITY_STATE_KEY,mActivityState);

        super.onSaveInstanceState(outState);
    }

    /**
     * Dispatch onStart() to all fragments. Ensure any created loaders are
     * now started.
     */
    @Override
    protected void onStart() {
        super.onStart();
        //logs event message
        String eventMessage=getResources().getString(R.string.on_start_str);
        Log.d(message,eventMessage);
    }

    /**
     * Dispatch onResume() to fragments. Note that for better inter-operation
     * with older versions of the platform, at the point of this call the
     * fragments attached to the activity are <em>not</em> resumed. This means
   * that in some cases the previous state may still be saved, not allowing
     * fragment transactions that modify the state. To correctly interact
     * with fragments in their proper state, you should instead override
     * {@link #onResumeFragments()}.
     */
    @Override
    protected void onResume() {
        super.onResume();
        //logs event message
        String eventMessage=getResources().getString(R.string.on_resume_str);
        Log.d(message,eventMessage);
    }

    /**
     * Dispatch onPause() to fragments.
     */
    @Override
    protected void onPause() {
        super.onPause();
        //logs event message
        String eventMessage=getResources().getString(R.string.on_pause_str);
        Log.d(message,eventMessage);
    }

    @Override
    protected void onStop() {
        super.onStop();
        //logs event message
        String eventMessage=getResources().getString(R.string.on_stop_str);
        Log.d(message,eventMessage);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //logs event message
        String eventMessage=getResources().getString(R.string.on_destroy_str);
        Log.d(message,eventMessage);
    }

       /*@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }*/
}

----------------------------------------

strings.xml:

---------------------------------------

<resources>
    <string name="app_name">Activity_docmentation</string>
    <string name="app_str">Activity_docmentation</string>
    <string name="app_lifecycle_log">Activity Lifecycle log:</string>
    <string name="on_create_str">onCreate() method</string>
    <string name="on_start_str">onStart() method</string>
    <string name="on_resume_str">onResume() method</string>
    <string name="on_pause_str">onPause() method</string>
    <string name="on_stop_str">onStop() method</string>
    <string name="on_destroy_str">onDestroy() method</string>
    <string name="action_settings">Settings</string>
</resources>

Logcat statements:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Hi, I am trying to create an XML JTree viewer using the DOM parser instead of...
Hi, I am trying to create an XML JTree viewer using the DOM parser instead of the SAX parser, I am having trouble changing my code in order to utilize the DOM parser to extract the XML data into the tree structure. Would you be able to explain what changes should be made to the code in order to use the DOM parser instead of the SAX parser? // Java Packages //      import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import...
I am trying to make a program in C# and was wondering how it could be...
I am trying to make a program in C# and was wondering how it could be done based on the given instructions. Here is the code that i have so far... namespace Conversions { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if...
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT