gpt4 book ai didi

java - Android 从另一个 fragment 设置 Textview

转载 作者:行者123 更新时间:2023-11-30 08:45:16 25 4
gpt4 key购买 nike

我想将 setText 设置为从一个 fragment 到另一个 fragment 的 Textview。

我该怎么做?现在我得到一个:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a 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 所在 fragment 的类: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

最佳答案

您收到空指针异常,因为您的 View 在调用 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")));

希望这对您有所帮助。

关于java - Android 从另一个 fragment 设置 Textview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33380511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com