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>
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:
Get Answers For Free
Most questions answered within 1 hours.