2

I searched everywhere for a solution to my problem, but couldn't get one.So, the problem is I want to launch another activity called FifthActivity from my MainActivity via the button but my activity keeps crashing. I checked the logcat and found this -

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.india.chemistry/com.example.india.chemistry.FifthActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference

Below is my mainActivity java file.

package com.example.india.chemistry;

public class MainActivity extends AppCompatActivity {
    private static boolean isRunning = false;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(!isRunning)
        {
            isRunning = true;
            startActivity(new Intent(this, Intro.class));//Play your video here
        }
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
 }

    @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) {
        int id = item.getItemId();
       if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void part_a( View View){
            Intent intent1 = new Intent("com.example.india.chemistry.Second_Activity");
            startActivity(intent1);
    }

    public void part_b( View View) {
        Intent intent2 = new Intent("com.example.india.chemistry.ThirdActivity");
        startActivity(intent2);
    }

    public void formulas( View View) {
        Intent intent4 = new Intent(this,FifthActivity.class);
        startActivity(intent4);
    }    
}

Below is my FifthActivity.java file.

package com.example.india.chemistry;


public class FifthActivity extends ActionBarActivity   {
    final Animation shake = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.shake);
    final Animation bounce = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.bounce);

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

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_fifth, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();    

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

        return super.onOptionsItemSelected(item);
    }


        public void nernst(View View) {
            Intent intentf1 = new Intent("com.example.india.chemistry.nernst_activity");
            startActivity(intentf1);
        }

        public void waterhardness(View View) {
            Intent intentf2 = new Intent("com.example.india.chemistry.WaterHardness");
            startActivity(intentf2);

        }

        public void determinationOfCao(View view) {
            Intent intentf3 = new Intent("com.example.india.chemistry.CementCao");
            startActivity(intentf3);
        }

        public void COD_waste_water(View view) {
            Intent intentf4 = new Intent("com.example.india.chemistry.COD");
            startActivity(intentf4);
        }

        public void C_in_B(View view) {
            Intent intentf5 = new Intent("com.example.india.chemistry.Copper_in_Brass");
            startActivity(intentf5);
        }

        public void alkalinity(View view) {
            Intent intentf6 = new Intent("com.example.india.chemistry.Alkalinity");
            startActivity(intentf6);
        }

        public void haematite_ore(View view) {
            Intent intentf7 = new Intent("com.example.india.chemistry.HaemetiteOre");
            startActivity(intentf7);
        }

        public void colourimetry_copper(View view) {
            Intent intentf8 = new Intent("com.example.india.chemistry.Colourimetric_Copper");
            startActivity(intentf8);
        }

        public void potentiometry_fas(View view) {
            Intent intentf9 = new Intent("com.example.india.chemistry.Potentiometry_fas");
            startActivity(intentf9);
        }

        public void conductometry(View view) {
            Intent intentf10 = new Intent("com.example.india.chemistry.Conductometry");
            startActivity(intentf10);
        }

        public void pka(View view) {
            Intent intentf11 = new Intent("com.example.india.chemistry.pka");
            startActivity(intentf11);
        }



}

Below is the logcat.

10-14 17:13:58.145 15430-15430/com.example.india.chemistry E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.india.chemistry, PID: 15430 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.india.chemistry/com.example.india.chemistry.FifthActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2420)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107)
at com.example.india.chemistry.FifthActivity.<init>(FifthActivity.java:21)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1085)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2410)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569) 
at android.app.ActivityThread.access$900(ActivityThread.java:150) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:168) 
at android.app.ActivityThread.main(ActivityThread.java:5885) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 

Please help me.

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
Chinmaya KN
  • 29
  • 1
  • 9
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – yennsarah Oct 14 '16 at 11:47

6 Answers6

1

Try this and let me know if problem solved or not.....

Animation shake,bounce;

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

    shake = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.shake);
    bounce = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.bounce);

}
Maveňツ
  • 1
  • 12
  • 50
  • 89
Mureed Hussain
  • 2,745
  • 2
  • 18
  • 22
  • yes. Because you can use getApplicationContext() only after activity created. you can not use activity context before activity onCreate – Mureed Hussain Oct 14 '16 at 12:04
1

You can't call getApplicationContext(result in null) before oncreate because activity has not been created properly yet so you need to call this once your oncreate has been executed or after the super call .

The super call of onCreate fetches the application context for your activity.

Solution :

So either put your code inside oncreate or create a function and call it from inside oncreate after the super call.

Note : you will have to avoid final to make your identifiers global because the lazy initialization with final only works with constructors but don't worry , onCreate gets executed once when your activity is created or recreated.

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
1

You need to add the activity in your manifest file, under application. Whenever you create a new activity add it to manifest file.

<application
...
<activity
            android:name=". FifthActivity"
            android:label="Fifth" />
...
</application>

Hope this helps.

Adheesh
  • 83
  • 1
  • 11
0

try this:

public class FifthActivity extends ActionBarActivity   {
    Animation shake,bounce;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fifth);
shake = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.shake);
    bounce = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.bounce);
    }
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
0

Your view has not yet been created, move :

shake = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.shake);
bounce = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.bounce);

To your oncreate method.

Megadec
  • 466
  • 10
  • 25
0

stack trace's first line gives Detailed information : getApplicationContext()' on a null object reference.Means Activity was not created,it's return null Context. You make a both animation's Object Globally. try to edit your code this way.

public class FifthActivity extends ActionBarActivity   {

Animation shake,bounce; 

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

    shake = AnimationUtils.loadAnimation(getApplicationContext(),
        R.anim.shake);
    bounce = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.bounce);
} 

}

Jatin Mandanka
  • 411
  • 11
  • 16