2

I am not able to set the text from Homefragment.java

Currently I have below xml files:

activity_main.xml

 <LinearLayout
     android:id="@+id/tab_bar"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true" >

     <include
        android:id="@+id/ftr"
        layout="@layout/tab" />
</LinearLayout>

 <LinearLayout
                android:id="@+id/main_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >
            </LinearLayout>

Now tab.xml

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/txt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>


    </TableRow>

Now class files:

activity_main.java

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

        initUI();

    }

    private void initUI() {

     fragment = new HomeFragment();
}

HomeFragment.java

public class HomeFragment extends Fragment {


    private TextView txt;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container,
                false);

        context = getActivity();
        initUI(rootView);

        return rootView;
    }

    private void initUI(View view) {




    txt = (TextView)view.findViewbyid(R.id.txt1);

         txt.setText("hello. test");
}

fragment_home.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>

....
</RelativeLayout

Error log:

Caused by: java.lang.NullPointerException
            at com.xx.ew.fragment.HomeFragment.initUI(HomeFragment.java:112)
            at com.xx.ew.fragment.HomeFragment.onCreateView(HomeFragment.java:99)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
            at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174)
            at android.app.Activity.performStart(Activity.java:5356)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2340)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5333)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
            at dalvik.system.NativeStart.main(Native Method)

Now the issue is When I am trying to set text of Textview of tab.xml in homefragment class then it is not set the text and force close the app.

How can set the text of textview(txt1) of tab.xml from homefragment.java ?

  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – akash Aug 29 '15 at 04:15
  • No. its not same question. –  Aug 29 '15 at 04:20

3 Answers3

0

Changed my answer

Call your fragment as follows in initUI() method of activity:-

private void initUI() {
  fragment = new HomeFragment();
  FragmentManager fragmentManager = getSupportFragmentManager();
  FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();
  fragmentTransaction.replace(R.id.tab_bar, fragment);
  fragmentTransaction.commit();
 }

In Fragment change only one line which is in createView method

View rootView = inflater.inflate(R.layout.tab, container,
                false);
Pankaj
  • 7,908
  • 6
  • 42
  • 65
  • Caused by: java.lang.NullPointerException at com. com.xx.ew.fragment.HomeFragment.initUI(HomeFragment.java:107) at com.xx.ew.fragment.HomeFragment.onCreateView(HomeFragment.java:99) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)` –  Aug 29 '15 at 04:25
  • i have already have ` fragmentTransaction.replace(R.id.main_content, fragment);` which is LinearLayout in that xml. –  Aug 29 '15 at 04:48
  • And Have you changed the line in your **HomeFragemnt** which i have mentioned in my answer – Pankaj Aug 29 '15 at 04:54
  • if i change the rootView then what happens to old layout xml which is currently defined in my code. –  Aug 29 '15 at 05:09
  • Inflating rootview in fragment only replace rootview layout the other views wont get affected by it. – Pankaj Aug 29 '15 at 05:11
  • ok so i have to change the `initUI()` of activity_main.java or have to change in Homefragment.java file? –  Aug 29 '15 at 05:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88245/discussion-between-deepak-and-clairvoyant). –  Aug 29 '15 at 05:25
  • Its the only code you have posted here for `fragment` or are you doing something else also in **onCreateView()** method. – Pankaj Aug 29 '15 at 05:27
  • can we discuss this in chat ? if possible then. please. –  Aug 29 '15 at 05:28
0

MainActivity.java

public class MainActivity extends FragmentActivity {

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

    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
    if (fragment == null) {
        fragment = new HomeFragment();
        fm.beginTransaction().add(R.id.fragmentContainer, fragment)
                .commit();
    }
}
}

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >

</FrameLayout>

HomeFragment.java

public class HomeFragment extends Fragment {
private Context context;

private TextView txt;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_home, container,
            false);

    context = getActivity();
    initUI(rootView);

    return rootView;
}

private void initUI(View view) {
    txt = (TextView) view.findViewById(R.id.txt1);

    txt.setText("hello. test");
}
}

fragment_home.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <include
    android:id="@+id/ftr"
    layout="@layout/tab" />


</RelativeLayout>
Even2015
  • 39
  • 6
-1

Try: Make rootView a global variable. And then override the onStart method and call initUI from there.

olfek
  • 3,210
  • 4
  • 33
  • 49