0

I get

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.addView(android.view.View)' on a null object reference

Here is my code:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    b1=(Button)findViewById(R.id.btn);
    f1=(FrameLayout)findViewById(R.id.frame_layout);
    f1.addView(new myview(getApplicationContext()));
    animation= 
AnimationUtils.loadAnimation(getApplicationContext(),R.anim.translation);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            f1.startAnimation(animation);
        }
    });

The error is shown in line f1.addView(new myview(getApplicationContext()));. I have created an animation program in which i want to call other class into the framelayout that is mention in the code so when i run this then runtime error is showing which is shown below.

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2706)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1514)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:163)
    at android.app.ActivityThread.main(ActivityThread.java:6221)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.addView(android.view.View)' on a null object reference
    at com.cenet.animationclass.MainActivity.onCreate(MainActivity.java:25)
    at android.app.Activity.performCreate(Activity.java:6875)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659
Zabuzard
  • 25,064
  • 8
  • 58
  • 82

3 Answers3

1

you need to add a layout to your activity first:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1=(Button)findViewById(R.id.btn);
    f1=(FrameLayout)findViewById(R.id.frame_layout);
}
Teresa
  • 373
  • 2
  • 8
0

You did not inflated your layout and you are finding views inside it. This was wrong in your code.

When you wrote f1=(FrameLayout)findViewById(R.id.frame_layout); then it returned null so f1 was null. Now when you tried to use f1.addView then you tried to call addView method of null object so you got NullPointerException.

Solution is to create activity layout first by adding setContentView(R.layout.yourLayout); after super in onCreate like

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1=(Button)findViewById(R.id.btn);
    f1=(FrameLayout)findViewById(R.id.frame_layout);
}

Caution check frame_layout is present in your xml layout

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
0

I have forgot to add setContent(R.layout.(xmlfile)). Thats why the run time error shows.Hence when ever you inflate your file you must add setContent(R.layout.(xmlfile)).