Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
package com.example.dell.alc;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {
    public static final String TAG =  "Lifecycle_activity";
    public static int createCount = 0;
    public static int startCount = 0;
    public static int restartCount = 0;
    public static int resumeCount = 0;
    public static int pauseCount = 0;
    public static int stopCount = 0;
    public static int destroyCount = 0;
    TextView tv,tv1,tv2,tv3,tv4,tv5,tv6;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        createCount++;
        Log.d(TAG,"The number of times the Activity created: " + createCount);
        if(createCount>=1){
            TextView tv = (TextView) findViewById(R.id.onCreate);
            tv.setText("Start count:" + createCount);
        }
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        startCount++;
        Log.d(TAG,"The number of times the Activity started: " + startCount);
        if(startCount>=1){
            TextView tv1 = (TextView) findViewById(R.id.onStart);
            tv1.setText("Start count:" + startCount);
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        startCount++;
        Log.d(TAG,"The number of times the Activity Restarted: " + startCount);
        if(restartCount>1){
            TextView tv2 = (TextView) findViewById(R.id.onRestart);
            tv2.setText("Restart count:" + restartCount);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopCount++;
        Log.d(TAG,"The number of times the Activity stopped: " + stopCount);
        if(stopCount>=1){
            tv3 = (TextView) findViewById(R.id.onStop);
            tv3.setText("stop count:" + stopCount);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        resumeCount++;
        Log.d(TAG,"The number of times the Activity is resumed: " + resumeCount);
        if(resumeCount>=1){
            TextView tv4 = (TextView) findViewById(R.id.onResume);
            tv4.setText("Resume count:" + resumeCount);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        startCount++;
        Log.d(TAG,"The number of times the Activity paused: " + pauseCount);
        if(pauseCount>=0){
            TextView tv5 = (TextView) findViewById(R.id.onPause);
            tv5.setText("pause count:" + pauseCount);
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        destroyCount++;
        Log.d(TAG, "The number of times the Activity created: " + destroyCount);
        if (destroyCount >= 0) {
            TextView tv6 = (TextView) findViewById(R.id.onDestroy);
            tv6.setText("destroy count:" + destroyCount);
        }
    }

    @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);
    }
}
Posted
Updated 27-Feb-15 17:28pm
v2
Comments
Sergey Alexandrovich Kryukov 27-Feb-15 23:28pm    
I suspect that's "null pointer" or "null reference", not "null point". Is that right? In what line?
This is one of the simplest exceptions to locate and prevent.

By the way, you need to format the code samples properly, to keep them readable. I've done that for you, please click "Improve question" to see how it's done.

—SA
Member 11486438 27-Feb-15 23:34pm    
hey, hi its a null pointer error however I tried running the code you sent and I get even more errors. do you want me to send you my XML file as well?
Sergey Alexandrovich Kryukov 27-Feb-15 23:42pm    
No. Just give me a line. Comment this line where the exception is thrown and tell us how to locate this line.
—SA
Member 11486438 28-Feb-15 0:39am    
I get this error when i run my code:
02-28 05:37:49.581 1120-1120/com.example.dell.alc D/dalvikvm﹕ Late-enabling CheckJNI
02-28 05:37:49.749 1120-1120/com.example.dell.alc D/Lifecycle_activity﹕ The number of times the Activity created: 1
02-28 05:37:49.749 1120-1120/com.example.dell.alc D/AndroidRuntime﹕ Shutting down VM
02-28 05:37:49.749 1120-1120/com.example.dell.alc W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4bd3648)
02-28 05:37:49.749 1120-1120/com.example.dell.alc E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell.alc/com.example.dell.alc.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.dell.alc.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
Sergey Alexandrovich Kryukov 28-Feb-15 1:06am    
Are you going to tell the line or not?
—SA

1 solution

02-28 05:37:49.581 1120-1120/com.example.dell.alc D/dalvikvm﹕ Late-enabling CheckJNI
02-28 05:37:49.749 1120-1120/com.example.dell.alc D/Lifecycle_activity﹕ The number of times the Activity created: 1
02-28 05:37:49.749 1120-1120/com.example.dell.alc D/AndroidRuntime﹕ Shutting down VM
02-28 05:37:49.749 1120-1120/com.example.dell.alc W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4bd3648)
02-28 05:37:49.749 1120-1120/com.example.dell.alc E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell.alc/com.example.dell.alc.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.dell.alc.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900