Android studio Fragment TabHost -


i want display fragmenttabhost in activity_main when button clicked.when click it, shows "unfortunately fragtabhost has stopped".below code. please me!!

mainactivity

public class mainactivity extends activity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         button button=(button) findviewbyid(r.id.button);         button.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 testfragment fragment=new testfragment();                 fragmentmanager fragmentmanager=fragment.getfragmentmanager();                 fragmenttransaction transaction=fragmentmanager.begintransaction();                 transaction.replace(r.id.relative,fragment);                 transaction.commit();               }         });      }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     } } 

activity_main

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"     android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     android:paddingbottom="@dimen/activity_vertical_margin"     android:id="@+id/relative"     tools:context=".mainactivity">       <button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/button"         android:text="tabhost"/>  </relativelayout> 

test_fragment

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical" >      <framelayout         android:id="@android:id/tabcontent"         android:layout_width="0dp"         android:layout_height="0dp"         android:layout_weight="0" />      <framelayout         android:id="@+id/realtabcontent"         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="1"/>      <tabwidget         android:id="@android:id/tabs"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_weight="0"         android:orientation="horizontal" />  </linearlayout> 

testfragment

package com.example.sabudaniel61.fragtabhost;  /**  * created sabudaniel61 on 2015-09-30.  */  import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmenttabhost; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview;  public class testfragment extends fragment {     @override     public view oncreateview(layoutinflater inflater,                              viewgroup container, bundle savedinstancestate) {          fragmenttabhost tabhost = new fragmenttabhost(getactivity());         inflater.inflate(r.layout.test_fragment, tabhost);         tabhost.setup(getactivity(),                 getchildfragmentmanager(), r.id.realtabcontent);          tabhost.addtab(tabhost.newtabspec("simple")                 .setindicator("simple"), dummysectionfragment.class, null);         tabhost.addtab(tabhost.newtabspec("contacts")                 .setindicator("contacts"), dummysectionfragment.class, null);         return tabhost;     }      /**      * dummy fragment representing section of app,      * displays dummy text.      */     public static class dummysectionfragment extends fragment {         private static int count = 0;         @override         public view oncreateview(layoutinflater inflater, viewgroup container,                                  bundle savedinstancestate) {             view rootview = inflater.inflate(r.layout.fragment_section_dummy,                     container, false);             ((textview) rootview.findviewbyid(android.r.id.text1))                     .settext("dummy section " + count++);             return rootview;         }     } } 

fragment_section_dummy

 <textview          xmlns:android="http://schemas.android.com/apk/res/android"         android:id="@android:id/text1"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:gravity="center"         android:textsize="24sp"         android:padding="32dp"         /> 

logcat:

09-30 00:43:18.676  28335-28335/com.example.sabudaniel61.fragtabhost e/androidruntime﹕ fatal exception: main     process: com.example.sabudaniel61.fragtabhost, pid: 28335     java.lang.nullpointerexception: attempt invoke virtual method 'android.support.v4.app.fragmenttransaction android.support.v4.app.fragmentmanager.begintransaction()' on null object reference             @ com.example.sabudaniel61.fragtabhost.mainactivity$1.onclick(mainactivity.java:24)             @ android.view.view.performclick(view.java:4756)             @ android.view.view$performclick.run(view.java:19749)             @ android.os.handler.handlecallback(handler.java:739)             @ android.os.handler.dispatchmessage(handler.java:95)             @ android.os.looper.loop(looper.java:135)             @ android.app.activitythread.main(activitythread.java:5221)             @ java.lang.reflect.method.invoke(native method)             @ java.lang.reflect.method.invoke(method.java:372)             @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899)             @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694) 09-30 00:48:19.063  28335-28335/com.example.sabudaniel61.fragtabhost i/process﹕ sending signal. pid: 28335 sig: 9 

you getting nullpointerexception @ line# 24 in mainactivity.

replace

fragmentmanager fragmentmanager=fragment.getfragmentmanager(); 

with

fragmentmanager fragmentmanager=this.getsupportfragmentmanager(); 

in mainactivity.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -