Android从另一个片段设置Textview

Posted

技术标签:

【中文标题】Android从另一个片段设置Textview【英文标题】:Android set Textview from another fragment 【发布时间】:2016-01-27 14:30:14 【问题描述】:

我想从一个片段到另一个片段的 setText 到一个 Textview。

我该怎么做?现在我得到一个:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on an null object reference

FragmentRonde1.java

lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() 
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) 

            ListClickedFragment newFragment = new ListClickedFragment();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();

            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);

            transaction.commit();

            Cursor c2 = db.getIdOfSelectedItem(id);

            ListClickedFragment fragmentRonde1 = new ListClickedFragment();

            if (c2 != null) 
                c2.moveToFirst();

                // i want to set listClickedTitle from here but listClickedTitle is in ListClickedFragment
                fragmentRonde1.gettext(c2.getString(c2.getColumnIndex("title")));

            

        
    );

这是 ListCLickedTitle 所在片段的类:ListClickedFragment

public class ListClickedFragment extends android.support.v4.app.Fragment 

TextView tvTitle, tvDesc, tv, oop;


public ListClickedFragment() 
    // Required empty public constructor





@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) 
    // Inflate the layout for this fragment
    final View rootview2 = inflater.inflate(R.layout.fragment_list_clicked, container, false);

    tvTitle = (TextView)getActivity().findViewById(R.id.listClickedTitle);

    return rootview2;


public void gettext (String text) 

    tvTitle.setText(text);



LOGCAT

    10-28 09:37:25.373 14349-14349/justin.drawer E/InputEventReceiver: Exception dispatching input event.
10-28 09:37:25.381 14349-14349/justin.drawer E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at justin.drawer.ListClickedFragment.gettext(ListClickedFragment.java:40)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at justin.drawer.FragmentRonde1$1.onItemClick(FragmentRonde1.java:71)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.widget.AdapterView.performItemClick(AdapterView.java:310)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.widget.AbsListView$PerformClick.run(AbsListView.java:3042)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.widget.AbsListView.onTouchUp(AbsListView.java:3891)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.widget.AbsListView.onTouchEvent(AbsListView.java:3656)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.View.dispatchTouchEvent(View.java:9294)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2547)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2240)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.app.Activity.dispatchTouchEvent(Activity.java:2765)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.View.dispatchPointerEvent(View.java:9514)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857)
10-28 09:37:25.420 14349-14349/justin.drawer E/MessageQueue-JNI:    at android.view.ViewRootImpl$WindowInputEventReceive
10-28 09:37:25.420 14349-14349/justin.drawer D/AndroidRuntime: Shutting down VM

【问题讨论】:

显然它会是空对象,尝试在片段中创建一个方法来保存文本视图,并在从另一个片段中获取片段实例后调用该方法。 看看这个:***.com/questions/17436298/… 您可以使用setArguments(Bundle) 方法将字符串传递给片段。 所以我做了一个方法,但它仍然给了我一个 nullpointerEcxeption。你能看看我更新的问题吗? @AndreiVerdes 【参考方案1】:

你得到一个空指针异常,因为你的视图在 onCreateView 被调用后被夸大了。 您应该在 ListClickedFrgament 的构造函数中传递一个 Bundle 或 String 参数,然后使用它来设置文本。

ListClickedFragment:

    String textToSet;
    public ListClickedFragment(String text) 
           this.textToSet = text;
    

在 ListClickedFragment 的 OnViewCreated 方法中将此文本设置为您的 textview。

在fragmentRonde1中:

    ListClickedFragment fragmentRonde1 = new ListClickedFragment(c2.getString(c2.getColumnIndex("title")));

希望这会有所帮助。

【讨论】:

那么你把 this.textToSet 放在哪里,我把 textview 和 settext 放在哪里?我还不完全明白,但我知道你要去哪里@hadi 不是贾斯汀,关键是textview是一个UI组件,只有在onCreateView方法完成后才能得到它的引用。您将要做的是覆盖 ListClickedFragment 中的 onViewCreated 方法,在该方法中,您可以使用 textToSet 字符串并将其设置为您的 textview。 我明白你的意思了!如果可行,我将尝试并接受作为答案! @hadi 一直在尝试,但我仍然收到 NullPointerException :( 也无法使其捆绑工作。@hadi 2 分。 1. 替换这一行:tvTitle = (TextView)getActivity().findViewById(R.id.listClickedTitle);【参考方案2】:
tvTitle = (TextView)getActivity().findViewById(R.id.listClickedTitle);

在你的活动中找到 tvTitle 而不是片段

【讨论】:

【参考方案3】:

试试这个: 我在这里做了一些假设,我真的不明白为什么你有同一个片段的 2 个实例......而你没有与第二个(fragmentRonde1)进行交易......这不会显示在活动......我有点困惑,但也许这会有所帮助。

编辑:为什么你第二次得到 NullPointerException ?因为您只调用了ListClickedFragment fragmentRonde1 的构造函数。不与它进行交易,不在某些FrameLayout 中添加/替换它不会触发onCreateView() ...视图不会被夸大,因此 TextView 将为空。

lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 

        ListClickedFragment newFragment = ListClickedFragment.newInstance();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);

        transaction.commit();

        Cursor c2 = db.getIdOfSelectedItem(id);

        ListClickedFragment fragmentRonde1;

        if (c2 != null) 
            c2.moveToFirst();
            fragmentRonde1 = ListClickedFragment.newInstance(c2.getString(c2.getColumnIndex("title")));
         else 
            fragmentRonde1 = ListClickedFragment.newInstance();
        

    
);

还有这个

public class ListClickedFragment extends android.support.v4.app.Fragment 

public static final String STRING_TAG = "some_tag_in_here";

public static ListClickedFragment newInstance() 
    return new ListClickedFragment();


public static ListClickedFragment newInstance(String pString) 
    ListClickedFragment listClickedFragment = newInstance();
    Bundle bundle = new Bundle();
    bundle.putString(STRING_TAG, pString);


TextView tvTitle;
TextView tvDesc;
TextView tv;
TextView oop;

public ListClickedFragment() 


@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) 
    // Inflate the layout for this fragment
    final View rootview2 = inflater.inflate(R.layout.fragment_list_clicked, container, false);

    tvTitle = (TextView) getActivity().findViewById(R.id.listClickedTitle);
    if (getArguments() != null && getArguments().getString(STRING_TAG) != null) 
        tvTitle.setText(getArguments().getString(STRING_TAG));
    
    return rootview2;


【讨论】:

如果我一两个小时后回家一定会试试这个! @AndreiVerdes【参考方案4】:

您可以使用总线实现来管理片段和活动之间的所有操作。本教程将为您提供帮助! Tutroial...

【讨论】:

【参考方案5】:

第一步

在片段 A 中,使用 callback 访问活动。您可以从这里学习如何使用它。 Fragment Callbacks

第 2 步

在activity中,使用FragmentManager找到Fragment B,然后就可以访问Fragment B中的TextView

【讨论】:

以上是关于Android从另一个片段设置Textview的主要内容,如果未能解决你的问题,请参考以下文章

在android中从另一个片段获取数据到片段

从另一个片段打开一个片段始终保留前一个片段标题

从另一个图表导航到片段,而不是起始目的地

从另一个活动中打开一个活动的片段

使用片段从另一个类获取值时出现 NullPointerException [重复]

从另一个提供循环依赖的模块打开活动