我是初学者,我只是想学习android方法的生命周期。这是我用过的简单代码。但这不起作用并给出致命的例外。
此外,在我的手机上删除此应用程序后,它会出现错误Unfortunately Stopped activity
。请帮忙!
MainActivity.java
package com.example.activity101;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
public class MainActivity extends Activity {
String tag = "Lifecycle";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag, "In the onCreate() event");
}
public void onStart()
{
super.onStart();
Log.d(tag, "In the onStart() event");
}
public void onRestart()
{
super.onRestart();
Log.d(tag,"In the OnResart() Event");
}
public void onResume()
{
super.onResume();
Log.d(tag, "In the onResume() event");
}
public void onPause()
{
super.onPause();
Log.d(tag, "In the onPause() event");
}
public void onStop() {
}
{
super.onStop();
Log.d(tag, "In the onStop() event");
}
public void onDestroy()
{
super.onDestroy();
Log.d(tag, "In the onDestroy() event");
}
}
我的控制台错误:
07-09 11:48:17.512: I/Process(10035): Sending signal. PID: 10035 SIG: 9
07-09 11:49:07.257: D/AndroidRuntime(10595): Shutting down VM
07-09 11:49:07.257: W/dalvikvm(10595): threadid=1: thread exiting with uncaught exception (group=0x40d8e2a0)
07-09 11:49:07.262: E/AndroidRuntime(10595): FATAL EXCEPTION: main
07-09 11:49:07.262: E/AndroidRuntime(10595): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.activity101/com.example.activity101.MainActivity}: java.lang.NullPointerException
07-09 11:49:07.262: E/AndroidRuntime(10595): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
07-09 11:49:07.262: E/AndroidRuntime(10595): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
07-09 11:49:07.262: E/AndroidRuntime(10595): at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-09 11:49:07.262: E/AndroidRuntime(10595): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
07-09 11:49:07.262: E/AndroidRuntime(10595): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 11:49:07.262: E/AndroidRuntime(10595): at android.os.Looper.loop(Looper.java:137)
07-09 11:49:07.262: E/AndroidRuntime(10595): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-09 11:49:07.262: E/AndroidRuntime(10595): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 11:49:07.262: E/AndroidRuntime(10595): at java.lang.reflect.Method.invoke(Method.java:511)
07-09 11:49:07.262: E/AndroidRuntime(10595): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
07-09 11:49:07.262: E/AndroidRuntime(10595): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
07-09 11:49:07.262: E/AndroidRuntime(10595): at dalvik.system.NativeStart.main(Native Method)
07-09 11:49:07.262: E/AndroidRuntime(10595): Caused by: java.lang.NullPointerException
07-09 11:49:07.262: E/AndroidRuntime(10595): at android.app.Activity.onStop(Activity.java:1403)
07-09 11:49:07.262: E/AndroidRuntime(10595): at com.example.activity101.MainActivity.<init>(MainActivity.java:45)
07-09 11:49:07.262: E/AndroidRuntime(10595): at java.lang.Class.newInstanceImpl(Native Method)
07-09 11:49:07.262: E/AndroidRuntime(10595): at java.lang.Class.newInstance(Class.java:1319)
07-09 11:49:07.262: E/AndroidRuntime(10595): at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
07-09 11:49:07.262: E/AndroidRuntime(10595): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
07-09 11:49:07.262: E/AndroidRuntime(10595): ... 11 more
07-09 11:49:44.497: I/Process(10595): Sending signal. PID: 10595 SIG: 9
答案 0 :(得分:3)
使用super.onStop();
方法拨打onStop()
,而不是外线:
public void onStop() {
super.onStop();
Log.d(tag, "In the onStop() event");
}
答案 1 :(得分:0)
请执行以下操作 -
1) 在所有方法之前添加@Override。
2) 在每种方法中调用super。
<强> MainActivity.java 强>
package com.example.activity101;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
public class MainActivity extends Activity {
String tag = "Lifecycle";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag, "In the onCreate() event");
}
@Override
public void onStart()
{
super.onStart();
Log.d(tag, "In the onStart() event");
}
@Override
public void onRestart()
{
super.onRestart();
Log.d(tag,"In the OnResart() Event");
}
@Override
public void onResume()
{
super.onResume();
Log.d(tag, "In the onResume() event");
}
@Override
public void onPause()
{
super.onPause();
Log.d(tag, "In the onPause() event");
}
@Override
public void onStop()
{
super.onStop();
Log.d(tag, "In the onStop() event");
}
@Override
public void onDestroy()
{
super.onDestroy();
Log.d(tag, "In the onDestroy() event");
}
}