0

I want to know why my application is throwing a runtime exception when I run it? I used to the code in the following link for my application, dynamically add and remove view to viewpager.

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActionBar ab=getActionBar();

    pagerAdapter = new MainPagerAdapter();
    pager = (ViewPager) findViewById (R.id.pager);

    pager.setAdapter (pagerAdapter);


    inflater = (LayoutInflater)getSystemService
              (Context.LAYOUT_INFLATER_SERVICE);
    v0 = (FrameLayout) inflater.inflate (R.layout.listlayout, null);
    Button button=(Button)findViewById(R.id.button1);
    button.setOnClickListener(this);

    pagerAdapter.addView (v0, 0);

    pagerAdapter.notifyDataSetChanged();

}
@Override
public void onClick(View v) {
    if(v.getId()==R.id.button1)
    {
        pagerAdapter.addView (v0);

        pagerAdapter.notifyDataSetChanged();
    }

}

LogCat:

  FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.todolistworkable/com.example.todolistworkable.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
at android.app.ActivityThread.access$600(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)Caused by: java.lang.NullPointerException
at com.example.todolistworkable.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
Community
  • 1
  • 1
afnan1992
  • 57
  • 10
  • Can you post more of your code? I can't see the onClick method... – Phantômaxx Mar 11 '14 at 15:34
  • Dunno which line #43 is, but getActionBar() can return null if the Activity has no Action Bar. – Turnsole Mar 11 '14 at 15:34
  • I doubt this is related to adding onClickListener. Check your code to make sure your views and your adapter are not null. I'd even check that LayoutInflater. – Rarw Mar 11 '14 at 15:38
  • How can i check whether views or adapter are null? If i remove the setonclicklistener line it works fine? – afnan1992 Mar 11 '14 at 16:11
  • @afnan1992 comment the 2 lines inside onClick and run it should be alright. see if exception is thrown – Raghunandan Mar 11 '14 at 16:19

1 Answers1

0

the object on line 43 of your code within your onCreate method is null. Most likely the target of one of your findViewById calls is not returning what you expect it to.

See this for more info. How to interpret Logcat

Community
  • 1
  • 1
Kuffs
  • 35,581
  • 10
  • 79
  • 92