我该怎么做?现在我得到一个: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
由于在调用onCreateView之后,视图被膨胀,您将得到一个空指针异常。您应该在ListClickedFrgament的构造函数中传递一个Bundle或String参数,然后使用它来设置文本。
ListClickedFragment:
String textToSet;
public ListClickedFragment(String text) {
this.textToSet = text;
}
将此文本设置为ListClickedFragment的OnViewCreated方法中的textview。
ListClickedFragment fragmentRonde1 = new ListClickedFragment(c2.getString(c2.getColumnIndex("title")));
问题内容: 我是Android的新手,这是我的第二个应用程序。我正在创建一个选项卡式活动,其中第一个片段具有创建新任务的形式,第二个片段具有所有已保存任务的列表,第三个片段从第二个列表中选择时将显示任务注释分段。第三个片段也应该像一个聊天活动,当您键入评论并点击发送按钮时,它会发布评论。当我从另一个地方(在下面的链接的GitHub分支中)实施此聊天活动时,该应用程序将完全按预期运行。但是,当我尝试
我需要一些暑期项目的帮助 这是我的事件片段 这是我的MyList片段 我正在使用RecyclerViewCardView显示事件。其想法是,用户可以单击每张卡片右侧的大加号,卡片将显示在MyList片段中。我想问一下,是否可以将一张卡直接从一个片段转移到另一个片段?而且,这两个片段都包含在同一个活动中,这使得它有点棘手(我还没有找到任何可用的解决方案)。 如果不可能,另一种方法是将CardView
这是我完整的logcat: 在Android.support.v4.app.backStackRecord.doaddop(backStackRecord.java:414)在Android.support.v4.app.backStackRecord.replace(backStackRecord.java:449)在Android.support.v4.app.backStackRecord.
在我的中有5个片段(home、settings、newPost、notifications和profile)。 在中,我有一个,其中包含许多post视图,每个post都包含一个“open”按钮,该按钮应该打开一个。问题是,我只能在类和populateItemRows()方法中使用,所以当我不能使用... RecycerViewAdapter:
我知道的一种方法是通过活动。我们可以将数据从一个片段发送到另一个活动,再从一个活动发送到另一个片段。还有其他方法吗?
先生/女士,我想使用android JAVA将一个片段回调到另一个片段。我试图找到问题,但没有找到解决方案。若我使用接口,它会向活动发送回调响应,这是我不想要的。非常感谢。