無法從fragment類中設置Textview上的文本

[英]Not able to set text on Textview from fragment class


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

我不能設置來自Homefragment.java的文本

Currently I have below xml files:

目前我有以下xml文件:

activity_main.xml

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

現在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

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

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

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.

現在的問題是當我試圖設置Textview的時候。在homefragment類中,它沒有設置文本並強制關閉應用程序。

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

如何設置選項卡的textview(txt1)的文本。從homefragment xml。java ?

3 个解决方案

#1


0  

Changed my answer

改變了我的答案

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

調用您的片段如下在initUI()方法的活動:-

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

在片段中,只更改createView方法中的一行

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

#2


0  

MainActivity.java

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

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

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

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>

#3


-1  

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

嘗試:使rootView成為一個全局變量。然后重寫onStart方法並從那里調用initUI。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/08/29/72a30f70364983a0c5229746270ad51e.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com