I get
java.lang.NullPointerException
: Attempt to invoke virtual method 'void android.view.ViewGroup.addView(android.view.View)
' on anull
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