Android从另一个片段设置Textview

时间:2015-10-27 23:48:27

标签: java android android-fragments android-studio nullpointerexception

我想将TextText设置为从片段到另一个片段的Textview。

我该怎么做?现在我得到一个: java.lang.NullPointerException:尝试调用虚方法' void android.widget.TextView.setText(java.lang.CharSequence)'在空对象引用上

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

5 个答案:

答案 0 :(得分: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")));

希望这有帮助。

答案 1 :(得分:1)

试试这个: 我在这里做了一些假设,我真的不明白为什么你有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;
}
}

答案 2 :(得分:0)

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

答案 3 :(得分:0)

第1步

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

第2步

在活动中,使用FragmentManager查找片段B,然后您就可以访问片段B中的TextView

答案 4 :(得分:0)

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

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